
"use strict";

import {Model} from '../classes/Model';
import {Collection} from '../classes/Collection';

let http;
let cache;

function appendTransform(transform) {
    let defaults = http.defaults.transformResponse;
    defaults = angular.isArray(defaults) ? defaults : [defaults];
    return defaults.concat(transform);
};

class Manager {

    constructor($http, $cacheFactory) {
        http = $http;
        if (cache === undefined) {
            cache = $cacheFactory('monad-managers');
        }
        // Set this per-manager where needed:
        this.model = Model;
        this.collection = new Collection();
    }

    get count() {
        return 0;
    }

    list(url) {
        return http({
            url,
            method: 'GET',
            transformResponse: appendTransform(values => {
                while (this.collection.length) {
                    this.collection.pop();
                }
                values.map(value => this.collection.push((new this.model()).$load(value)));
                return this.collection;
            }),
            cache
        });
    }

    find(url) {
        return http({
            url,
            method: 'GET',
            transformResponse: appendTransform(item => (new this.model()).$load(item)),
            cache
        });
    }

    update(model) {
        for (let field in model) {
            if (field.substring(0, 1) == '$') {
                continue;
            }
            if (typeof model[field] == 'object' && model[field] != null) {
                if ('$delete' in model[field] && model[field].$deleted) {
                    model[field].$delete();
                } else if ('$update' in model[field] && model[field].$dirty) {
                    model[field].$update();
                } else if ('map' in model[field]) {
                    model[field].map(item => {
                        if (typeof item == 'object') {
                            if ('$delete' in item && item.$deleted) {
                                item.$delete();
                            } else if ('$update' in item && item.$dirty) {
                                item.$update();
                            }
                        }
                    });
                }
            }
            if (model.$initial[field]) {
                model.$initial[field] = model.$data[field];
            }
        }
    }

    get http() {
        return http;
    }

};

Manager.$inject = ['$http', '$cacheFactory'];

export {Manager};


@@ -1313,21 +1313,19 @@
  %7D%0A%0A    
-updat
+sav
 e(model)
@@ -1339,259 +1339,89 @@
     
-for (let field in model) %7B%0A            if (field.substring(0, 1) == '$') %7B%0A                continue;%0A            %7D%0A            if (typeof model%5Bfield%5D == 'object' && model%5Bfield%5D != null) %7B%0A                if ('$delete' in model%5Bfield%5D && model%5Bfield%5D
+if (model.$new) %7B%0A            return this.create(model);%0A        %7D else if (model
 .$de
@@ -1445,52 +1445,46 @@
     
-        
-model%5Bfield%5D.$delete();%0A    
+return this%5B'delete'%5D(model);%0A
         
     
@@ -1471,36 +1471,32 @@
 model);%0A        
-    
 %7D else if ('$upd
@@ -1494,49 +1494,13 @@
 if (
-'$update' in model%5Bfield%5D && model%5Bfield%5D
+model
 .$di
@@ -1522,37 +1522,32 @@
     
-        model%5Bfield%5D.$
+return this.
 update(
+model
 );%0A 
@@ -1549,32 +1549,34 @@
 l);%0A        
+%7D%0A
         
 %7D else if ('
@@ -1567,623 +1567,583 @@
     
-%7D else if ('map' in model%5Bfield%5D) %7B
+return %7B%7D;
 %0A    
- 
+%7D%0A%0A
     
+/**%0A
      
-      model%5Bfield%5D.map(item =%3E %7B%0A                        if (typeof item == 'object') %7B%0A               
+* API interface. These should be overridden by a custom implementation,%0A     * since we have no way to guesstimate how your API works. Hence, these%0A     * throw an error as a friendly reminder :)%0A
      
+*%0A
      
-   if ('$delete' in item && item.$deleted) %7B%0A                                item.$delete();%0A                            %7D else if ('$update' in item && item.$dirty) %7B%0A                                item.$update();%0A                            %7D%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D%0A            if (model.$initial%5Bfield%5D) %7B%0A                model.$initial%5Bfield%5D = model.$data%5Bfield%5D;%0A            %7D%0A        %7D
+* The actual implementations should return promises.%0A     */%0A    create(model) %7B%0A        throw %22Manager.create must use a custom implementation.%22;%0A    %7D%0A%0A    update(model) %7B%0A        throw %22Manager.update must use a custom implementation.%22;%0A    %7D%0A%0A    %5B'delete'%5D(model) %7B%0A        throw %22Manager.delete must use a custom implementation.%22;
 %0A   

define stubs, and use a save service