"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 forms_1 = require("@angular/forms");
var rxjs_1 = require("rxjs");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __notification = typescript_angular_utilities_1.services.notification;
var async_service_1 = require("../../services/async/async.service");
var form_service_1 = require("../../services/form/form.service");
exports.baseInputs = {
    save: 'save',
};
var FormComponent = (function () {
    function FormComponent(notification, asyncHelper, formService, guidService, parentForm) {
        this.submitted = new rxjs_1.Subject();
        this.notification = notification;
        this.asyncHelper = asyncHelper;
        this.formService = formService;
        this.form = new forms_1.FormGroup({});
        if (!this.save) {
            this.save = function () { return null; };
        }
        if (parentForm) {
            parentForm.form.addControl("form-" + guidService.random(), this.form);
        }
    }
    Object.defineProperty(FormComponent.prototype, "dirty", {
        get: function () {
            return this.form.dirty;
        },
        enumerable: true,
        configurable: true
    });
    FormComponent.prototype.submit = function () {
        if (this.validate()) {
            // kicks off the subscription since no observable is returned
            this.saveForm().subscribe();
            return true;
        }
        else {
            this.showErrors();
            return false;
        }
    };
    FormComponent.prototype.submitAndWait = function () {
        if (this.validate()) {
            return this.saveForm();
        }
        else {
            this.showErrors();
            return rxjs_1.Observable.of(false);
        }
    };
    FormComponent.prototype.validate = function () {
        return this.form.valid;
    };
    FormComponent.prototype.reset = function () {
        this.form.reset();
    };
    FormComponent.prototype.saveForm = function () {
        var waitOn = this.save(this.form.value);
        var request = this.asyncHelper.waitAsObservable(waitOn);
        return this.resetAfterSubmit(request);
    };
    FormComponent.prototype.showErrors = function () {
        var error = this.formService.getAggregateError(this.form);
        if (error) {
            this.notification.warning(error);
        }
        else {
            throw new Error('The form is invalid but there are no validation errors to show');
        }
    };
    FormComponent.prototype.resetAfterSubmit = function (request) {
        var _this = this;
        return request.do(function () { return _this.form.markAsPristine(); });
    };
    return FormComponent;
}());
__decorate([
    core_1.Input(),
    __metadata("design:type", Function)
], FormComponent.prototype, "save", void 0);
FormComponent = __decorate([
    core_1.Component({
        selector: 'rlForm',
        template: require('./form.html'),
    }),
    __param(4, core_1.Optional()), __param(4, core_1.SkipSelf()),
    __metadata("design:paramtypes", [__notification.NotificationService, async_service_1.AsyncHelper,
        form_service_1.FormService, typescript_angular_utilities_1.services.guid.GuidService, FormComponent])
], FormComponent);
exports.FormComponent = FormComponent;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9ybS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImZvcm0udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7OztBQUFBLHNDQUFnRjtBQUNoRix3Q0FBZ0U7QUFDaEUsNkJBQTJDO0FBRzNDLDZFQUF3RDtBQUN4RCxJQUFPLGNBQWMsR0FBRyx1Q0FBUSxDQUFDLFlBQVksQ0FBQztBQUU5QyxvRUFBNkU7QUFDN0UsaUVBQStEO0FBTWxELFFBQUEsVUFBVSxHQUFvQjtJQUMxQyxJQUFJLEVBQUUsTUFBTTtDQUNaLENBQUM7QUFVRixJQUFhLGFBQWE7SUFhekIsdUJBQVksWUFBZ0QsRUFDekQsV0FBd0IsRUFDeEIsV0FBd0IsRUFDeEIsV0FBc0MsRUFDZCxVQUF5QjtRQVZwRCxjQUFTLEdBQWtCL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