'use client'
import { __awaiter } from '../../../../../../_virtual/_tslib.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import '@dynamic-labs/utils';
import '../../../../components/Accordion/components/AccordionItem/AccordionItem.js';
import { ReactComponent as SvgAdd } from '../../../../shared/assets/add.js';
import { ReactComponent as SvgChevronLeft } from '../../../../shared/assets/chevron-left.js';
import '@dynamic-labs/iconic';
import { useViewContext } from '../../../../context/ViewContext/ViewContext.js';
import '@dynamic-labs/wallet-connector-core';
import '../../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../../../utils/constants/colors.js';
import '../../../../utils/constants/values.js';
import '@dynamic-labs/sdk-api-core';
import '../../../../shared/consts/index.js';
import '../../../../components/Alert/Alert.js';
import { useDynamicEvents } from '../../../../utils/hooks/events/useDynamicEvents/useDynamicEvents.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 'react-international-phone';
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 '../../../../events/dynamicEvents.js';
import { useProjectSettings } from '../../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.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 '../../../../components/Typography/Typography.js';
import '../../../../context/FooterAnimationContext/index.js';
import '../../../../components/ShadowDOM/ShadowDOM.js';
import '../../../../components/Transition/ZoomTransition/ZoomTransition.js';
import '../../../../components/Transition/SlideInUpTransition/SlideInUpTransition.js';
import '../../../../components/Transition/OpacityTransition/OpacityTransition.js';
import '../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.js';
import '../../../../context/WalletGroupContext/WalletGroupContext.js';
import '../../components/DynamicWidgetHeader/DynamicWidgetHeader.js';
import 'react-focus-lock';
import { useWidgetContext } from '../../context/DynamicWidgetContext.js';
import { IconButton } from '../../../../components/IconButton/IconButton.js';
import { ModalHeader } from '../../../../components/ModalHeader/ModalHeader.js';
import { TypographyButton } from '../../../../components/TypographyButton/TypographyButton.js';
import '../../../../components/MenuList/Dropdown/Dropdown.js';
import 'formik';
import '../../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.js';
import '../../../../store/state/sendBalances.js';
import '../../../../components/Input/Input.js';
import '../../../../components/OverlayCard/OverlayCard.js';
import { useEffectOnce } from '../../../../utils/hooks/useEffectOnce/useEffectOnce.js';
import '../../../../views/TransactionConfirmationView/TransactionConfirmationView.js';
import '../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.js';
import '../../../../context/PasskeyContext/PasskeyContext.js';
import '../../components/PasskeyCard/PasskeyCard.js';
import '../CryptoComOnramp/CryptoComOnramp.js';
import '../../../../context/OnrampContext/OnrampContext.js';
import '../../../../../index.js';
import '../../helpers/convertExchangeKeyAndProviderEnum.js';
import 'qrcode';
import { MfaDeviceTileSkeleton } from '../../../../components/MfaDeviceTileSkeleton/MfaDeviceTileSkeleton.js';
import { UserDeviceTile } from './components/UserDeviceTile.js';
import { sortDevices } from './components/utils/sort.js';
import '../ReceiveWalletFunds/ReceiveWalletFunds.js';
import '../../../../context/IpConfigurationContext/IpConfigurationContext.js';
import '../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.js';
import '../../../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 { useMfa } from '../../../../utils/hooks/useMfa/useMfa.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 '../../../../components/InlineWidget/InlineWidget.js';
import '../../../../components/IsBrowser/IsBrowser.js';
import '../../../../components/Popper/Popper/Popper.js';
import '../../../../components/Popper/PopperContext/PopperContext.js';

