'use client'
import { __awaiter } from '../../../../../../_virtual/_tslib.js';
import { InvalidPrimaryWalletOrConnectorError, DynamicError } from '@dynamic-labs/utils';
import '@dynamic-labs/sdk-api-core';
import '@dynamic-labs-sdk/client/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 '../../../../context/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 { findEmbeddedWalletFromVerifiedCredentials } from '../../../../utils/functions/findEmbeddedWalletFromVerifiedCredentials/findEmbeddedWalletFromVerifiedCredentials.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../../store/state/nonce/nonce.js';
import { addPasskeyIdentifierToWalletConnector } from '../../../../utils/functions/addPasskeyIdentifierToWalletConnector/addPasskeyIdentifierToWalletConnector.js';
import '../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../../events/dynamicEvents.js';
import { createTurnkeyEmbeddedWallet } from '../../../../data/api/embeddedWallets/embeddedWallets.js';
import '@dynamic-labs/locale';

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

export { createEmbeddedWalletRequest };
