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);
};
import { Component, Input, Output } from "@angular/core";
import { ValueField } from "./field/value-field";
import { FieldListField } from "./field/fieldList/field-list-field.component";
import { FocusUtility } from "./focus-utility";
var FormComponent = /** @class */ (function () {
    function FormComponent() {
    }
    FormComponent.prototype.onSubmit = function () {
        this.form.close(true, false);
    };
    FormComponent.prototype.ngAfterViewInit = function () {
        FocusUtility.focusFirstField(this.form);
    };
    __decorate([
        Input("form"),
        Output("form"),
        __metadata("design:type", Form)
    ], FormComponent.prototype, "form", void 0);
    FormComponent = __decorate([
        Component({
            selector: "path-form",
            templateUrl: "form.component.html",
        })
    ], FormComponent);
    return FormComponent;
}());
export { FormComponent };
var Form = /** @class */ (function () {
    function Form(pathService, app) {
        this.pathService = pathService;
        this.app = app;
        this._fields = [];
        this._rows = [];
        this._headerVisible = true;
        this._footerVisible = true;
        this._borderStyle = BorderStyle.Shadow;
    }
    Form.prototype.getApp = function () {
        return this.app;
    };
    Form.prototype.getKey = function () {
        return this.key;
    };
    Object.defineProperty(Form.prototype, "key", {
        get: function () {
            return this._key;
        },
        set: function (value) {
            this._key = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "rows", {
        get: function () {
            return this._rows;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "title", {
        get: function () {
            return this._title;
        },
        set: function (value) {
            this._title = value;
        },
        enumerable: false,
        configurable: true
    });
    Form.prototype.getFields = function () {
        return this.fields;
    };
    Object.defineProperty(Form.prototype, "fields", {
        get: function () {
            return this._fields;
        },
        set: function (value) {
            this._fields = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "handler", {
        get: function () {
            return this._handler;
        },
        set: function (value) {
            this._handler = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "url", {
        get: function () {
            return this._url;
        },
        set: function (value) {
            this._url = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "bean", {
        get: function () {
            return this._bean;
        },
        set: function (value) {
            this._bean = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "formFunction", {
        get: function () {
            return this._formFunction;
        },
        set: function (value) {
            this._formFunction = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "headerVisible", {
        get: function () {
            return this._headerVisible;
        },
        set: function (value) {
            this._headerVisible = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "footerVisible", {
        get: function () {
            return this._footerVisible;
        },
        set: function (value) {
            this._footerVisible = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "borderStyle", {
        get: function () {
            return this._borderStyle;
        },
        set: function (value) {
            this._borderStyle = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Form.prototype, "urlDefaults", {
        get: function () {
            return this._urlDefaults;
        },
        enumerable: false,
        configurable: true
    });
    Form.prototype.fromJson = function (modelForm) {
        this.url = modelForm["url"];
        if (modelForm["urlDefaults"] != null) {
            this._urlDefaults = modelForm["urlDefaults"];
        }
        this.headerVisible = true;
        if (modelForm["headerVisible"] != null) {
            this.headerVisible = modelForm["headerVisible"];
        }
        this.footerVisible = true;
        if (modelForm["footerVisible"] != null) {
            this.footerVisible = modelForm["footerVisible"];
        }
        this.borderStyle = BorderStyle.Shadow;
        if (modelForm["borderStyle"] != null) {
            var borderStyleString = modelForm["borderStyle"];
            this.borderStyle = BorderStyle[borderStyleString];
        }
    };
    Form.prototype.updateRows = function () {
        var rows = [];
        var currentFormRow;
        for (var _i = 0, _a = this.fields; _i < _a.length; _i++) {
            var field = _a[_i];
            if (field instanceof FieldListField) {
                for (var _b = 0, _c = field.subfields; _b < _c.length; _b++) {
                    var subField = _c[_b];
                    currentFormRow = this.calculateFieldRow(subField, currentFormRow, rows);
                    currentFormRow.fields.push(subField);
                }
            }
            else {
                currentFormRow = this.calculateFieldRow(field, currentFormRow, rows);
                currentFormRow.fields.push(field);
            }
        }
        this._rows = rows;
    };
    Form.prototype.calculateFieldRow = function (field, currentFormRow, rows) {
        // auto-start new row with form width 2
        if (currentFormRow == null ||
            field.newRow ||
            currentFormRow.fields.length >= 2 ||
            field.width >= 2 ||
            currentFormRow.getWidth() >= 2) {
            field.newRow = true;
            currentFormRow = new FormRow();
            rows.push(currentFormRow);
        }
        return currentFormRow;
    };
    Form.prototype.close = function (save, remove) {
        if (save || remove) {
            // call close handler
            if (this.handler != null) {
                this.handler.doSave(this._bean);
            }
            var data = {};
            for (var _i = 0, _a = this._fields; _i < _a.length; _i++) {
                var field = _a[_i];
                if (field instanceof ValueField && field.id != null) {
                    data[field.id] = field.value;
                }
                if (field instanceof FieldListField) {
                    for (var _b = 0, _c = field.subfields; _b < _c.length; _b++) {
                        var subfield = _c[_b];
                        data[subfield.id] = subfield.value;
                    }
                }
            }
            if (remove) {
                this.pathService.serverDelete(this.app.getBackendUrl(), this.url, this.formFunction.delete);
            }
            else if (this.key == null) {
                // create
                this.pathService.serverPost(this.app.getBackendUrl(), this.url, data, this.formFunction.save, null);
            }
            else {
                // update (with key)
                this.pathService.serverPut(this.app.getBackendUrl(), this.url, data, this.formFunction.save);
            }
        }
        else {
            this.formFunction.cancel();
        }
    };
    Form.prototype.onKey = function (event) {
        if (event.keyCode === 27) { // esc
            this.close(false, false);
        }
    };
    return Form;
}());
export { Form };
var FormRow = /** @class */ (function () {
    function FormRow() {
        this._fields = [];
    }
    Object.defineProperty(FormRow.prototype, "fields", {
        get: function () {
            return this._fields;
        },
        set: function (value) {
            this._fields = value;
        },
        enumerable: false,
        configurable: true
    });
    FormRow.prototype.getWidth = function () {
        var sum = 0;
        for (var _i = 0, _a = this.fields; _i < _a.length; _i++) {
            var field = _a[_i];
            sum += field.width;
        }
        return sum;
    };
    FormRow.prototype.isVisible = function () {
        for (var _i = 0, _a = this.fields; _i < _a.length; _i++) {
            var field = _a[_i];
            if (field.visible) {
                return true;
            }
        }
        return false;
    };
    return FormRow;
}());
export { FormRow };
export var BorderStyle;
(function (BorderStyle) {
    // TODO use string enum after TS upgrade, then use enum in template (with member)
    BorderStyle[BorderStyle["None"] = 1] = "None";
    BorderStyle[BorderStyle["Shadow"] = 2] = "Shadow";
})(BorderStyle || (BorderStyle = {}));
//# sourceMappingURL=form.component.js.map