'use strict';

module.exports = AssetAPIProvider;

/* @ngInject */
function AssetAPIProvider($http, $q) {

  var entryUrl = 'http://localhost:9000/api.json';
  var siren = require('./siren/siren')({
    url: entryUrl,
    http: {
      get: $http.get
    }
  });

  return {
    getBreadcrumb: getBreadcrumb,
    getChildAssets: getChildAssets
  };

  function getBreadcrumb(path) {
    return siren.entity(path).then(function createBreadcrumb(entity) {
      var breadcrumb = [];
      var current = entity;
      do {
        breadcrumb.unshift({
          label: current.property('name'),
          href: '#browser' + current.path()
        });
        current = current.parent();
      } while (current.parent());
      return breadcrumb;
    });
  }

  function getChildAssets(path) {
    return siren
      .entity(path).then(function successGetChildAssets(entity) {            // Entity object
        return entity.children()
          .then(function transformChildren(children) {
            return children.map(entity2Asset);
          });
      }, function failure(err) {
        console.log('Error: ' + err);
      });
  }

  function entity2Asset (entity) {  // Entity#children(map/filter:function)
    var linkSelf = entity.link('self');
    var linkThumb = entity.link('thumbnail');
    var linkContent = entity.link('content');
    return {
      name: entity.property('name'),
      path: entity.path(),      // Entity#path()
      href: '#browser' + entity.path(),
      isFolder: entity.hasClass('assets/folder'),
      parentPath: entity.parent() && entity.parent().path(),
      url: linkSelf && linkSelf.href(),
      thumbnailUrl: linkThumb && linkThumb.href(),
      contentUrl: linkContent && linkContent.href()
    };
  }
}

@@ -397,32 +397,39 @@
 ren.entity(path)
+%0A      
 .then(function c
@@ -456,24 +456,26 @@
 ty) %7B%0A      
+  
 var breadcru
@@ -483,16 +483,18 @@
 b = %5B%5D;%0A
+  
       va
@@ -513,16 +513,18 @@
 entity;%0A
+  
       do
@@ -534,16 +534,18 @@
         
+  
 breadcru
@@ -557,16 +557,18 @@
 shift(%7B%0A
+  
         
@@ -598,24 +598,26 @@
 ty('name'),%0A
+  
           hr
@@ -652,28 +652,32 @@
 h()%0A        
+  
 %7D);%0A
+  
         curr
@@ -700,16 +700,18 @@
 rent();%0A
+  
       %7D 
@@ -738,24 +738,26 @@
 t());%0A      
+  
 return bread
@@ -763,16 +763,18 @@
 dcrumb;%0A
+  
     %7D);%0A
@@ -848,16 +848,23 @@
 ty(path)
+%0A      
 .then(fu
@@ -905,36 +905,8 @@
 y) %7B
-            // Entity object
 %0A   
@@ -934,27 +934,33 @@
 ildren()
+;
 %0A     
+ %7D)%0A
      
+ 
 .then(fu
@@ -1000,28 +1000,24 @@
 ) %7B%0A        
-    
 return child
@@ -1043,93 +1043,8 @@
 t);%0A
-          %7D);%0A      %7D, function failure(err) %7B%0A        console.log('Error: ' + err);%0A
     
@@ -1092,49 +1092,8 @@
 y) %7B
-  // Entity#children(map/filter:function)
 %0A   
@@ -1301,30 +1301,8 @@
 h(),
-      // Entity#path()
 %0A   

improve formatting of api.service.js