'use client'
'use strict';

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

var _tslib = require('../../../../../../_virtual/_tslib.cjs');
var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactI18next = require('react-i18next');
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
require('@dynamic-labs-sdk/client/core');
require('../../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
var check = require('../../../../shared/assets/check.cjs');
var close = require('../../../../shared/assets/close.cjs');
var questionMark = require('../../../../shared/assets/question-mark.cjs');
require('../../../../context/ViewContext/ViewContext.cjs');
var backupWaas = require('../../../../shared/assets/backup-waas.cjs');
var googleDrive = require('../../../../shared/assets/google-drive.cjs');
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('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs/locale');
require('../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../../events/dynamicEvents.cjs');
var useUser = require('../../../../client/extension/user/useUser/useUser.cjs');
var ErrorContainer = require('../../../../components/ErrorContainer/ErrorContainer.cjs');
var Icon = require('../../../../components/Icon/Icon.cjs');
var IconButton = require('../../../../components/IconButton/IconButton.cjs');
var ModalHeader = require('../../../../components/ModalHeader/ModalHeader.cjs');
var Typography = require('../../../../components/Typography/Typography.cjs');
var TypographyButton = require('../../../../components/TypographyButton/TypographyButton.cjs');
require('../../../../context/DynamicContext/DynamicContext.cjs');
require('../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../../context/ErrorContext/ErrorContext.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('../../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../../context/ThemeContext/ThemeContext.cjs');
require('../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
var utils = require('../../../../utils/hooks/useTransactionSimulation/utils/utils.cjs');
require('bs58');
var useSocialAccounts = require('../../../../utils/hooks/useSocialAccounts/useSocialAccounts.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');
require('../../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('../../../../components/Alert/Alert.cjs');
require('../../../../context/WalletContext/WalletContext.cjs');
require('../../../../components/ShadowDOM/ShadowDOM.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');
require('../../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../../../../context/LoadingContext/LoadingContext.cjs');
var DynamicWidgetContext = require('../../context/DynamicWidgetContext.cjs');
require('../../helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../../context/FooterAnimationContext/index.cjs');
require('@dynamic-labs/types');
require('../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('../../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../../context/OnrampContext/OnrampContext.cjs');
var useRefreshUser = require('../../../../utils/hooks/useRefreshUser/useRefreshUser.cjs');
require('../../../../store/state/sendBalances.cjs');
require('../../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../components/PasskeyCard/PasskeyCard.cjs');
require('../CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../../../index.cjs');
require('../ReceiveWalletFunds/ReceiveWalletFunds.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');

const WaasBackupView = () => {
    const { setDynamicWidgetView } = DynamicWidgetContext.useWidgetContext();
    const user = useUser.useUser();
    const { primaryWallet } = useInternalDynamicContext.useInternalDynamicContext();
    const refresh = useRefreshUser.useRefreshUser();
    const { linkSocialAccount, isLinked, isProcessingForProvider } = useSocialAccounts.useSocialAccounts();
    const { t } = reactI18next.useTranslation();
    const [errorMessage, setErrorMessage] = React.useState(null);
    const isGoogleLinked = isLinked(sdkApiCore.ProviderEnum.Google);
    const checkGoogleDriveBackup = React.useCallback(() => {
        var _a, _b;
        if (!user || !primaryWallet)
            return false;
        const walletCredential = (_a = user.verifiedCredentials) === null || _a === void 0 ? void 0 : _a.find((cred) => cred.walletName === 'dynamicwaas' &&
            cred.address === primaryWallet.address);
        const keyShares = (_b = walletCredential === null || walletCredential === void 0 ? void 0 : walletCredential.walletProperties) === null || _b === void 0 ? void 0 : _b.keyShares;
        if (!keyShares || !Array.isArray(keyShares))
            return false;
        return keyShares.some((keyShare) => keyShare &&
            typeof keyShare === 'object' &&
            'backupLocation' in keyShare &&
            keyShare.backupLocation === 'googleDrive');
    }, [user, primaryWallet]);
    const isBackedUp = React.useMemo(() => checkGoogleDriveBackup(), [checkGoogleDriveBackup]);
    const handleBackClick = React.useCallback(() => setDynamicWidgetView('account-and-security-settings'), [setDynamicWidgetView]);
    const handleInfoClick = React.useCallback(() => {
        setDynamicWidgetView('waas-backup-info');
    }, [setDynamicWidgetView]);
    const handleBackupWalletClick = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        setErrorMessage(null);
        try {
            if (!primaryWallet) {
                throw new Error('No primary wallet found');
            }
            if (!isGoogleLinked) {
                yield linkSocialAccount(sdkApiCore.ProviderEnum.Google, {
                    forcePopup: true,
                    showWidgetAfterConnection: false,
                });
                yield refresh();
            }
            let waasConnector;
            if (walletConnectorCore.isDynamicWaasConnector(primaryWallet.connector)) {
                waasConnector = primaryWallet.connector;
            }
            else if (utils.isZeroDevConnector(primaryWallet.connector)) {
                const { eoaConnector } = primaryWallet.connector;
                if (!eoaConnector || !walletConnectorCore.isDynamicWaasConnector(eoaConnector)) {
                    throw new Error('ZeroDev connector must have a WaaS EOA connector');
                }
                waasConnector = eoaConnector;
            }
            else {
                throw new Error('Not a supported connector type');
            }
            yield waasConnector.backupKeySharesToGoogleDrive({
                accountAddress: primaryWallet.address,
            });
            yield refresh();
        }
        catch (error) {
            setErrorMessage(t('dyn_waas.backup.error'));
        }
    }), [primaryWallet, refresh, isGoogleLinked, linkSocialAccount, t]);
    const infoButton = (jsxRuntime.jsx(IconButton.IconButton, { type: 'button', onClick: handleInfoClick, "data-testid": 'info-button', children: jsxRuntime.jsx(questionMark.ReactComponent, {}) }));
    const closeButton = (jsxRuntime.jsx(IconButton.IconButton, { type: 'button', onClick: handleBackClick, "data-testid": 'close-button', children: jsxRuntime.jsx(close.ReactComponent, {}) }));
    return (jsxRuntime.jsxs("div", { className: 'waas-backup-view', children: [jsxRuntime.jsx(ModalHeader.ModalHeader, { leading: infoButton, trailing: closeButton, children: jsxRuntime.jsx("div", { className: 'send-balance-page-layout__header-content', children: jsxRuntime.jsx(Typography.Typography, { variant: 'title', color: 'primary', copykey: 'dyn_waas.backup.title', children: t('dyn_waas.backup.title') }) }) }), jsxRuntime.jsx("div", { className: 'account-wrap', children: jsxRuntime.jsxs("div", { className: 'settings-view__body', children: [errorMessage && (jsxRuntime.jsx(ErrorContainer.ErrorContainer, { variant: 'error', withIcon: false, children: errorMessage })), jsxRuntime.jsxs("div", { className: 'backup-content', children: [jsxRuntime.jsx("div", { className: 'backup-icon-container', children: jsxRuntime.jsx(Icon.Icon, { color: 'brand-primary', children: jsxRuntime.jsx(backupWaas.ReactComponent, { width: 64, height: 64 }) }) }), jsxRuntime.jsx("div", { className: 'backup-subtitle', children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', color: 'primary', copykey: 'dyn_waas.backup.subtitle', children: t('dyn_waas.backup.subtitle') }) }), jsxRuntime.jsx("div", { className: 'google-drive-section', children: jsxRuntime.jsxs("div", { className: 'google-drive-container', children: [jsxRuntime.jsxs("div", { className: 'google-drive-header', children: [jsxRuntime.jsx(googleDrive.ReactComponent, { className: 'google-drive-icon' }), jsxRuntime.jsxs("div", { className: 'google-drive-text', children: [jsxRuntime.jsxs("div", { className: 'google-drive-title-row', children: [jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', color: 'primary', weight: 'bold', copykey: 'dyn_waas.backup.google_drive', children: t('dyn_waas.backup.google_drive') }), isBackedUp && (jsxRuntime.jsx("div", { className: 'backed-up-pill', children: jsxRuntime.jsx(Typography.Typography, { color: 'inherit', weight: 'bold', copykey: 'dyn_waas.backup.backed_up', children: t('dyn_waas.backup.backed_up') }) }))] }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'secondary', copykey: 'dyn_waas.backup.google_drive_subtitle', children: t('dyn_waas.backup.google_drive_subtitle') })] }), isBackedUp && (jsxRuntime.jsx(check.ReactComponent, { className: 'google-drive-check-icon' }))] }), !isBackedUp && (jsxRuntime.jsx(TypographyButton.TypographyButton, { dataTestId: 'backup-wallet-button', onClick: handleBackupWalletClick, buttonClassName: 'backup-button', disabled: isProcessingForProvider(sdkApiCore.ProviderEnum.Google), typographyProps: {
                                                    color: 'primary',
                                                    weight: 'bold',
                                                }, copykey: 'dyn_waas.backup.back_up', buttonPadding: 'small', children: t('dyn_waas.backup.back_up') }))] }) })] })] }) })] }));
};

exports.WaasBackupView = WaasBackupView;
