(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../hooks/useStateProps", "../InputMask", "./components/SelectCountries", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var useStateProps_1 = require("../hooks/useStateProps");
    var InputMask_1 = (0, tslib_1.__importDefault)(require("../InputMask"));
    var SelectCountries_1 = (0, tslib_1.__importDefault)(require("./components/SelectCountries"));
    var styles_1 = require("./styles");
    var REGEX_SPACES = /\s/g;
    var MASK = [
        "+",
        "7",
        " ",
        /[1-9]/,
        /\d/,
        /\d/,
        " ",
        /\d/,
        /\d/,
        /\d/,
        " ",
        /\d/,
        /\d/,
        /\d/,
        /\d/
    ];
    var InputPhone = function (_a) {
        var onFocus = _a.onFocus, onBlur = _a.onBlur, value = _a.value, onChange = _a.onChange, _b = _a.testId, testId = _b === void 0 ? "inputPhone" : _b, isDisabled = _a.isDisabled, props = (0, tslib_1.__rest)(_a, ["onFocus", "onBlur", "value", "onChange", "testId", "isDisabled"]);
        var _c = (0, tslib_1.__read)((0, react_1.useState)(props.isAutoFocus), 2), isFocused = _c[0], setFocused = _c[1];
        var _d = (0, tslib_1.__read)((0, useStateProps_1.useStateProps)(value), 2), localValue = _d[0], setLocalValue = _d[1];
        var ref = (0, react_1.useRef)(null);
        var handleFocus = function (event) {
            setFocused(true);
            if (event.target.value.length === 0) {
                setTimeout(function () {
                    var _a;
                    ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.inputElement).setSelectionRange(2, 2);
                }, 0);
            }
            onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
        };
        var handleBlur = function (event, _value) {
            setFocused(false);
            if (_value.replace(REGEX_SPACES, "") === "+7") {
                setLocalValue("");
            }
            onBlur === null || onBlur === void 0 ? void 0 : onBlur(event, _value);
        };
        var handleChange = function (event, _value, id) {
            var val = _value.replace(REGEX_SPACES, " ");
            setLocalValue(val);
            onChange === null || onChange === void 0 ? void 0 : onChange(event, val, id);
        };
        return (react_1.default.createElement(InputMask_1.default, (0, tslib_1.__assign)({ testId: testId }, props, { mask: MASK, isDisabled: isDisabled, iconLeft: isFocused || Boolean(localValue) ? (react_1.default.createElement(SelectCountries_1.default, { isDisabled: isDisabled })) : undefined, onFocus: handleFocus, onBlur: handleBlur, onChange: handleChange, value: localValue, isShowMaskOnFocus: true, isShowMask: isFocused, placeholderChar: "\u2000", refMask: ref, iconRight: !isFocused && !localValue ? react_1.default.createElement(styles_1.RightContent, null) : undefined })));
    };
    exports.default = InputPhone;
});
