'use client'
'use strict';

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

var _tslib = require('../../../../../../_virtual/_tslib.cjs');
var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs-sdk/client/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('../../../../context/ViewContext/ViewContext.cjs');
require('../../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../../utils/constants/colors.cjs');
require('../../../../utils/constants/values.cjs');
require('../../../../shared/consts/index.cjs');
var findEmbeddedWalletFromVerifiedCredentials = require('../../../../utils/functions/findEmbeddedWalletFromVerifiedCredentials/findEmbeddedWalletFromVerifiedCredentials.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../store/state/nonce/nonce.cjs');
var addPasskeyIdentifierToWalletConnector = require('../../../../utils/functions/addPasskeyIdentifierToWalletConnector/addPasskeyIdentifierToWalletConnector.cjs');
require('../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../../events/dynamicEvents.cjs');
var embeddedWallets = require('../../../../data/api/embeddedWallets/embeddedWallets.cjs');
require('@dynamic-labs/locale');

const createEmbeddedWalletRequest = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ user, walletConnector, environmentId, chains, sessionPublicKey, webAuthnAttestation, withAuthenticator, }) {
    if (!walletConnector) {
        throw new utils.InvalidPrimaryWalletOrConnectorError();
    }
    if (!user) {
        throw new utils.DynamicError('User is not defined');
    }
    walletConnector.setEmail(user.email);
    let authenticatorBody;
    if (withAuthenticator || webAuthnAttestation !== undefined) {
        if (!user.email) {
            addPasskeyIdentifierToWalletConnector.addPasskeyIdentifierToWalletConnector(walletConnector, user);
        }
        const { attestation, challenge, displayName } = webAuthnAttestation || (yield walletConnector.getWebAuthnAttestation());
        authenticatorBody = {
            attestation: attestation,
            challenge,
            passkeyAlias: displayName,
        };
    }
    const updatedUser = yield embeddedWallets.createTurnkeyEmbeddedWallet(Object.assign({ chains,
        environmentId,
        sessionPublicKey }, authenticatorBody));
    if (!updatedUser) {
        throw new utils.DynamicError('Failed to create embedded wallet');
    }
    const primaryChain = walletConnector.connectedChain;
    const embeddedWalletVerifiedCredential = findEmbeddedWalletFromVerifiedCredentials.findEmbeddedWalletFromVerifiedCredentials(updatedUser, [primaryChain]);
    if (!embeddedWalletVerifiedCredential) {
        throw new utils.DynamicError('EmbeddedWalletVerifiedCredential not found');
    }
    walletConnector.setVerifiedCredentials(updatedUser.verifiedCredentials);
    return {
        embeddedWalletVerifiedCredential,
        updatedUser,
        walletConnector,
    };
});

exports.createEmbeddedWalletRequest = createEmbeddedWalletRequest;
