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GF,UAAU,YAqBR;IAEF,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,QAAQ;IAOhB,QAAQ;IAQR,OAAO,CAAC,mBAAmB;IAsD3B,OAAO,CAAC,UAAU;IAkClB,OAAO,CAAC,QAAQ;IAQhB;;OAEG;IACI,UAAU,IAAI,gBAAgB;IAIrC;;OAEG;IACI,KAAK;IAIZ;;OAEG;IACI,IAAI;IAIX;;OAEG;IACI,QAAQ;CAGlB;;AAED,wBAA8E","file":"intl-phone-input.d.ts","sourcesContent":["/* This Source Code Form is subject to the terms of the Mozilla Public\n * License, v. 2.0. If a copy of the MPL was not distributed with this\n * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n\n// @ts-nocheck\n\nimport React from 'react';\nimport { createCn } from 'bem-react-classname';\nimport { withTheme } from '../cn';\n\nimport FlagIcon from '../flag-icon/flag-icon';\nimport Input, { InputProps } from '../input/input';\nimport Select from '../select/select';\n\nimport countries from '../lib/countries';\nimport getRelatedTarget from '../lib/related-target';\n\nconst MAX_DIAL_CODE_LENGTH = 4;\n\nexport type IntlPhoneInputProps = InputProps;\n\ntype IntlPhoneInputState = {\n    countryIso2: string;\n    inputFocused: boolean;\n    inputValue: string;\n    selectFocused: boolean;\n    onceOpened: boolean;\n}\n\n/**\n * Компонент ввода международного телефона по маске.\n */\nexport class IntlPhoneInput extends React.PureComponent<IntlPhoneInputProps, IntlPhoneInputState> {\n    protected cn = createCn('intl-phone-input');\n\n    static defaultProps: Partial<IntlPhoneInputProps> = {\n        size: 'm',\n        value: '+7',\n    };\n\n    state = {\n        countryIso2: 'ru',\n        inputFocused: false,\n        inputValue: this.props.value,\n        selectFocused: false,\n        onceOpened: false,\n    };\n\n    private countries;\n\n    // TODO [issues/1018] переписать тесты нужно, что бы private был\n    input;\n\n    private select;\n\n    private timeoutId;\n\n    private util;\n\n    private asYouType;\n\n    async componentDidMount() {\n        await this.loadUtil();\n        this.setCountry();\n    }\n\n    // eslint-disable-next-line camelcase\n    UNSAFE_componentWillUpdate(nextProps, nextState) {\n        if (this.state.inputValue !== nextState.inputValue) {\n            if (this.props.onChange) {\n                this.props.onChange(nextState.inputValue);\n            }\n        }\n    }\n\n    componentWillUnmount() {\n        if (this.timeoutId) {\n            clearTimeout(this.timeoutId);\n            this.timeoutId = null;\n        }\n    }\n\n    render() {\n        return (\n            <Input\n                className={ this.cn() }\n                ref={ (input) => {\n                    this.input = input;\n                } }\n                { ...this.props }\n                focused={ this.state.inputFocused || this.state.selectFocused }\n                leftAddons={ (\n                    <Select\n                        className={ this.cn('select') }\n                        ref={ (select) => {\n                            this.select = select;\n                        } }\n                        disabled={ this.props.disabled }\n                        mode=\"radio\"\n                        options={ this.getOptions() }\n                        popupSecondaryOffset={ this.getSelectPopupOffset() }\n                        renderButtonContent={ this.renderSelectButtonContent }\n                        size={ this.props.size }\n                        value={ [this.state.countryIso2] }\n                        onBlur={ this.handleSelectBlur }\n                        onChange={ this.handleSelectChange }\n                        onClick={ this.handleSelectClick }\n                        onFocus={ this.handleSelectFocus }\n                        onButtonFocus={ this.handleSelectButtonFocus }\n                        onButtonBlur={ this.handleSelectButtonBlur }\n                    />\n                ) }\n                noValidate={ true }\n                type=\"tel\"\n                value={ this.getValue() }\n                onBlur={ this.handleInputBlur }\n                onChange={ this.handleInputChange }\n                onFocus={ this.handleInputFocus }\n            />\n        );\n    }\n\n    renderFlagIcon(countryIso2: string) {\n        return (\n            <FlagIcon\n                country={ countryIso2 }\n                mode={ this.state.onceOpened ? 'sprite' : 'chunk' }\n                size={ this.props.size }\n            />\n        );\n    }\n\n    private renderSelectButtonContent = () => this.renderFlagIcon(this.state.countryIso2);\n\n    private handleSelectFocus = (event) => {\n        if (!this.state.onceOpened) {\n            this.setState({\n                onceOpened: true,\n            });\n        }\n\n        this.resolveFocusedState({ selectFocused: true }, event);\n    };\n\n    private handleSelectButtonFocus = () => {\n        this.setState({\n            selectFocused: true,\n        });\n    };\n\n    private handleSelectBlur = (event) => {\n        this.resolveFocusedState({ selectFocused: false }, event);\n    };\n\n    private handleSelectButtonBlur = (event) => {\n        this.resolveFocusedState({ selectFocused: false }, event);\n    };\n\n    private handleSelectChange = (value) => {\n        const inputValue = `+${this.countries.find((country) => country.iso2 === value[0]).dialCode}`;\n\n        this.setState({\n            countryIso2: value[0],\n            inputValue,\n        }, () => {\n            // Wait for select blur, then focus on input\n            this.timeoutId = setTimeout(() => {\n                this.input.focus();\n                this.input.setSelectionRange(inputValue.length);\n            }, 0);\n        });\n    };\n\n    private handleSelectClick = () => {\n        // Set focus to input on select closing by it's button toggle\n        if (this.state.selectFocused) {\n            this.input.focus();\n            this.input.setSelectionRange(-1);\n        }\n    };\n\n    private handleInputFocus = (event) => {\n        this.resolveFocusedState({ inputFocused: true }, event);\n    };\n\n    private handleInputBlur = (event) => {\n        this.resolveFocusedState({ inputFocused: false }, event);\n    };\n\n    private handleInputChange = (value) => {\n        this.setState({\n            inputValue: value.length === 1 && value !== '+' ? `+${value}` : value,\n        }, this.setCountry);\n    };\n\n    // TODO: торчит для теста\n    getOptions = () => {\n        this.countries = countries.getCountries();\n\n        return this.state.onceOpened ? this.countries.map((country) => ({\n            value: country.iso2,\n            text: (\n                <span>\n                    { country.name }\n                    <span className={ this.cn('select-option-code') }>\n                        +\n                        { country.dialCode }\n                    </span>\n                </span>\n            ),\n            nativeText: `${country.name} +${country.dialCode}`,\n            icon: (\n                <span className={ this.cn('select-option-flag') }>\n                    { this.renderFlagIcon(country.iso2) }\n                </span>\n            ),\n        })) : [];\n    };\n\n    private getSelectPopupOffset() {\n        switch (this.props.size) {\n        case 's': return -18;\n        case 'm': return -24;\n        case 'l': return -27;\n        case 'xl': return -28;\n        }\n\n        return 0;\n    }\n\n    private getValue() {\n        // Use value from state not props, cause of some formatting steps in component\n        // Sync props.value with state.inputValue in UNSAFE_componentWillUpdate\n        return this.state.inputValue;\n    }\n\n    // TODO: торчит для теста\n    loadUtil() {\n        return import(/* webpackChunkName: \"libphonenumber\" */ 'libphonenumber-js/bundle/libphonenumber-js.min')\n            .then((util) => {\n                this.util = util;\n            })\n            .catch((error) => `An error occurred while loading libphonenumber-js:\\n${error}`);\n    }\n\n    private resolveFocusedState(nextFocusedStateItem, event) {\n        const focusedState = {\n            inputFocused: this.state.inputFocused,\n            selectFocused: this.state.selectFocused,\n        };\n\n        const relatedTarget = getRelatedTarget(event);\n        const hasMatchedRelatedTarget = relatedTarget === event.target;\n        let hasSelectRelatedTarget = false;\n        let isSwitchBetweenRelatedTargers = false;\n\n        // Check classNames matching in select's button (relatedTarget) & menu (focused target)\n        if (relatedTarget.classList && event.target.classList) {\n            hasSelectRelatedTarget = Array.from(relatedTarget.classList).some((item) => /select/.test(item))\n                === Array.from(event.target.classList).some((item) => /select/.test(item));\n\n            isSwitchBetweenRelatedTargers = !Object.values(focusedState).some((item) => item)\n                && !Array.from(event.target.classList).some((item) => /select/.test(item))\n                && Array.from(relatedTarget.classList).some((item) => /select/.test(item));\n        }\n\n        if (event.type === 'focus') {\n            if (hasMatchedRelatedTarget || hasSelectRelatedTarget || isSwitchBetweenRelatedTargers) {\n                // If we have smth already focused, do not do anything\n                const alreadyInFocus = Object.values(focusedState).some((item) => item);\n\n                if (!alreadyInFocus) {\n                    this.setState(nextFocusedStateItem);\n\n                    if (this.props.onFocus) {\n                        this.props.onFocus(event);\n                    }\n                }\n            }\n        }\n\n        if (event.type === 'blur') {\n            if (!hasMatchedRelatedTarget) {\n                // Set all values in focusedState to false cause we are blurring now\n                this.setState(\n                    Object.keys(focusedState).reduce((result, item) => {\n                        result[item] = false;\n\n                        return result;\n                    }, {}),\n                );\n\n                if (this.props.onBlur) {\n                    this.props.onBlur(event);\n                }\n            }\n        }\n    }\n\n    private setCountry() {\n        const inputValue = this.getValue().replace(/ /g, '');\n\n        for (let i = 0; i < this.countries.length; i++) {\n            const country = this.countries[i];\n\n            if (new RegExp(`^\\\\+${country.dialCode}`).test(inputValue)) {\n                // Handle countries with priority field\n                if (country.priority === undefined) {\n                    this.setValue(country.iso2, inputValue);\n                    break;\n                }\n\n                // Check max dial code length to allow country change\n                // For countries with identical dial codes or North American Numbering Plan (NANP)\n                if (inputValue.length < MAX_DIAL_CODE_LENGTH) {\n                    // Update only value if countries are equal\n                    if (this.state.countryIso2 === country.iso2) {\n                        this.setValue(country.iso2, inputValue);\n                        break;\n                    // If not equal — set highest by priority\n                    } else if (country.priority === 0) {\n                        this.setValue(country.iso2, inputValue);\n                        break;\n                    }\n                // Otherwise don't change already selected country, just set value\n                } else if (this.state.countryIso2 === country.iso2) {\n                    this.setValue(country.iso2, inputValue);\n                    break;\n                }\n            }\n        }\n    }\n\n    private setValue(countryIso2, inputValue) {\n        this.asYouType = this.util ? new this.util.AsYouType(countryIso2.toUpperCase()) : null;\n        this.setState({\n            inputValue: this.asYouType ? this.asYouType.input(inputValue) : inputValue,\n            countryIso2,\n        });\n    }\n\n    /**\n     * Возвращает ссылку на HTMLElement инпута.\n     */\n    public getControl(): HTMLInputElement {\n        return this.input.getControl();\n    }\n\n    /**\n     * Устанавливает фокус на поле ввода.\n     */\n    public focus() {\n        this.input.focus();\n    }\n\n    /**\n     * Убирает фокус с поля ввода.\n     */\n    public blur() {\n        this.input.blur();\n    }\n\n    /**\n     * Скроллит страницу до поля ввода.\n     */\n    public scrollTo() {\n        this.input.scrollTo();\n    }\n}\n\nexport default withTheme<IntlPhoneInputProps, IntlPhoneInput>(IntlPhoneInput);\n"]}