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

const PhoneNumberField = ({ isLoading, disabled, error, label, onUpdate, appendRight, setIsFocused, setIsHovered, initialPhone, onBlur, countries, }) => {
    const { t } = useTranslation();
    const countryInputRef = useRef(null);
    const [countryDropdownOpen, setCountryDropdownOpen] = useState(false);
    const anchorRef = useRef(null);
    const { defaultPhoneInputIso2 } = useInternalDynamicContext();
    const [invalidNumberMessage, setInvalidNumberMessage] = useState(undefined);
    const { formattedPhone, currentCountry, handleCountryInput, handlePhoneInput, hasInput, inputRef, countryInput, onBlurCountry, filteredCountries, openDropdown, handleCountryKeyDown, closeDropdown, setCountry, onMenuDisplayToggle, onFocusCountry, countryFontSize, } = usePhoneInputLogic({
        countries,
        countryInputRef,
        defaultIso2: defaultPhoneInputIso2,
        initialPhone,
        onUpdate,
        setCountryDropdownOpen,
        setInvalidNumberMessage,
    });
    return (jsxs("div", { className: 'phone-number-field', children: [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: [jsxs("div", { className: classNames('phone-number-field__fields__country-code', {
                            'phone-number-field__fields__country-code--disabled': isLoading || disabled,
                        }), onClick: openDropdown, "data-testid": 'country-code', children: [jsx(FlagImage, { iso2: currentCountry.iso2, size: '24px', className: 'phone-number-field__fields__country-code__flag' }), jsx(Typography, { className: 'phone-number-field__fields__country-code__plus', variant: 'body_normal', weight: 'regular', style: { fontSize: countryFontSize }, color: 'primary', children: "+" }), jsx(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' }), jsx(Icon, { className: 'phone-number-field__fields__country-code__chevron', children: jsx(SvgChevronDown, {}) })] }), jsx(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 && (jsx(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] }), jsx(MenuList, { isOpen: countryDropdownOpen, onClickClose: closeDropdown, popperProps: {
                    anchorOrigin: 'bottom-left',
                    anchorRef,
                }, mobileTitle: 'Select country', dropdownClassName: 'phone-number-field__dropdown', children: jsx("div", { className: 'phone-number-field__dropdown__items', ref: onMenuDisplayToggle, children: filteredCountries.map((countryData) => {
                        const country = parseCountry(countryData);
                        const { iso2, name, dialCode } = country;
                        const isSelected = iso2 === currentCountry.iso2;
                        return (jsxs("div", { className: classNames('phone-number-field__dropdown__items__country', {
                                'phone-number-field__dropdown__items__country--selected': isSelected,
                            }), onClick: () => {
                                setCountry(country);
                                closeDropdown();
                            }, id: iso2, children: [jsx(FlagImage, { iso2: iso2, size: '24px' }), jsx(Typography, { variant: 'body_small', color: 'primary', children: name }), jsx(Typography, { variant: 'body_small', color: 'secondary', children: dialCode })] }, iso2));
                    }) }) }), Boolean(invalidNumberMessage || error) && (jsx(Typography, { variant: 'body_mini', className: 'phone-number-field__error-message', children: error !== null && error !== void 0 ? error : invalidNumberMessage }))] }));
};

export { PhoneNumberField };
