'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
var utils$1 = require('@dynamic-labs/utils');
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');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
var api = require('../api.cjs');
var utils = require('../utils.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../events/dynamicEvents.cjs');
var storeTokenAndUser = require('../../../client/extension/storeTokenAndUser/storeTokenAndUser.cjs');

const storeSelectedWallet = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ environmentId, walletId, }) {
    try {
        const response = yield api.sdkApi().selectUserWallet({
            environmentId,
            userWalletSelectionRequest: {
                walletId,
            },
        });
        return storeTokenAndUser.storeTokenAndUser(response);
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error transfering wallet');
        throw new utils$1.DynamicError(data.error);
    }
});
const linkWallet = (environmentId_1, _b) => _tslib.__awaiter(void 0, [environmentId_1, _b], void 0, function* (environmentId, { messageToSign, network, signedMessage, publicWalletAddress, chain, walletName, walletProvider, additionalWalletAddresses, backup, }) {
    const verifyRequest = sdkApiCore.VerifyRequestFromJSON({
        additionalWalletAddresses,
        backup,
        chain,
        messageToSign,
        network,
        publicWalletAddress,
        signedMessage,
        walletName,
        walletProvider,
    });
    try {
        const response = yield api.sdkApi().verifyLink({
            environmentId,
            verifyRequest,
        });
        storeTokenAndUser.storeTokenAndUser(response);
        return response;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error linkWallet');
        if (data.code === 'embedded_wallet_address_already_linked') {
            throw new utils$1.EmbeddedWalletExistsError(data.code);
        }
        if (data.code === 'reassign_wallet_confirm' ||
            data.code === 'reassign_wallet_error') {
            throw new utils$1.WalletUsedError(data.code);
        }
        if (data.code === 'merge_accounts_confirmation') {
            throw new utils$1.MergeAccountsConfirmationError({
                code: data.code,
                mergeConflicts: data.payload.mergeConflicts,
                walletAddress: publicWalletAddress,
            });
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new utils$1.DynamicError(data.error);
    }
});
const unlinkWallet = (_c) => _tslib.__awaiter(void 0, [_c], void 0, function* ({ environmentId, primaryWalletId, walletId, onSuccess, }) {
    // 7702 wallets have a -zerodev suffix because it's a credential created on the fly
    const normalizedPrimaryWalletId = primaryWalletId === null || primaryWalletId === void 0 ? void 0 : primaryWalletId.replace('-zerodev', '');
    try {
        const response = yield api.sdkApi().verifyUnlink({
            environmentId,
            verifyUnlinkRequest: {
                primaryWalletId: normalizedPrimaryWalletId,
                walletId,
            },
        });
        onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error transfering wallet');
        throw new utils$1.DynamicError(data.error);
    }
});
const transferWallet = (environmentId_2, _d) => _tslib.__awaiter(void 0, [environmentId_2, _d], void 0, function* (environmentId, { network, messageToSign, signedMessage, publicWalletAddress, chain, walletName, walletProvider, skipEmptyAccountCheck = false, }) {
    // We should find a better way to do this than force type casting
    const verifyRequest = {
        chain,
        messageToSign,
        network,
        publicWalletAddress,
        signedMessage,
        skipEmptyAccountCheck,
        walletName,
        walletProvider,
    };
    try {
        const response = yield api.sdkApi().verifyTransfer({
            environmentId,
            verifyRequest,
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error transfering wallet');
        throw new utils$1.DynamicError(data.error);
    }
});
const verifyWallet = (environmentId_3, _e) => _tslib.__awaiter(void 0, [environmentId_3, _e], void 0, function* (environmentId, { messageToSign, signedMessage, publicWalletAddress, chain, walletName, walletProvider, captchaToken, network, oauth, additionalWalletAddresses, sessionPublicKey, }) {
    const verifyRequest = sdkApiCore.VerifyRequestFromJSON({
        additionalWalletAddresses,
        captchaToken,
        chain,
        messageToSign,
        network,
        oauth,
        publicWalletAddress,
        sessionPublicKey,
        signedMessage,
        walletName,
        walletProvider,
    });
    try {
        const response = yield api.sdkApi().verify({
            environmentId,
            verifyRequest,
        });
        return response;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error verifyWallet');
        if (data.code === 'wallet_not_deployed') {
            throw new utils$1.WalletNotDeployedError();
        }
        if (data.error.code === 'chainalysis_blocked_wallet') {
            throw new utils$1.ChainalysisError(data.payload.walletPublicKey);
        }
        if (data.error.code === 'gate_blocked') {
            throw new utils$1.GateBlockedError(data.payload.walletPublicKey);
        }
        if (data.error.code === 'missing_from_list') {
            throw new utils$1.NoAccessError({
                walletPublicKey: data.payload.walletPublicKey,
            });
        }
        if (data.code === 'email_associated_with_different_provider') {
            throw new utils$1.AccountExistsError(data.error, data.payload);
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new utils$1.DynamicError(data.error);
    }
});
const upgradeEmbeddedWalletToV2 = (_f) => _tslib.__awaiter(void 0, [_f], void 0, function* ({ environmentId, sessionPublicKey, }) {
    try {
        const response = yield api.sdkApi().upgradeEmbeddedWalletToV2({
            environmentId,
            upgradeEmbeddedWalletToV2Request: { sessionPublicKey },
        });
        return response;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error upgradeEmbeddedWalletToV2');
        throw new utils$1.DynamicError(data.error);
    }
});
const softDeleteEmbeddedWallet = (_g) => _tslib.__awaiter(void 0, [_g], void 0, function* ({ environmentId, walletId, }) {
    try {
        yield api.sdkApi().softDeleteEmbeddedWallet({
            environmentId,
            walletId,
        });
    }
    catch (e) {
        const data = yield utils.logResponseError(e, `Error softDeleteEmbeddedWallet for ${walletId} in ${environmentId}`);
        throw new utils$1.DynamicError(data.error);
    }
});
const restoreEmbeddedWallet = (_h) => _tslib.__awaiter(void 0, [_h], void 0, function* ({ environmentId, walletId, }) {
    try {
        yield api.sdkApi().restoreEmbeddedWallet({
            environmentId,
            walletId,
        });
    }
    catch (e) {
        const data = yield utils.logResponseError(e, `Error restoreEmbeddedWallet for ${walletId} in ${environmentId}`);
        throw new utils$1.DynamicError(data.error);
    }
});

exports.linkWallet = linkWallet;
exports.restoreEmbeddedWallet = restoreEmbeddedWallet;
exports.softDeleteEmbeddedWallet = softDeleteEmbeddedWallet;
exports.storeSelectedWallet = storeSelectedWallet;
exports.transferWallet = transferWallet;
exports.unlinkWallet = unlinkWallet;
exports.upgradeEmbeddedWalletToV2 = upgradeEmbeddedWalletToV2;
exports.verifyWallet = verifyWallet;
