"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 core_1 = require("@angular/core");
var rxjs_1 = require("rxjs");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var TimeoutService = typescript_angular_utilities_1.services.timeout.TimeoutService;
var async_service_1 = require("../async/async.service");
exports.COMPLETE_MESSAGE_DURATION = 1000;
var AutosaveActionService = (function () {
    function AutosaveActionService(timeoutService, asyncService) {
        var _this = this;
        this.autosaveSuccessful = function () {
            _this.resolveAutosave(true);
        };
        this.autosaveFailed = function () {
            _this.resolveAutosave(false);
        };
        this.resolveAutosave = function (success) {
            _this._saving$.next(false);
            _this._complete$.next(true);
            _this._successful$.next(success);
            _this.timeoutService.setTimeout(function () { return _this._complete$.next(false); }, exports.COMPLETE_MESSAGE_DURATION);
        };
        this.timeoutService = timeoutService;
        this.asyncService = asyncService;
        this._saving$ = new rxjs_1.BehaviorSubject(false);
        this._complete$ = new rxjs_1.BehaviorSubject(false);
        this._successful$ = new rxjs_1.BehaviorSubject(false);
    }
    Object.defineProperty(AutosaveActionService.prototype, "saving$", {
        get: function () {
            return this._saving$.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AutosaveActionService.prototype, "complete$", {
        get: function () {
            return this._complete$.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AutosaveActionService.prototype, "successful$", {
        get: function () {
            return this._successful$.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    AutosaveActionService.prototype.waitOn = function (waitOn) {
        this._saving$.next(true);
        return this.asyncService.waitAsObservable(waitOn)
            .do(this.autosaveSuccessful, this.autosaveFailed);
    };
    return AutosaveActionService;
}());
AutosaveActionService = __decorate([
    core_1.Injectable(),
    __metadata("design:paramtypes", [TimeoutService,
        async_service_1.AsyncHelper])
], AutosaveActionService);
exports.AutosaveActionService = AutosaveActionService;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXV0b3NhdmVBY3Rpb24uc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImF1dG9zYXZlQWN0aW9uLnNlcnZpY2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLHNDQUEyQztBQUMzQyw2QkFBbUQ7QUFFbkQsNkVBQXdEO0FBQ3hELElBQU8sY0FBYyxHQUFHLHVDQUFRLENBQUMsT0FBTyxDQUFDLGNBQWMsQ0FBQztBQUV4RCx3REFBaUU7QUFFcEQsUUFBQSx5QkFBeUIsR0FBVyxJQUFJLENBQUM7QUFVdEQsSUFBYSxxQkFBcUI7SUFJakMsK0JBQVksY0FBOEIsRUFDdEMsWUFBeUI7UUFEN0IsaUJBT0M7UUF3Qk8sdUJBQWtCLEdBQUc7WUFDNUIsS0FBSSxDQUFDLGVBQWUsQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUM1QixDQUFDLENBQUE7UUFFTyxtQkFBYyxHQUFHO1lBQ3hCLEtBQUksQ0FBQyxlQUFlLENBQUMsS0FBSyxDQUFDLENBQUM7UUFDN0IsQ0FBQyxDQUFBO1FBRU8sb0JBQWUsR0FBRyxVQUFDLE9BQWdCO1lBQzFDLEtBQUksQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxDQUFDO1lBQzFCLEtBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLElBQUks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