var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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); }
};
import { PageElement, Key } from "../page-element";
import { PathService } from "../../../service/path.service";
import { Inject } from "@angular/core";
import { FormFunction } from "../../../form/form-function";
import { TranslationService } from "../../../service/translation.service";
import { FocusUtility } from "../../../form/focus-utility";
var InlineForm = /** @class */ (function (_super) {
    __extends(InlineForm, _super);
    function InlineForm(app, pathService, translationService) {
        var _this = _super.call(this, app) || this;
        _this.pathService = pathService;
        _this.translationService = translationService;
        return _this;
    }
    Object.defineProperty(InlineForm.prototype, "url", {
        get: function () {
            return this._url;
        },
        set: function (value) {
            this._url = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(InlineForm.prototype, "formId", {
        get: function () {
            return this._formId;
        },
        set: function (value) {
            this._formId = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(InlineForm.prototype, "form", {
        get: function () {
            return this._form;
        },
        set: function (value) {
            this._form = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(InlineForm.prototype, "page", {
        get: function () {
            return this._page;
        },
        set: function (value) {
            this._page = value;
        },
        enumerable: false,
        configurable: true
    });
    InlineForm.prototype.fromJson = function (modelFormField) {
        _super.prototype.fromJson.call(this, modelFormField);
        if (modelFormField["form"] != null) {
            this.formId = modelFormField["form"];
        }
        if (modelFormField["page"] != null) {
            this.page = modelFormField["page"];
        }
        if (modelFormField["key"] == null) {
            // only set key if not set by model
            this.key = this.parentPageElement != null ? this.parentPageElement.key : null;
        }
    };
    InlineForm.prototype.loadNextForm = function (forward) {
        var _this = this;
        if (this._url != null) {
            this.pathService.serverGet(this.app.getBackendUrl(), this.url, function (data) {
                if (data != null && data["length"] != null && data.length > 0) {
                    var foundNewKey = false;
                    if (_this._currentKey == null) {
                        var firstItem = data[0];
                        _this._currentKey = new Key(firstItem["key"]["key"], firstItem["key"]["name"]);
                        foundNewKey = true;
                    }
                    else {
                        var counter = 0;
                        for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
                            var item = data_1[_i];
                            counter++;
                            if (item["key"]["key"] === _this._currentKey.getKey() && item["key"]["name"] === _this._currentKey.getName()) {
                                if (forward && data.length > counter) {
                                    _this._currentKey = new Key(data[counter]["key"]["key"], data[counter]["key"]["name"]);
                                    foundNewKey = true;
                                }
                                else if (!forward && counter > 1) {
                                    _this._currentKey = new Key(data[counter - 2]["key"]["key"], data[counter - 2]["key"]["name"]);
                                    foundNewKey = true;
                                }
                                break;
                            }
                        }
                    }
                    if (_this._currentKey != null && foundNewKey) {
                        console.log("load next inline form with key " + _this._currentKey.getKey() + "/" + _this._currentKey.getName());
                        var formFunction = new FormFunction();
                        formFunction.save = function (formdata) {
                            _this.loadNextForm(true);
                        };
                        formFunction.cancel = function () {
                            _this.loadNextForm(true);
                        };
                        formFunction.delete = function (formdata) {
                            _this.loadNextForm(false);
                        };
                        _this._form = _this.app.createForm(_this._formId, _this._currentKey, null, formFunction, _this);
                        _this.name = _this._form.title;
                        window.setTimeout(function () { FocusUtility.focusFirstField(_this.form); }, 1);
                    }
                    else {
                        _this._form = null;
                        if (_this.page == null) {
                            _this.app.navigateBack();
                        }
                        else {
                            _this.app.setCurrentPage(_this.page, _this);
                        }
                    }
                }
                else {
                    window.alert(_this.translationService.getText("NoDataError"));
                    _this.app.navigateBack();
                }
            }, null);
        }
    };
    InlineForm = __decorate([
        __param(1, Inject(PathService)),
        __param(2, Inject(TranslationService)),
        __metadata("design:paramtypes", [Object, PathService,
            TranslationService])
    ], InlineForm);
    return InlineForm;
}(PageElement));
export { InlineForm };
//# sourceMappingURL=inline-form.js.map