'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 sms = require('../../../data/api/sms/sms.cjs');
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 createSmsHandler = (environmentId, { shouldRegisterSessionKeysOnSignin, generateSessionKey, }) => (phone, captchaToken) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const { verificationUUID } = yield sms.createSmsVerification({
        captchaToken,
        environmentId,
        isoCountryCode: phone.iso2,
        phoneCountryCode: phone.dialCode,
        phoneNumber: phone.phone,
    });
    return {
        type: 'sms',
        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 sms.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.storeTokenAndUser(response);
            }
            return {
                destination: phone,
                response: response,
                verified: true,
            };
        }),
    };
});

exports.createSmsHandler = createSmsHandler;
