"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var core_1 = require('angular2/core');
var _ = require('lodash');
var array_service_1 = require('../../array/array.service');
var http_service_1 = require('../../http/http.service');
var baseDataServiceBehavior_1 = require('../baseDataServiceBehavior');
var dataContractsHelper_service_1 = require('../dataContractsHelper/dataContractsHelper.service');
var DataService = (function () {
    function DataService(http, array, options) {
        this.array = array;
        this.behavior = new baseDataServiceBehavior_1.BaseDataServiceBehavior(http, options.transform);
        this.useDeepSearch = options.useDeepSearch;
        this.mockData = options.mockData;
        this.endpoint = options.endpoint;
        this.url = this.endpoint;
        this.useMock = options.useMock;
        this.logRequests = options.logRequests;
    }
    DataService.prototype.getItemEndpoint = function (id) {
        return this.url + '/' + id.toString();
    };
    DataService.prototype.getList = function (params) {
        var _this = this;
        var requestParams = {
            params: params,
            endpoint: this.url,
            getMockData: function () { return _this.mockData; },
            useMock: this.useMock,
            logRequests: this.logRequests,
        };
        if (this.useDeepSearch) {
            return this.behavior.search(requestParams);
        }
        else {
            return this.behavior.getList(requestParams);
        }
    };
    DataService.prototype.getDetail = function (id) {
        var _this = this;
        return this.behavior.getItem({
            endpoint: this.getItemEndpoint(id),
            getMockData: function () {
                return _.find(_this.mockData, function (item) {
                    return item.id === id;
                });
            },
            useMock: this.useMock,
            logRequests: this.logRequests,
        });
    };
    DataService.prototype.create = function (domainObject) {
        var _this = this;
        return this.behavior.create({
            domainObject: domainObject,
            endpoint: this.url,
            addMockData: function (data) {
                var nextId = _.maxBy(_this.mockData, 'id').id + 1;
                domainObject.id = nextId;
                _this.mockData.push(domainObject);
            },
            useMock: this.useMock,
            logRequests: this.logRequests,
        });
    };
    DataService.prototype.update = function (domainObject) {
        var _this = this;
        return this.behavior.update({
            domainObject: domainObject,
            endpoint: this.getItemEndpoint(domainObject.id),
            updateMockData: function (data) {
                var oldObject = _.find(_this.mockData, function (item) {
                    return item.id === data.id;
                });
                oldObject = _.assign(oldObject, data);
            },
            useMock: this.useMock,
            logRequests: this.logRequests,
        });
    };
    DataService.prototype.delete = function (domainObject) {
        var _this = this;
        return this.behavior.delete({
            domainObject: domainObject,
            endpoint: this.getItemEndpoint(domainObject.id),
            removeMockData: function (data) {
                _this.array.remove(_this.mockData, domainObject);
            },
            useMock: this.useMock,
            logRequests: this.logRequests,
        });
    };
    DataService.prototype.version = function (versionNumber) {
        var dataService = _.clone(this);
        dataService.url = dataContractsHelper_service_1.helper.versionEndpoint(dataService.url, versionNumber);
        return dataService;
    };
    DataService = __decorate([
        core_1.Injectable(),
        __param(0, core_1.Inject(http_service_1.httpToken)),
        __param(1, core_1.Inject(array_service_1.arrayToken)), 
        __metadata('design:paramtypes', [Object, Object, Object])
    ], DataService);
    return DataService;
}());
exports.DataService = DataService;
var DataServiceFactory = (function () {
    function DataServiceFactory(http, array) {
        this.http = http;
        this.array = array;
    }
    DataServiceFactory.prototype.getInstance = function (options) {
        return new DataService(this.http, this.array, options);
    };
    DataServiceFactory = __decorate([
        core_1.Injectable(),
        __param(0, core_1.Inject(http_service_1.httpToken)),
        __param(1, core_1.Inject(array_service_1.arrayToken)), 
        __metadata('design:paramtypes', [Object, Object])
    ], DataServiceFactory);
    return DataServiceFactory;
}());
exports.DataServiceFactory = DataServiceFactory;
exports.dataServiceToken = new core_1.OpaqueToken('A service for making http requests against a REST endpoint');
exports.DATA_SERVICE_PROVIDER = new core_1.Provider(exports.dataServiceToken, {
    useClass: DataServiceFactory,
});
function DataServiceProvider(options) {
    return core_1.provide(exports.dataServiceToken, {
        deps: [http_service_1.httpToken, array_service_1.arrayToken],
        useFactory: function (http, array) { return new DataService(http, array, options); },
    });
}
exports.DataServiceProvider = DataServiceProvider;
//# sourceMappingURL=data:application/json;base64, 