"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIInputPhone = void 0;
var base_input_1 = require("./base-input");
var aurelia_framework_1 = require("aurelia-framework");
var libphonenumber_js_1 = require("libphonenumber-js");
var exports_1 = require("../../exports");
var UIInputPhone = exports.UIInputPhone = (function (_super) {
    __extends(UIInputPhone, _super);
    function UIInputPhone(element) {
        var _this = _super.call(this, element) || this;
        _this.defaultCountry = "BE";
        _this.autofocus = false;
        _this.currentPlaceholder = "";
        _this.codeFlagsLib = new exports_1.CountryDialCodes();
        return _this;
    }
    UIInputPhone.prototype.valueChanged = function (value) {
        this.validateValue(value);
    };
    ;
    UIInputPhone.prototype.currentCountryChanged = function (newValue, oldValue) {
        this.setDialCode(newValue, oldValue);
        var ayt = new libphonenumber_js_1.AsYouType({ defaultCountry: this.currentCountry, defaultCallingCode: this.currentDialCode });
        ayt.input(this.currentDialCode);
        this.setPlaceholder(ayt);
    };
    ;
    UIInputPhone.prototype.isValidChanged = function (value, oldValue) {
        if (value == true) {
            this.fadeValidIcon();
        }
    };
    ;
    UIInputPhone.prototype.attached = function () {
        var _this = this;
        this.initCountriesList();
        if (this.defaultCountry && this.value == null || this.value.toString().trim().length == 0) {
            this.currentCountry = this.defaultCountry;
        }
        this.validateValue(this.value, false);
        exports_1.UIInternal.subscribe(exports_1.UIInternal.EVT_CHANGE_LANGUAGE, function () {
            _this.initCountriesList();
        });
    };
    UIInputPhone.prototype.initCountriesList = function () {
        this.countriesList = this.codeFlagsLib.getCountryListMap().sortBy("isFavorite", false);
        this.prepareHtmlForSelectOptions();
    };
    UIInputPhone.prototype.setCurrentDialCodeAsValue = function () {
        if ((this.value == null || this.value.trim().length == 0) && this.currentDialCode != null) {
            this.value = this.currentDialCode + " ";
        }
    };
    UIInputPhone.prototype.setDialCode = function (newValue, oldValue) {
        var _a;
        if (this.countriesList == null)
            return;
        var ctry = this.countriesList.find(function (co) { return co.code == newValue; });
        this.currentDialCode = ctry === null || ctry === void 0 ? void 0 : ctry.dialCode;
        if (((_a = this.value) === null || _a === void 0 ? void 0 : _a.trim().length) > 0) {
            var oldCtry = this.countriesList.find(function (co) { return co.code == oldValue; });
            if (oldCtry != null && ctry != null && this.value.replace(" ", "").includes(oldCtry.dialCode)) {
                this.value = this.value.replace(" ", "").replace(oldCtry.dialCode, ctry.dialCode) + " ";
            }
            else if (!this.value.includes(ctry.dialCode)) {
                this.value = ctry.dialCode + this.value;
            }
        }
    };
    UIInputPhone.prototype.prepareHtmlForSelectOptions = function () {
        var _a;
        (_a = this.countriesList) === null || _a === void 0 ? void 0 : _a.filter(function (co) { return co; }).forEach(function (co) {
            co.htmlForSelectOption =
                "<div class=\"ui-input-phone__option-country\">\n                    <span class=\"ui-input-phone__option-country__flag\"><span class=\"ui-flag ".concat(co.code, "\"></span></span>\n                    <span class=\"ui-input-phone__option-country__dial-code\">").concat(co.dialCode, "</span>    \n                    <span class=\"ui-input-phone__option-country__country\">").concat(co.country, "</span>\n                    ").concat(co.isFavorite ? "<span class='ui-input-phone__option-country__favorite digi-star'></span>" : "", "\n                    <span class=\"ui-input-phone__option-country__code\">").concat(co.code, "</span>\n                </div>");
        });
    };
    UIInputPhone.prototype.validateValue = function (value, checkValidity) {
        if (checkValidity === void 0) { checkValidity = true; }
        this.errors = [];
        if (value == null || value.toString().trim().length == 0) {
            this.isValid = null;
            return;
        }
        if (this.isValidFadeTimer)
            clearTimeout(this.isValidFadeTimer);
        var ayt = new libphonenumber_js_1.AsYouType();
        ayt.input(value);
        this.formatPhoneValue(ayt, checkValidity);
        this.checkNewCountry(ayt);
    };
    UIInputPhone.prototype.formatPhoneValue = function (ayt, checkValidity) {
        var _this = this;
        var _a;
        if (checkValidity === void 0) { checkValidity = true; }
        try {
            var formattedValue_1 = ayt.formattedOutput;
            if (checkValidity)
                this.setIsValid(formattedValue_1);
            if (this.value !== formattedValue_1 && (formattedValue_1 === null || formattedValue_1 === void 0 ? void 0 : formattedValue_1.length) > 3) {
                var focusSelStart_1, focusSelEnd_1;
                var focusSelDir_1;
                var inputRef_1;
                if (this.uiInputRef) {
                    inputRef_1 = this.uiInputRef.querySelector('input');
                    if ((inputRef_1 === null || inputRef_1 === void 0 ? void 0 : inputRef_1.selectionStart) < ((_a = this.value) === null || _a === void 0 ? void 0 : _a.length)) {
                        focusSelStart_1 = inputRef_1.selectionStart;
                        focusSelEnd_1 = inputRef_1.selectionEnd;
                        focusSelDir_1 = inputRef_1.selectionDirection;
                    }
                }
                exports_1.UIInternal.queueTask(function () {
                    _this.value = formattedValue_1;
                    if (focusSelStart_1 != null && (inputRef_1 === null || inputRef_1 === void 0 ? void 0 : inputRef_1.setSelectionRange) != null) {
                        exports_1.UIInternal.queueTask(function () {
                            inputRef_1.setSelectionRange(focusSelStart_1, focusSelEnd_1, focusSelDir_1);
                        });
                    }
                });
            }
        }
        catch (error) {
            console.error(error);
        }
    };
    UIInputPhone.prototype.setIsValid = function (value) {
        if (value == null || value.trim().length == 0) {
            this.isValid = null;
            return;
        }
        var pn = (0, libphonenumber_js_1.parsePhoneNumber)(value);
        this.isValid = null;
        if (this.isValidTimer)
            clearTimeout(this.isValidTimer);
        this.isValid = pn.isValid();
    };
    UIInputPhone.prototype.checkNewCountry = function (ayt) {
        var ctry = ayt.getCountry();
        if (ctry != null && this.currentCountry != ctry) {
            this.currentCountry = ctry;
        }
    };
    UIInputPhone.prototype.setPlaceholder = function (ayt) {
        var _a;
        var template = (_a = ayt.formatter) === null || _a === void 0 ? void 0 : _a.template;
        if (template != null && this.currentDialCode != null) {
            this.currentPlaceholder = this.currentDialCode + template.substring(this.currentDialCode.length);
        }
        else {
            this.currentPlaceholder = null;
        }
    };
    UIInputPhone.prototype.fadeValidIcon = function (delay) {
        var _this = this;
        if (delay === void 0) { delay = 3000; }
        if (this.isValidFadeTimer)
            clearTimeout(this.isValidFadeTimer);
        this.isValidFadeTimer = setTimeout(function () {
            _this.isValid = null;
        }, delay);
    };
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UIInputPhone.prototype, "value", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", String)
    ], UIInputPhone.prototype, "defaultCountry", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Boolean)
    ], UIInputPhone.prototype, "autofocus", void 0);
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", String)
    ], UIInputPhone.prototype, "currentCountry", void 0);
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", Boolean)
    ], UIInputPhone.prototype, "isValid", void 0);
    UIInputPhone = __decorate([
        (0, aurelia_framework_1.customElement)("ui-input-phone"),
        __metadata("design:paramtypes", [Element])
    ], UIInputPhone);
    return UIInputPhone;
}(base_input_1.BaseInput));

//# sourceMappingURL=ui-input-phone.js.map
