'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactInternationalPhone = require('react-international-phone');
var reactI18next = require('react-i18next');
var classNames = require('../../utils/functions/classNames/classNames.cjs');
require('../Accordion/components/AccordionItem/AccordionItem.cjs');
var chevronDown = require('../../shared/assets/chevron-down.cjs');
require('@dynamic-labs/iconic');
require('../../context/ViewContext/ViewContext.cjs');
require('../../../../_virtual/_tslib.cjs');
require('@dynamic-labs/wallet-connector-core');
require('../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../utils/constants/colors.cjs');
require('../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../shared/consts/index.cjs');
require('../Alert/Alert.cjs');
require('../../events/dynamicEvents.cjs');
require('../../context/DynamicContext/DynamicContext.cjs');
require('../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../store/state/authMode/authMode.cjs');
var useInternalDynamicContext = require('../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
require('../../context/CaptchaContext/CaptchaContext.cjs');
require('../../context/ErrorContext/ErrorContext.cjs');
require('@dynamic-labs/multi-wallet');
require('../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-sdk/client/core');
require('../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../config/ApiEndpoint.cjs');
require('@dynamic-labs/locale');
require('../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../utils/functions/compareChains/compareChains.cjs');
require('../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../context/ThemeContext/ThemeContext.cjs');
require('../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../context/LoadingContext/LoadingContext.cjs');
require('../../context/WalletContext/WalletContext.cjs');
require('../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../context/MockContext/MockContext.cjs');
require('../../views/CollectUserDataView/useFields.cjs');
require('../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../store/state/walletOptions/walletOptions.cjs');
var Typography = require('../Typography/Typography.cjs');
require('../../context/FooterAnimationContext/index.cjs');
require('../ShadowDOM/ShadowDOM.cjs');
require('../Transition/ZoomTransition/ZoomTransition.cjs');
require('../Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../Transition/OpacityTransition/OpacityTransition.cjs');
require('../OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('react-focus-lock');
var Icon = require('../Icon/Icon.cjs');
require('../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../IconButton/IconButton.cjs');
require('../MenuList/Dropdown/Dropdown.cjs');
require('formik');
require('../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../store/state/sendBalances.cjs');
var Input = require('../Input/Input.cjs');
var MenuList = require('../MenuList/MenuList/MenuList.cjs');
require('../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../context/PasskeyContext/PasskeyContext.cjs');
require('../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../context/OnrampContext/OnrampContext.cjs');
require('../../../index.cjs');
require('../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('qrcode');
require('../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../widgets/DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../store/state/tokenBalances.cjs');
require('../../store/state/multichainBalances.cjs');
require('../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
require('../InlineWidget/InlineWidget.cjs');
require('../IsBrowser/IsBrowser.cjs');
require('../OverlayCard/OverlayCard.cjs');
var usePhoneInputLogic = require('./usePhoneInputLogic/usePhoneInputLogic.cjs');
require('../Popper/Popper/Popper.cjs');
require('../Popper/PopperContext/PopperContext.cjs');

const PhoneNumberField = ({ isLoading, disabled, error, label, onUpdate, appendRight, setIsFocused, setIsHovered, initialPhone, onBlur, countries, }) => {
    const { t } = reactI18next.useTranslation();
    const countryInputRef = React.useRef(null);
    const [countryDropdownOpen, setCountryDropdownOpen] = React.useState(false);
    const anchorRef = React.useRef(null);
    const { defaultPhoneInputIso2 } = useInternalDynamicContext.useInternalDynamicContext();
    const [invalidNumberMessage, setInvalidNumberMessage] = React.useState(undefined);
    const { formattedPhone, currentCountry, handleCountryInput, handlePhoneInput, hasInput, inputRef, countryInput, onBlurCountry, filteredCountries, openDropdown, handleCountryKeyDown, closeDropdown, setCountry, onMenuDisplayToggle, onFocusCountry, countryFontSize, } = usePhoneInputLogic.usePhoneInputLogic({
        countries,
        countryInputRef,
        defaultIso2: defaultPhoneInputIso2,
        initialPhone,
        onUpdate,
        setCountryDropdownOpen,
        setInvalidNumberMessage,
    });
    return (jsxRuntime.jsxs("div", { className: 'phone-number-field', children: [jsxRuntime.jsxs("div", { className: 'phone-number-field__fields', onPointerEnter: () => setIsHovered === null || setIsHovered === void 0 ? void 0 : setIsHovered(true), onPointerLeave: () => setIsHovered === null || setIsHovered === void 0 ? void 0 : setIsHovered(false), onFocus: () => setIsFocused === null || setIsFocused === void 0 ? void 0 : setIsFocused(true), onBlur: (event) => {
                    setIsFocused === null || setIsFocused === void 0 ? void 0 : setIsFocused(false);
                    onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
                }, ref: anchorRef, children: [jsxRuntime.jsxs("div", { className: classNames.classNames('phone-number-field__fields__country-code', {
                            'phone-number-field__fields__country-code--disabled': isLoading || disabled,
                        }), onClick: openDropdown, "data-testid": 'country-code', children: [jsxRuntime.jsx(reactInternationalPhone.FlagImage, { iso2: currentCountry.iso2, size: '24px', className: 'phone-number-field__fields__country-code__flag' }), jsxRuntime.jsx(Typography.Typography, { className: 'phone-number-field__fields__country-code__plus', variant: 'body_normal', weight: 'regular', style: { fontSize: countryFontSize }, color: 'primary', children: "+" }), jsxRuntime.jsx(Input.Input, { label: '', id: 'country_code_field', value: countryInput, onChange: handleCountryInput, disabled: isLoading || disabled, containerClassName: 'phone-number-field__fields__country-code__container', className: 'phone-number-field__fields__country-code__container__input', onBlur: onBlurCountry, ref: countryInputRef, onKeyDown: handleCountryKeyDown, onFocus: onFocusCountry, style: { fontSize: countryFontSize }, "data-testid": 'country-code-input', inputMode: 'numeric', autoComplete: 'tel-country-code' }), jsxRuntime.jsx(Icon.Icon, { className: 'phone-number-field__fields__country-code__chevron', children: jsxRuntime.jsx(chevronDown.ReactComponent, {}) })] }), jsxRuntime.jsx(Input.Input, { label: '', id: 'phoneNumber', value: formattedPhone, onChange: handlePhoneInput, disabled: isLoading || disabled, containerClassName: 'phone-number-field__fields__phone-number', className: 'phone-number-field__fields__phone-number__input', ref: inputRef, error: Boolean(invalidNumberMessage || error), inputMode: 'numeric', "data-testid": 'phone_number_input', autoComplete: 'tel-national' }), !hasInput && (jsxRuntime.jsx(Typography.Typography, { className: 'phone-number-field__fields__placeholder', variant: 'body_normal', weight: 'regular', copykey: 'dyn_collect_user_data.fields.phone_number.label', children: label !== null && label !== void 0 ? label : t('dyn_collect_user_data.fields.phone_number.label') })), appendRight] }), jsxRuntime.jsx(MenuList.MenuList, { isOpen: countryDropdownOpen, onClickClose: closeDropdown, popperProps: {
                    anchorOrigin: 'bottom-left',
                    anchorRef,
                }, mobileTitle: 'Select country', dropdownClassName: 'phone-number-field__dropdown', children: jsxRuntime.jsx("div", { className: 'phone-number-field__dropdown__items', ref: onMenuDisplayToggle, children: filteredCountries.map((countryData) => {
                        const country = reactInternationalPhone.parseCountry(countryData);
                        const { iso2, name, dialCode } = country;
                        const isSelected = iso2 === currentCountry.iso2;
                        return (jsxRuntime.jsxs("div", { className: classNames.classNames('phone-number-field__dropdown__items__country', {
                                'phone-number-field__dropdown__items__country--selected': isSelected,
                            }), onClick: () => {
                                setCountry(country);
                                closeDropdown();
                            }, id: iso2, children: [jsxRuntime.jsx(reactInternationalPhone.FlagImage, { iso2: iso2, size: '24px' }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', children: name }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'secondary', children: dialCode })] }, iso2));
                    }) }) }), Boolean(invalidNumberMessage || error) && (jsxRuntime.jsx(Typography.Typography, { variant: 'body_mini', className: 'phone-number-field__error-message', children: error !== null && error !== void 0 ? error : invalidNumberMessage }))] }));
};

exports.PhoneNumberField = PhoneNumberField;
