'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { logger } from '../../../shared/logger.js';
import { generateMessages } from '../generateMessages/index.js';
import { getNonce, fetchAndStoreNonce, consumeNonce } from '../../../store/state/nonce/nonce.js';

// TODO - This function is doing too much we should split it up.
const getVerifyArgs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ walletConnector, walletProvider, environmentId, displaySiweStatement, skipEmptyAccountCheck, siweStatement, publicWalletAddress, signedMessageOverride, messageToSignOverride, }) {
    // WARNING - PLEASE READ!
    // DO NOT ADD ANY NEW ASYNC WORK WITH AWAIT!
    // Deep linking could break with too many blocking awaits
    // before the we call generateMessages.
    // This could manifest in users being directed to the app store
    // instead of the actual wallet app.
    const chain = walletConnector.connectedChain;
    logger.logVerboseTroubleshootingMessage('[getVerifyArgs] called', {
        chain,
        displaySiweStatement,
        environmentId,
        hasMessageToSignOverride: Boolean(messageToSignOverride),
        hasSignedMessageOverride: Boolean(signedMessageOverride),
        publicWalletAddress,
        siweStatement,
        walletConnectorKey: walletConnector.key,
    });
    if (signedMessageOverride && messageToSignOverride) {
        logger.logVerboseTroubleshootingMessage('[getVerifyArgs] using overrides', {
            messageToSignOverride,
            messageToSignOverrideLength: messageToSignOverride.length,
            signedMessageOverride,
            signedMessageOverrideLength: signedMessageOverride.length,
        });
        const [network, additionalWalletAddresses] = yield Promise.all([
            walletConnector.getNetwork(true),
            walletConnector.getAdditionalAddresses(publicWalletAddress),
        ]);
        const result = {
            additionalWalletAddresses,
            chain,
            messageToSign: messageToSignOverride,
            network: String(network),
            publicWalletAddress,
            signedMessage: signedMessageOverride,
            skipEmptyAccountCheck,
            walletName: walletConnector.key,
            walletProvider,
        };
        logger.logVerboseTroubleshootingMessage('[getVerifyArgs] returning with overrides', {
            chain: result.chain,
            messageToSign: result.messageToSign,
            network: result.network,
            publicWalletAddress: result.publicWalletAddress,
            signedMessage: result.signedMessage,
            walletName: result.walletName,
        });
        return result;
    }
    if (!getNonce()) {
        logger.debug('[getVerifyArgs] Nonce missing, attempting to fetch');
        yield fetchAndStoreNonce();
    }
    const nonce = consumeNonce();
    // generate message to sign and then initiate signing step to prove ownership
    const { messageToSign, signedMessage } = yield generateMessages(publicWalletAddress, walletConnector, nonce, environmentId, displaySiweStatement, siweStatement);
    const [network, additionalWalletAddresses] = yield Promise.all([
        walletConnector.getNetwork(true),
        walletConnector.getAdditionalAddresses(publicWalletAddress),
    ]);
    return {
        additionalWalletAddresses,
        chain,
        messageToSign,
        network: String(network),
        publicWalletAddress,
        signedMessage,
        skipEmptyAccountCheck,
        walletName: walletConnector.key,
        walletProvider,
    };
});

export { getVerifyArgs };
