"use strict";
var angular = require("angular");
var _ = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var componentsDowngrade_1 = require("../../componentsDowngrade");
var triggers = require("./triggers/triggers.service");
exports.triggers = triggers;
var form_service_ng1_1 = require("../form/form.service.ng1");
exports.moduleName = 'rl.ui.services.autosave';
exports.factoryName = 'autosaveFactory';
var AutosaveService = (function () {
    function AutosaveService(notification, autosaveService, options, triggerServiceFactory, formService) {
        var _this = this;
        this.notification = notification;
        this.autosaveService = autosaveService;
        this.formService = formService;
        this.autosave = function () {
            var data = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                data[_i] = arguments[_i];
            }
            var result = _this.validateAndSave.apply(_this, data);
            if (_.isBoolean(result)) {
                return result;
            }
            else {
                _this.autosaveService.waitOn(result).subscribe();
                return true;
            }
        };
        this.contentForm = options.contentForm || this.nullForm();
        this.save = options.save;
        this.saveWhenInvalid = options.saveWhenInvalid;
        this.triggerService = triggerServiceFactory.getInstance();
        this.configureTriggers(options);
        this.triggerService.setTriggers(options.triggers, this.autosave);
    }
    AutosaveService.prototype.validateAndSave = function () {
        var _this = this;
        var data = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            data[_i] = arguments[_i];
        }
        if (this.contentForm.$pristine) {
            return true;
        }
        if (this.contentForm.$valid || this.saveWhenInvalid) {
            var promise = this.save.apply(this, data);
            if (!_.isUndefined(promise)) {
                return promise.then(function () {
                    if (_this.contentForm != null) {
                        _this.contentForm.$setPristine();
                    }
                });
            }
            return true;
        }
        else {
            var error = this.formService.getAggregateError(this.contentForm);
            if (error) {
                this.notification.warning(error);
            }
            else {
                throw new Error('The form is invalid but there are no validation errors to show');
            }
            return false;
        }
    };
    AutosaveService.prototype.configureTriggers = function (options) {
        this.triggerService.triggers.onChange.configure({
            form: options.contentForm,
            setChangeListener: options.setChangeListener,
            debounceDuration: options.debounceDuration,
            saveWhenInvalid: options.saveWhenInvalid,
        });
    };
    AutosaveService.prototype.nullForm = function () {
        return {
            $pristine: false,
            $dirty: true,
            $valid: true,
            $setPristine: function () {
                return;
            },
        };
    };
    return AutosaveService;
}());
autosaveServiceFactory.$inject = [typescript_angular_utilities_1.downgrade.notificationServiceName, componentsDowngrade_1.autosaveActionServiceName, triggers.factoryName, form_service_ng1_1.serviceName];
function autosaveServiceFactory(notification, autosaveService, triggerServiceFactory, formService) {
    'use strict';
    return {
        getInstance: function (options) {
            return new AutosaveService(notification, autosaveService, options, triggerServiceFactory, formService);
        }
    };
}
angular.module(exports.moduleName, [typescript_angular_utilities_1.downgrade.moduleName, componentsDowngrade_1.moduleName, triggers.moduleName, form_service_ng1_1.moduleName])
    .factory(exports.factoryName, autosaveServiceFactory);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXV0b3NhdmUuc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImF1dG9zYXZlLnNlcnZpY2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLGlDQUFtQztBQUNuQywwQkFBNEI7QUFFNUIsNkVBQW1FO0FBSW5FLGlFQUErRztBQUMvRyxzREFBd0Q7QUFJL0MsNEJBQVE7QUFIakIsNkRBQWtIO0FBS3ZHLFFBQUEsVUFBVSxHQUFXLHlCQUF5QixDQUFDO0FBQy9DLFFBQUEsV0FBVyxHQUFXLGlCQUFpQixDQUFDO0FBaUJuRDtJQU1DLHlCQUFvQixZQUFpRCxFQUN6RCxlQUF1QyxFQUMvQyxPQUFnQyxFQUNoQyxxQkFBc0QsRUFDOUMsV0FBeUI7UUFKckMsaUJBWUM7UUFabUIsaUJBQVksR0FBWixZQUFZLENBQXFDO1FBQ3pELG9CQUFlLEdBQWYsZUFBZSxDQUF3QjtRQUd2QyxnQkFBVyxHQUFYLFdBQVcsQ0FBYztRQVVyQyxhQUFRLEdBQWtDO1lBQUMsY0FBYztpQkFBZCxVQUFjLEVBQWQscUJBQWMsRUFBZCxJQUFjO2dCQUFkLH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