"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("@angular/core");
var form_1 = require("../../components/form/form");
var autosaveAction_service_1 = require("../../services/autosaveAction/autosaveAction.service");
exports.DEFAULT_AUTOSAVE_DEBOUNCE = 3000;
var AutosaveDirective = (function () {
    function AutosaveDirective(form, autosaveAction) {
        var _this = this;
        this.keyupListener = this.resetDebounce;
        this.setDebounce = function () {
            if (_this.canAutosave()) {
                if (!_this.timer && _this.form.dirty && (_this.saveWhenInvalid || _this.form.validate())) {
                    _this.timer = setTimeout(_this.autosave, exports.DEFAULT_AUTOSAVE_DEBOUNCE);
                }
            }
        };
        this.autosave = function () {
            if (_this.canAutosave()) {
                var waitOn = _this.submitAndWait();
                if (waitOn) {
                    _this.autosaveAction.waitOn(waitOn).subscribe();
                }
                clearTimeout(_this.timer);
            }
        };
        this.form = form;
        this.autosaveAction = autosaveAction;
    }
    AutosaveDirective.prototype.ngAfterViewInit = function () {
        this.form.form.statusChanges.subscribe(this.setDebounce);
    };
    AutosaveDirective.prototype.ngOnDestroy = function () {
        if (this.timer) {
            clearTimeout(this.timer);
        }
    };
    AutosaveDirective.prototype.resetDebounce = function () {
        if (this.timer && this.canAutosave()) {
            clearTimeout(this.timer);
            this.timer = null;
            this.setDebounce();
        }
    };
    AutosaveDirective.prototype.submitAndWait = function () {
        if (this.saveWhenInvalid) {
            return this.form.saveForm();
        }
        else {
            return this.form.submitAndWait();
        }
    };
    AutosaveDirective.prototype.canAutosave = function () {
        return this.form.dirty && (this.saveWhenInvalid || this.form.validate());
    };
    return AutosaveDirective;
}());
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], AutosaveDirective.prototype, "saveWhenInvalid", void 0);
__decorate([
    core_1.HostListener('keyup'),
    __metadata("design:type", Object)
], AutosaveDirective.prototype, "keyupListener", void 0);
AutosaveDirective = __decorate([
    core_1.Directive({
        selector: '[rlAutosave]',
    }),
    __param(0, core_1.Self()),
    __metadata("design:paramtypes", [form_1.FormComponent,
        autosaveAction_service_1.AutosaveActionService])
], AutosaveDirective);
exports.AutosaveDirective = AutosaveDirective;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXV0b3NhdmUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJhdXRvc2F2ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7O0FBQUEsc0NBQW9GO0FBRXBGLG1EQUEyRDtBQUMzRCwrRkFBNkY7QUFHaEYsUUFBQSx5QkFBeUIsR0FBVyxJQUFJLENBQUM7QUFLdEQsSUFBYSxpQkFBaUI7SUFRN0IsMkJBQXFCLElBQW1CLEVBQ3BDLGNBQXFDO1FBRHpDLGlCQUlDO1FBVnNCLGtCQUFhLEdBQUcsSUFBSSxDQUFDLGFBQWEsQ0FBQztRQXNCMUQsZ0JBQVcsR0FBRztZQUNiLEVBQUUsQ0FBQyxDQUFDLEtBQUksQ0FBQyxXQUFXLEVBQUUsQ0FBQyxDQUFDLENBQUM7Z0JBQ3hCLEVBQUUsQ0FBQyxDQUFDLENBQUMsS0FBSSxDQUFDLEtBQUssSUFBSSxLQUFJLENBQUMsSUFBSSxDQUFDLEtBQUssSUFBSSxDQUFDLEtBQUksQ0FBQyxlQUFlLEl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