const ManageTotpMfaWidgetView = () => {
    var _a;
    const [userDevices, setUserDevices] = useState([]);
    const [mfaMethod, setMfaMethod] = useState();
    const [loading, setLoading] = useState(false);
    const { setDynamicWidgetView, dynamicWidgetView } = useWidgetContext();
    const { setShowMfaQRCode, setShowOTPVerification } = useDynamicModals();
    const { clearStackAndPush } = useViewContext();
    const { getUserDevices, deleteUserDevice } = useMfa();
    const { t } = useTranslation();
    const { setShowAuthFlow } = useInternalDynamicContext();
    const projectSettings = useProjectSettings();
    const isMfaRequired = Boolean((_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.security.mfa) === null || _a === void 0 ? void 0 : _a.required);
    const getDevices = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
        setLoading(true);
        const devices = yield getUserDevices();
        // set the user devices with the default device first
        setUserDevices(sortDevices(devices));
        setLoading(false);
    }), [getUserDevices]);
    // re-fetch devices when a new device is added
    useDynamicEvents('mfaCompletionSuccess', (_b) => __awaiter(void 0, [_b], void 0, function* ({ mfaToken }) {
        if (mfaToken) {
            // we now require an MFA token to delete a device which is an async operation
            if ((mfaMethod === null || mfaMethod === void 0 ? void 0 : mfaMethod.action) === 'remove') {
                yield deleteUserDevice(mfaMethod.params.deviceId, mfaToken);
                setMfaMethod(undefined);
            }
        }
        getDevices();
    }));
    // delete device and then re-fetch devices
    const deleteDevice = (id) => __awaiter(void 0, void 0, void 0, function* () {
        setMfaMethod({ action: 'remove', params: { deviceId: id } });
        setShowOTPVerification(true);
    });
    // load devices on initial page load
    useEffectOnce(() => {
        getDevices();
    });
    const backButton = dynamicWidgetView === 'manage-totp-mfa' ? (jsx(IconButton, { type: 'button', onClick: () => setDynamicWidgetView('account-and-security-settings'), "data-testid": 'back-button', children: jsx(SvgChevronLeft, {}) })) : undefined;
    const handleGenerateBackupCodes = () => __awaiter(void 0, void 0, void 0, function* () {
        setShowAuthFlow(true);
        clearStackAndPush('mfa-display-backup-codes', { regenerateCodes: true });
    });
    return (jsxs("div", { className: 'manage-totp-mfa-widget-view', children: [jsx(ModalHeader, { leading: backButton, children: jsx("div", { className: 'send-balance-page-layout__header-content', children: jsx(Typography, { variant: 'title', color: 'primary', copykey: 'dyn_manage_mfa.title', children: t('dyn_manage_mfa.title') }) }) }), jsxs("div", { className: 'manage-totp-mfa-widget-view__scroll-container', children: [loading && jsx(MfaDeviceTileSkeleton, {}), !loading && userDevices.length === 0 && (jsx(Typography, { className: 'manage-totp-mfa-widget-view__no-devices', variant: 'body_normal', color: 'secondary', copykey: 'dyn_manage_mfa.no_devices', children: t('dyn_manage_mfa.no_devices') })), userDevices.map((device, index) => (jsx(UserDeviceTile, { isMfaRequired: isMfaRequired, userDevice: device, index: index, deleteDevice: deleteDevice }, device.id)))] }), jsx("div", { className: 'manage-totp-mfa-widget-view__add-mfa-button-container', children: userDevices.length > 0 ? (jsx(TypographyButton, { buttonClassName: 'manage-totp-mfa-widget-view__add-mfa-button-container__button', onClick: handleGenerateBackupCodes, copykey: 'dyn_manage_mfa.generate_backup_codes_button', children: t('dyn_manage_mfa.generate_backup_codes_button') })) : (jsx(TypographyButton, { buttonClassName: 'manage-totp-mfa-widget-view__add-mfa-button-container__button', onClick: () => setShowMfaQRCode(true), copykey: 'dyn_manage_mfa.add_mfa_button', startSlot: jsx(SvgAdd, {}), children: t('dyn_manage_mfa.add_mfa_button') })) })] }));
};

export { ManageTotpMfaWidgetView };
