import { __awaiter, __decorate, __extends, __generator } from "tslib";
import { Component, Emit, Prop, Watch } from 'vue-property-decorator';
import { FormArray } from '../../utils/form/form-array';
import { FormControl } from '../../utils/form/form-control';
import { FormGroup } from '../../utils/form/form-group';
import { FormatMode } from '../../utils/i18n/i18n';
import { getString } from '../../utils/str/str';
import { ModulVue } from '../../utils/vue/vue';
import { MMessage } from '../message/message';
import { MToast } from '../toast/toast';
import { MAccordionTransition } from '../transitions/accordion-transition/accordion-transition';
import { FormActions } from './form-action-type';
import WithRender from './form.html?style=./form.scss';
var MForm = /** @class */ (function (_super) {
    __extends(MForm, _super);
    function MForm() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.displaySummary = false;
        _this.displayToast = false;
        return _this;
    }
    MForm.prototype.emitSubmit = function () { };
    MForm.prototype.emitReset = function () { };
    MForm.prototype.onFormGroupErrorsChange = function (controlErrors) {
        if (this.formErrors.length === 0) {
            this._hideSummaryAndToast();
        }
    };
    Object.defineProperty(MForm.prototype, "formErrors", {
        get: function () {
            return this.formGroup.errorsDeep;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MForm.prototype, "summaryMessages", {
        get: function () {
            return this.formErrors.map(function (error) { return getString(error.groupMessage) || getString(error.message); });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MForm.prototype, "toastMessage", {
        get: function () {
            var count = this._formControlsInErrorCount();
            return this.$i18n.translate(count <= 1 ? 'm-form:multipleErrorsToCorrect' : 'm-form:multipleErrorsToCorrect.p', { totalNbOfErrors: count }, undefined, undefined, undefined, FormatMode.Sprintf);
        },
        enumerable: false,
        configurable: true
    });
    MForm.prototype.triggerActionFallouts = function (action) {
        var _this = this;
        this.actionFallouts
            .filter(function (a) { return action & a.action; })
            .forEach(function (a) { return a.fallout(_this); });
    };
    MForm.prototype.submit = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.formGroup.submit()];
                    case 1:
                        _a.sent();
                        if (!this.formGroup.valid) {
                            this.triggerActionFallouts(FormActions.InvalidSubmit);
                            return [2 /*return*/];
                        }
                        this.triggerActionFallouts(FormActions.ValidSubmit);
                        this.emitSubmit();
                        return [2 /*return*/];
                }
            });
        });
    };
    MForm.prototype.reset = function () {
        this.formGroup.reset();
        this.triggerActionFallouts(FormActions.Reset);
        this.emitReset();
    };
    MForm.prototype.created = function () {
        this.triggerActionFallouts(FormActions.Created);
    };
    MForm.prototype.updated = function () {
        this.triggerActionFallouts(FormActions.Updated);
    };
    MForm.prototype.beforeDestroy = function () {
        this.triggerActionFallouts(FormActions.Destroyed);
        this.formGroup.reset();
    };
    MForm.prototype._getAllFormValidators = function (control) {
        var _this = this;
        var result = control.validators;
        control.controls.forEach(function (c) {
            if (c instanceof FormControl) {
                result = result.concat(c.validators);
            }
            else if (c instanceof FormGroup || c instanceof FormArray) {
                result = result.concat(_this._getAllFormValidators(c));
            }
        });
        return result;
    };
    MForm.prototype._hideSummaryAndToast = function () {
        this.displaySummary = this.displayToast = false;
    };
    MForm.prototype._formControlsInErrorCount = function (control) {
        var _this = this;
        var first = false;
        if (!control) {
            control = this.formGroup;
            first = true;
        }
        return control.controls.reduce(function (a, c) {
            if (c instanceof FormGroup || c instanceof FormArray) {
                if (c.hasError()) {
                    a++;
                }
                if (c.hasErrorDeep()) {
                    a += _this._formControlsInErrorCount(c);
                }
            }
            else if (c instanceof FormControl && c.hasError()) {
                a++;
            }
            return a;
        }, first && this.formGroup.hasError() ? 1 : 0);
    };
    __decorate([
        Prop({
            required: true
        })
    ], MForm.prototype, "formGroup", void 0);
    __decorate([
        Prop({ default: function () { return ModulVue.prototype.$form.formActionFallouts || []; } })
    ], MForm.prototype, "actionFallouts", void 0);
    __decorate([
        Emit('submit')
    ], MForm.prototype, "emitSubmit", null);
    __decorate([
        Emit('reset')
    ], MForm.prototype, "emitReset", null);
    __decorate([
        Watch('formErrors')
    ], MForm.prototype, "onFormGroupErrorsChange", null);
    MForm = __decorate([
        WithRender,
        Component({
            components: {
                MAccordionTransition: MAccordionTransition,
                MToast: MToast,
                MMessage: MMessage
            }
        })
    ], MForm);
    return MForm;
}(ModulVue));
export { MForm };
//# sourceMappingURL=form.js.map