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);
};
import { Component, Input, Output } from "@angular/core";
import { ValueField } from "../value-field";
import { FormFunction } from "../../form-function";
import { OkButton } from "../button/ok-button";
import { CancelButton } from "../button/cancel-button";
import { TextField } from "../text/text-field.component";
import { FocusUtility } from "../../focus-utility";
var TranslationFieldComponent = /** @class */ (function () {
    function TranslationFieldComponent() {
    }
    __decorate([
        Input("field"),
        Output("field"),
        __metadata("design:type", TranslationField)
    ], TranslationFieldComponent.prototype, "field", void 0);
    TranslationFieldComponent = __decorate([
        Component({
            selector: "path-translationfield",
            templateUrl: "translation-field.component.html"
        })
    ], TranslationFieldComponent);
    return TranslationFieldComponent;
}());
export { TranslationFieldComponent };
var TranslationField = /** @class */ (function (_super) {
    __extends(TranslationField, _super);
    function TranslationField(form, pathService, translationService) {
        var _this = _super.call(this, form, translationService) || this;
        _this.pathService = pathService;
        _this.translationService = translationService;
        _this._defaultTranslation = null;
        _this.userLanguage = translationService.getUserLanguage();
        _this.languages = translationService.getSupportedLanguageCodes();
        var initialList = [];
        for (var _i = 0, _a = _this.languages; _i < _a.length; _i++) {
            var language = _a[_i];
            initialList.push([{ "key": language }, ""]);
        }
        _this.setValue(initialList);
        _this._translationLabel = translationService.getText("Translation");
        return _this;
    }
    Object.defineProperty(TranslationField.prototype, "defaultTranslation", {
        get: function () {
            return this._defaultTranslation;
        },
        set: function (value) {
            this._defaultTranslation = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TranslationField.prototype, "translationLabel", {
        get: function () {
            return this._translationLabel;
        },
        enumerable: false,
        configurable: true
    });
    TranslationField.prototype.setValue = function (value) {
        _super.prototype.setValue.call(this, value);
        this._defaultTranslation = this.getDefaultTranslation();
    };
    TranslationField.prototype.getDefaultTranslation = function () {
        if (this.value != null) {
            for (var _i = 0, _a = this.value; _i < _a.length; _i++) {
                var item = _a[_i];
                if (item[0] != null && item[0]["key"] === this.userLanguage) {
                    return item[1];
                }
            }
        }
        return null;
    };
    TranslationField.prototype.editTranslations = function () {
        var _this = this;
        var form = this.form.getApp().createDynamicForm();
        var translationFields = [];
        form["title"] = this.name + " " + this.translationService.getText("Translations");
        form["formFunction"] = new FormFunction();
        form["formFunction"].save = function (data) {
            var resultList = [];
            for (var _i = 0, translationFields_1 = translationFields; _i < translationFields_1.length; _i++) {
                var field = translationFields_1[_i];
                resultList.push([{ "key": field.id }, field.value]);
            }
            _this.setValue(resultList);
            _this.getForm().getApp().closeCurrentForm();
        };
        form["formFunction"].cancel = function () {
            _this.getForm().getApp().closeCurrentForm();
        };
        var translations = this.value;
        for (var _i = 0, translations_1 = translations; _i < translations_1.length; _i++) {
            var key = translations_1[_i];
            var textField = new TextField(form, this.translationService);
            textField.type = "text";
            textField.id = key[0]["key"];
            textField.name = this.translationService.getText(key[0]["key"]);
            textField.visible = true;
            textField.newRow = true;
            textField.width = 2;
            textField.height = this.height;
            textField.labelVisible = true;
            textField.required = true;
            textField.setValue(key[1]);
            form["fields"].push(textField);
            translationFields.push(textField);
        }
        var cancelButton = new CancelButton(form, this.translationService);
        cancelButton.type = "cancelButton";
        cancelButton.name = this.translationService.getText("Cancel");
        cancelButton.visible = true;
        form["fields"].push(cancelButton);
        var okButton = new OkButton(form, this.translationService);
        okButton.type = "okButton";
        okButton.name = this.translationService.getText("Ok");
        okButton.visible = true;
        form["fields"].push(okButton);
        form.updateRows();
        this.form.getApp()["_formStack"].push(form); // TODO
        FocusUtility.focusFirstField(form);
    };
    return TranslationField;
}(ValueField));
export { TranslationField };
var TranslationEntry = /** @class */ (function () {
    function TranslationEntry() {
    }
    Object.defineProperty(TranslationEntry.prototype, "text", {
        get: function () {
            return this._text;
        },
        set: function (value) {
            this._text = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TranslationEntry.prototype, "code", {
        get: function () {
            return this._code;
        },
        set: function (value) {
            this._code = value;
        },
        enumerable: false,
        configurable: true
    });
    return TranslationEntry;
}());
export { TranslationEntry };
//# sourceMappingURL=translation-field.component.js.map