'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('../../../client/client.cjs');
require('react');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../ViewContext/ViewContext.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');
var email = require('../../../data/api/email/email.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
var getClientSessionKeys = require('../../../utils/functions/clientSessionKeys/getClientSessionKeys.cjs');
require('../../../events/dynamicEvents.cjs');
var storeTokenAndUser = require('../../../client/extension/storeTokenAndUser/storeTokenAndUser.cjs');

const createEmailHandler = (environmentId, { shouldRegisterSessionKeysOnSignin, generateSessionKey, }) => (email$1, captchaToken) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const createEmailVerificationResponse = yield email.createEmailVerification({
        captchaToken,
        email: email$1,
        environmentId,
    });
    let { verificationUUID } = createEmailVerificationResponse;
    return {
        retry: () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
            const retryEmailVerificationResponse = yield email.retryEmailVerification({
                email: email$1,
                environmentId,
                verificationUUID,
            });
            // eslint-disable-next-line prefer-destructuring
            verificationUUID = retryEmailVerificationResponse.verificationUUID;
        }),
        type: 'email',
        verify: (oneTimePassword, options) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
            let sessionPublicKey = undefined;
            if (shouldRegisterSessionKeysOnSignin()) {
                const keypair = yield generateSessionKey();
                sessionPublicKey = keypair.publicKey;
            }
            yield getClientSessionKeys.generateClientSessionKeys();
            const response = yield email.signInWithEmailVerification({
                captchaToken,
                environmentId,
                sessionPublicKey,
                verificationToken: oneTimePassword,
                verificationUUID,
            });
            // Skip setting the user auth token and only return the VerifyResponse
            // if needed because setUser can trigger view re-renders.
            if (!(options === null || options === void 0 ? void 0 : options.skipSetUserAndAuthToken)) {
                storeTokenAndUser.storeTokenAndUser(response);
            }
            return {
                destination: email$1,
                response: response,
                verified: true,
            };
        }),
    };
});

exports.createEmailHandler = createEmailHandler;
