'use client'
import { __awaiter } from '../../../../../../_virtual/_tslib.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import 'react';
import 'eventemitter3';
import '@dynamic-labs/utils';
import '@dynamic-labs-sdk/client';
import '../../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../ViewContext/ViewContext.js';
import '../../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../../../utils/constants/colors.js';
import '../../../../utils/constants/values.js';
import '../../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../../store/state/nonce/nonce.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import { retryEmailVerification, signInWithEmailVerification } from '../../../../data/api/email/email.js';
import '@dynamic-labs/locale';
import '../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import '../../../../events/dynamicEvents.js';
import { storeTokenAndUser } from '../../../../client/extension/storeTokenAndUser/storeTokenAndUser.js';
import { generateSessionKeys } from '../../../../client/extension/functions/generateSessionKeys/generateSessionKeys.js';
import { persistOtpVerificationState } from '../otpVerificationStorage/otpVerificationStorage.js';

/**
 * Builds a ProviderVerificationHandler for email OTP from the given parameters.
 * Used both after creating a new email verification and when restoring a
 * persisted verification state after a webview remount.
 */
const buildEmailOtpHandler = ({ captchaToken, email, embeddedWalletSessionKeys: { shouldRegisterSessionKeysOnSignin, generateSessionKey, }, environmentId, initialVerificationUUID, }) => {
    // Mutable so retry can update the UUID for subsequent verify calls
    let verificationUUID = initialVerificationUUID;
    return {
        retry: () => __awaiter(void 0, void 0, void 0, function* () {
            const retryEmailVerificationResponse = yield retryEmailVerification({
                email,
                environmentId,
                verificationUUID,
            });
            // eslint-disable-next-line prefer-destructuring
            verificationUUID = retryEmailVerificationResponse.verificationUUID;
            persistOtpVerificationState({
                captchaToken,
                destination: email,
                type: 'email',
                verificationUUID,
            });
        }),
        type: 'email',
        verify: (oneTimePassword, options) => __awaiter(void 0, void 0, void 0, function* () {
            let sessionPublicKey = undefined;
            if (shouldRegisterSessionKeysOnSignin()) {
                const keypair = yield generateSessionKey();
                sessionPublicKey = keypair.publicKey;
            }
            yield generateSessionKeys();
            const response = yield signInWithEmailVerification({
                captchaToken,
                environmentId,
                sessionPublicKey,
                verificationToken: oneTimePassword,
                verificationUUID,
            });
            if (!(options === null || options === void 0 ? void 0 : options.skipSetUserAndAuthToken)) {
                storeTokenAndUser(response);
            }
            return {
                destination: email,
                response: response,
                verified: true,
            };
        }),
    };
};

export { buildEmailOtpHandler };
