'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import '../../../client/client.js';
import 'react';
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 '@dynamic-labs/utils';
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 { createSmsVerification, signInWithSmsVerification } from '../../../data/api/sms/sms.js';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import { generateClientSessionKeys } from '../../../utils/functions/clientSessionKeys/getClientSessionKeys.js';
import '../../../events/dynamicEvents.js';
import { storeTokenAndUser } from '../../../client/extension/storeTokenAndUser/storeTokenAndUser.js';

const createSmsHandler = (environmentId, { shouldRegisterSessionKeysOnSignin, generateSessionKey, }) => (phone, captchaToken) => __awaiter(void 0, void 0, void 0, function* () {
    const { verificationUUID } = yield createSmsVerification({
        captchaToken,
        environmentId,
        isoCountryCode: phone.iso2,
        phoneCountryCode: phone.dialCode,
        phoneNumber: phone.phone,
    });
    return {
        type: 'sms',
        verify: (oneTimePassword, options) => __awaiter(void 0, void 0, void 0, function* () {
            let sessionPublicKey = undefined;
            if (shouldRegisterSessionKeysOnSignin()) {
                const keypair = yield generateSessionKey();
                sessionPublicKey = keypair.publicKey;
            }
            yield generateClientSessionKeys();
            const response = yield signInWithSmsVerification({
                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(response);
            }
            return {
                destination: phone,
                response: response,
                verified: true,
            };
        }),
    };
});

export { createSmsHandler };
