'use client'
import { jsxs, jsx } from 'react/jsx-runtime';
import { useRef, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { KycFieldType, CustomFieldType } from '@dynamic-labs/sdk-api-core';
import { classNames } from '../../utils/functions/classNames/classNames.js';
import '../Accordion/components/AccordionItem/AccordionItem.js';
import { ReactComponent as SvgAdd } from '../../shared/assets/add.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 { getValueByKey } from '../../shared/utils/functions/getValueByKey/index.js';
import '@dynamic-labs/utils';
import '../../utils/constants/colors.js';
import '../../utils/constants/values.js';
import { NAME_SERVICE_FIELD_NAME } from '../../utils/constants/fields.js';
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 { useErrorContext } from '../../context/ErrorContext/ErrorContext.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import { getEnabledOAuthProviders } from '../../utils/functions/getEnabledOAuthProviders/index.js';
import { getUserProfileFields } from '../../utils/functions/getUserProfileFields/getUserProfileFields.js';
import '../../store/state/nonce/nonce.js';
import { isSocialKycEnabled } from '../../utils/functions/isSocialKycEnabled/isSocialKycEnabled.js';
import { isAllowedToLinkWallet } from '../../utils/functions/isAllowedToLinkWallet/isAllowedToLinkWallet.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 { useInternalUserWallets } from '../../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 { useFields } from '../../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 '../../widgets/DynamicWidget/context/DynamicWidgetContext.js';
import { ErrorContainer } from '../ErrorContainer/ErrorContainer.js';
import '../IconButton/IconButton.js';
import { TypographyButton } from '../TypographyButton/TypographyButton.js';
import { getWalletVerifiedCredential } from '../../utils/functions/getWalletVerifiedCredential/getWalletVerifiedCredential.js';
import { Wallet } from '../../widgets/DynamicWidget/components/Wallet/Wallet.js';
import { UserProfileField } from './parts/UserProfileField/UserProfileField.js';
import { UserProfileSection } from './parts/UserProfileSection/UserProfileSection.js';
import { UserProfileSocialAccount } from './parts/UserProfileSocialAccount/UserProfileSocialAccount.js';
import { EditProfileButton } from './parts/EditProfileButton/EditProfileButton.js';
import 'formik';
import '../../utils/hooks/useSubdomainCheck/useSubdomainCheck.js';
import '../../store/state/sendBalances.js';
import '../Input/Input.js';
import '../OverlayCard/OverlayCard.js';
import '../MenuList/Dropdown/Dropdown.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 { useDynamicModals } from '../../utils/hooks/useDynamicModals/useDynamicModals.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 '../Popper/Popper/Popper.js';
import '../Popper/PopperContext/PopperContext.js';

const UserProfile = ({ rootClassName }) => {
    var _a;
    const { projectSettings, user, setSelectedWalletConnectorKey, multiWallet, primaryWallet, } = useInternalDynamicContext();
    const { error } = useErrorContext();
    const { setShowLinkNewWalletModal } = useDynamicModals();
    const { userWallets } = useInternalUserWallets();
    const { t } = useTranslation();
    const { fieldsConfig } = useFields();
    const containerRef = useRef(null);
    useEffect(() => {
        if (error && containerRef.current) {
            containerRef.current.scrollTop = 0;
        }
    }, [error]);
    const allUserFields = getUserProfileFields({
        customFields: projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.customFields,
        projectSettingsKyc: projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.kyc,
        user,
    });
    const ensDomain = useMemo(() => { var _a; return (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.nameService) === null || _a === void 0 ? void 0 : _a.evm.domain; }, [projectSettings]);
    const showEnsField = useMemo(() => (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector.isEmbeddedWallet) && ensDomain, [primaryWallet, ensDomain]);
    const providers = isSocialKycEnabled(projectSettings)
        ? getEnabledOAuthProviders(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers)
        : [];
    const canLink = isAllowedToLinkWallet(projectSettings, user === null || user === void 0 ? void 0 : user.verifiedCredentials, multiWallet);
    const addNewWallet = () => {
        setShowLinkNewWalletModal(true);
        setSelectedWalletConnectorKey(null);
    };
    const mapFieldKeyToValue = (fieldKey) => user === null || user === void 0 ? void 0 : user[fieldKey];
    if (!user)
        return null;
    // if it's multiwallet, the wallet should appear in the wallets tab
    // if there's a primary wallet, it will be displayed in the header
    const wallet = !multiWallet && !primaryWallet && userWallets[0];
    return (jsxs("div", { "data-testid": 'userProfile', className: classNames('user-profile', rootClassName), ref: containerRef, children: [error && jsx(ErrorContainer, { children: error }), wallet && (jsx(UserProfileSection, { title: t('dyn_user_profile.my_wallet.title'), children: jsx(Wallet, { ens: (_a = getWalletVerifiedCredential(wallet.address, user === null || user === void 0 ? void 0 : user.verifiedCredentials, wallet.chain)) === null || _a === void 0 ? void 0 : _a.nameService, wallet: wallet }, wallet.id) })), (allUserFields === null || allUserFields === void 0 ? void 0 : allUserFields.length) > 0 && (jsx(UserProfileSection, { title: t('dyn_user_profile.my_information.title'), action: jsx(EditProfileButton, {}), children: jsx("div", { className: 'user-profile__fields', children: allUserFields.map(({ name, validationType, type, label }) => {
                        var _a, _b;
                        if (type === KycFieldType.Custom) {
                            const value = (_b = (_a = user.metadata) === null || _a === void 0 ? void 0 : _a[name]) !== null && _b !== void 0 ? _b : '';
                            let fieldValue = value;
                            if (validationType === CustomFieldType.Checkbox)
                                fieldValue = value.length > 0 ? 'Checked' : 'Unchecked';
                            // Use name instead of label here because the back-end doesn't set label
                            let fieldLabel = name;
                            if (name === NAME_SERVICE_FIELD_NAME) {
                                if (!showEnsField) {
                                    return null;
                                }
                                fieldLabel = t('dyn_subdomain_field.label');
                                if (fieldValue !== '') {
                                    fieldValue = `${fieldValue}.${ensDomain}`;
                                }
                                else {
                                    fieldValue = '';
                                }
                            }
                            return (jsx(UserProfileField, { name: name, label: fieldLabel, value: fieldValue }, name));
                        }
                        const { key } = getValueByKey(fieldsConfig, name);
                        const value = mapFieldKeyToValue(name);
                        return (jsx(UserProfileField, { copykey: key, name: name, label: t(`${key}`), value: value }, name));
                    }) }) })), canLink && !multiWallet && (jsx(UserProfileSection, { title: t('dyn_user_profile.wallets.title'), children: jsx(TypographyButton, { dataTestId: 'link-wallet-button', onClick: addNewWallet, startSlot: jsx(SvgAdd, {}), showInternalLoading: false, children: jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_user_profile.wallets.link_wallet_button', children: t('dyn_user_profile.wallets.link_wallet_button') }) }) })), (providers === null || providers === void 0 ? void 0 : providers.length) > 0 && (jsx(UserProfileSection, { title: t('dyn_user_profile.social_accounts.title'), children: jsx("div", { className: 'user-profile__social-accounts', children: providers.map(({ provider }) => (jsx(UserProfileSocialAccount, { provider: provider }, provider))) }) }))] }));
};

export { UserProfile };
