import { BindingEngine, Container, signalBindings } from 'aurelia-framework';
import { Config } from "../../../config";
import { EventAggregator } from 'aurelia-event-aggregator';
import { I18N } from '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 = Container.instance.get(Config);
        this.mlInput.languages = new Array();
        this.config.globalConfig.configuredLanguages.forEach(function (lang) {
            _this.mlInput.languages.push(lang);
        });
        Container.instance.get(EventAggregator).subscribe('i18n:locale:changed', function () {
            return _this.changeSelectedLangage();
        });
        this.i18n = Container.instance.get(I18N);
        this.bindingEngine = Container.instance.get(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 () {
                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;
}());
export { MLInputHelper };

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