"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MLInputHelper = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var config_1 = require("../../../config");
var aurelia_event_aggregator_1 = require("aurelia-event-aggregator");
var aurelia_i18n_1 = require("aurelia-i18n");
var MLInputHelper = (function () {
    function MLInputHelper(mlInput, changeSelectedLangageCallback) {
        if (changeSelectedLangageCallback === void 0) { changeSelectedLangageCallback = null; }
        var _this = this;
        this.changeSelectedLangageCallback = changeSelectedLangageCallback;
        this.subscribers = [];
        this.mlInput = mlInput;
        this.config = aurelia_framework_1.Container.instance.get(config_1.Config);
        this.mlInput.languages = new Array();
        this.config.globalConfig.configuredLanguages.forEach(function (lang) {
            _this.mlInput.languages.push(lang);
        });
        aurelia_framework_1.Container.instance.get(aurelia_event_aggregator_1.EventAggregator).subscribe('i18n:locale:changed', function () {
            return _this.changeSelectedLangage();
        });
        this.i18n = aurelia_framework_1.Container.instance.get(aurelia_i18n_1.I18N);
        this.bindingEngine = aurelia_framework_1.Container.instance.get(aurelia_framework_1.BindingEngine);
    }
    MLInputHelper.prototype.isTranslatable = function () {
        if (this.mlInput.value == null)
            return false;
        return Object.getOwnPropertyNames(this.mlInput.value).indexOf('_translation') >= 0;
    };
    MLInputHelper.prototype.changeSelectedLangage = function () {
        var currentLanguage = this.i18n.getLocale();
        if (currentLanguage == null)
            currentLanguage = "fr";
        this.mlInput.selectedLanguage = this.config.globalConfig.configuredLanguages.find(function (l) { return l.code.toLocaleLowerCase() == currentLanguage; });
        if (this.changeSelectedLangageCallback != null) {
            this.changeSelectedLangageCallback();
        }
    };
    MLInputHelper.prototype.onClear = function () {
        var _this = this;
        this.langProperties().forEach(function (p) { return _this.mlInput.value[p] = null; });
    };
    MLInputHelper.prototype.selectedLanguageCodeChanged = function (newValue, oldValue) {
        if (newValue != null && newValue != oldValue) {
            this.setSelectedLanguage();
        }
    };
    MLInputHelper.prototype.setSelectedLanguage = function () {
        var _this = this;
        this.mlInput.selectedLanguage = this.config.globalConfig.configuredLanguages.find(function (l) {
            return l.code.toLocaleLowerCase() == _this.mlInput.selectedLanguageCode.toLocaleLowerCase();
        });
    };
    MLInputHelper.prototype.disposeSubscribers = function () {
        var _a;
        if (((_a = this.subscribers) === null || _a === void 0 ? void 0 : _a.length) > 0) {
            this.subscribers.forEach(function (s) { return s.dispose(); });
            this.subscribers = [];
        }
    };
    MLInputHelper.prototype.rebindSubscribers = function (callback) {
        var _this = this;
        if (callback === void 0) { callback = null; }
        this.disposeSubscribers();
        this.langProperties().forEach(function (p) {
            return _this.subscribers.push(_this.bindingEngine
                .expressionObserver(_this.mlInput.value, p)
                .subscribe(function () {
                (0, aurelia_framework_1.signalBindings)("translation-changed");
                if (callback != null) {
                    callback();
                }
            }));
        });
    };
    MLInputHelper.prototype.langProperties = function () {
        if (this.mlInput.value != null) {
            var values = Object.values(this.mlInput.value);
            if ((values === null || values === void 0 ? void 0 : values.length) > 0) {
                return Object.keys(values[1]).filter(function (p) { return p.startsWith('lang'); });
            }
        }
        return [];
    };
    return MLInputHelper;
}());
exports.MLInputHelper = MLInputHelper;

//# sourceMappingURL=ml-input-helper.js.map
