'use client'
'use strict';

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

var React = require('react');
var reactI18next = require('react-i18next');
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
require('../../../context/DynamicContext/DynamicContext.cjs');
require('../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../../utils/constants/colors.cjs');
require('../../../utils/constants/values.cjs');
require('../../../shared/consts/index.cjs');
require('../../../events/dynamicEvents.cjs');
require('../../../../../_virtual/_tslib.cjs');
require('../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../context/ErrorContext/ErrorContext.cjs');
require('@dynamic-labs/multi-wallet');
var isPhoneNumberProviderEnabled = require('../../../utils/functions/isPhoneNumberProviderEnabled/isPhoneNumberProviderEnabled.cjs');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
var isEmailProviderEnabled = require('../../../utils/functions/isEmailProviderEnabled/isEmailProviderEnabled.cjs');
require('@dynamic-labs-sdk/client/core');
require('../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
var useProjectSettings = require('../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
var isPasskeyProviderEnabled = require('../../../utils/functions/isPasskeyProviderEnabled/isPasskeyProviderEnabled.cjs');
require('../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../../store/state/authMode/authMode.cjs');
require('../../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../utils/functions/compareChains/compareChains.cjs');
require('../../Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../context/ThemeContext/ThemeContext.cjs');
require('../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
require('@dynamic-labs/types');
var SocialRedirectContext = 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('../../CollectUserDataView/useFields.cjs');
require('../../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../../store/state/walletOptions/walletOptions.cjs');
require('../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('../../../components/Alert/Alert.cjs');
require('../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../components/IconButton/IconButton.cjs');
require('../../../components/InlineWidget/InlineWidget.cjs');
require('../../../components/Input/Input.cjs');
require('../../../components/IsBrowser/IsBrowser.cjs');
require('../../../components/MenuList/Dropdown/Dropdown.cjs');
require('../../../components/OverlayCard/OverlayCard.cjs');
require('../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../components/Popper/Popper/Popper.cjs');
require('../../../components/Popper/PopperContext/PopperContext.cjs');
require('react-focus-lock');
require('qrcode');
require('formik');
require('../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
var useWalletList = require('../../../utils/hooks/useWalletList/useWalletList.cjs');
require('../../../context/WalletGroupContext/WalletGroupContext.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('../../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../context/FooterAnimationContext/index.cjs');
var getLoginViewStructure = require('./getLoginViewStructure/getLoginViewStructure.cjs');
require('../../MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../../../store/state/sendBalances.cjs');
require('../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../../index.cjs');
require('../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
var useViewSettings = require('../../../utils/hooks/useViewSettings/useViewSettings.cjs');
require('../../../store/state/tokenBalances.cjs');
require('../../../store/state/multichainBalances.cjs');
require('../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
var useInternalDynamicContext = require('../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');

/* eslint-disable multiline-comment-style */
const NUMBER_OF_WALLETS = 3;
const useLoginView = () => {
    const loginView = useViewSettings.useViewSettings(sdkApiCore.SdkViewType.Login);
    const { authorizationViewDisplayOrder, loginWithEmail, walletsFilter } = useInternalDynamicContext.useInternalDynamicContext();
    const projectSettings = useProjectSettings.useProjectSettings();
    const { t } = reactI18next.useTranslation();
    const { enabledSocialProviders } = SocialRedirectContext.useSocialRedirectContext();
    const { numberOfWallets } = useWalletList.useWalletList({
        viewWalletsFilter: walletsFilter,
    });
    const hasSocialProvidersEnabled = Boolean(enabledSocialProviders === null || enabledSocialProviders === void 0 ? void 0 : enabledSocialProviders.length);
    const hasEmailProviderEnabled = isEmailProviderEnabled.isEmailProviderEnabled(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers);
    const hasPhoneProviderEnabled = isPhoneNumberProviderEnabled.isPhoneNumberProviderEnabled(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers);
    const hasPasskeyProviderEnabled = isPasskeyProviderEnabled.isPasskeyProviderEnabled(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers);
    const isWalletOnly = React.useMemo(() => {
        var _a;
        if (!projectSettings) {
            return false;
        }
        // When no other methods are enabled we can safely assume it's wallet only
        if (!loginWithEmail &&
            !hasPhoneProviderEnabled &&
            !hasSocialProvidersEnabled) {
            return true;
        }
        // backwards compatibility for those that didn't set up login view sections
        if (!((_a = loginView === null || loginView === void 0 ? void 0 : loginView.sections) === null || _a === void 0 ? void 0 : _a.length)) {
            return (!hasEmailProviderEnabled &&
                !hasPhoneProviderEnabled &&
                !hasSocialProvidersEnabled);
        }
        return (loginView.sections.length === 1 && loginView.sections[0].type === 'wallet');
    }, [
        loginView === null || loginView === void 0 ? void 0 : loginView.sections,
        projectSettings,
        loginWithEmail,
        hasSocialProvidersEnabled,
        hasPhoneProviderEnabled,
        hasEmailProviderEnabled,
    ]);
    /**
     * remove wallet view section from projectSettings?.sdk?.views if no wallet defined
     * @param sections
     * @returns
     */
    const filterLoginViewSections = React.useCallback((sections) => {
        var _a;
        const hasEnabledNetwork = (chain) => { var _a; return (_a = chain.networks) === null || _a === void 0 ? void 0 : _a.some((network) => network.enabled); };
        const hasEnabledChain = (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.chains) === null || _a === void 0 ? void 0 : _a.some((chain) => chain.enabled && hasEnabledNetwork(chain));
        if (projectSettings &&
            !hasEnabledChain &&
            sections.find((section) => section.type === 'wallet')) {
            logger.logger.error('Failed to render WalletSignInSection - no chains enabled');
        }
        if (numberOfWallets > 0) {
            return sections;
        }
        return sections.filter((section) => section.type !== 'wallet');
    }, [numberOfWallets, projectSettings]);
    /**
     * returns the login view sections to be displayed in order from projectSettings?.sdk?.views
     * and has backwards compatibility with project settings design.modal props
     */
    const loginViewSections = React.useMemo(() => {
        var _a, _b, _c, _d, _e, _f;
        // If client provides the sections explicitly, we use their definitions
        if ((_a = loginView === null || loginView === void 0 ? void 0 : loginView.sections) === null || _a === void 0 ? void 0 : _a.length) {
            return filterLoginViewSections(loginView === null || loginView === void 0 ? void 0 : loginView.sections);
        }
        // If not, we build it using the default structure
        const { splitEmailAndSocial = false, socialAboveEmail = false } = (_b = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.design.modal) !== null && _b !== void 0 ? _b : {};
        const sections = [];
        const sectionRecipes = getLoginViewStructure.getLoginViewStructure({
            defaultSocialItem: enabledSocialProviders[0],
            numOfWalletsToDisplay: ((_d = (_c = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.design) === null || _c === void 0 ? void 0 : _c.modal) === null || _d === void 0 ? void 0 : _d.showWalletsButton)
                ? 0
                : NUMBER_OF_WALLETS,
            showEmail: hasEmailProviderEnabled,
            showPasskey: hasPasskeyProviderEnabled,
            showPhone: hasPhoneProviderEnabled,
            showSocial: Boolean(enabledSocialProviders === null || enabledSocialProviders === void 0 ? void 0 : enabledSocialProviders.length),
            // is email only is set in the project settings when the "Email (Web2) only" is toggled on in the dashboard
            showWallets: numberOfWallets > 0 && !((_f = (_e = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.design) === null || _e === void 0 ? void 0 : _e.modal) === null || _f === void 0 ? void 0 : _f.emailOnly),
            socialAboveEmail,
            splitEmailAndSocial,
            web3Layout: authorizationViewDisplayOrder === 'web3',
        });
        const separator = {
            label: t('dyn_login.separators.default'),
            type: sdkApiCore.SdkViewSectionType.Separator,
        };
        let skipSeparator = false;
        for (const recipe of sectionRecipes) {
            if (!recipe.condition)
                continue;
            // First we add the separator
            if (sections.length > 0 &&
                !skipSeparator &&
                recipe.hideSeparator !== 'above')
                sections.push(separator);
            sections.push(recipe.section);
            skipSeparator = recipe.hideSeparator === 'below';
        }
        return sections;
    }, [
        loginView === null || loginView === void 0 ? void 0 : loginView.sections,
        projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.design.modal,
        enabledSocialProviders,
        hasEmailProviderEnabled,
        hasPasskeyProviderEnabled,
        hasPhoneProviderEnabled,
        numberOfWallets,
        authorizationViewDisplayOrder,
        t,
        filterLoginViewSections,
    ]);
    return {
        isWalletOnly,
        loginViewSections,
    };
};

exports.NUMBER_OF_WALLETS = NUMBER_OF_WALLETS;
exports.useLoginView = useLoginView;
