'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var multiWallet = require('@dynamic-labs/multi-wallet');
var utils = require('@dynamic-labs/utils');
var logger = require('../../../shared/logger.cjs');

const getChainIdForMessage = (walletConnector) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    // If it's a Solana wallet, we should not specify a chainId
    // since it breaks the message signature in some wallets (e.g. Trust Wallet)
    if (walletConnector.connectedChain === 'SOL') {
        return undefined;
    }
    if (walletConnector.connectedChain === 'SUI') {
        return undefined;
    }
    const chain = yield walletConnector.getNetwork(true);
    if (chain) {
        return chain;
    }
    if (walletConnector.key === 'bloctoevm') {
        return 137;
    }
    if (walletConnector.connectedChain === 'EVM') {
        return 1;
    }
    return chain;
});
const generateMessages = (publicWalletAddress, walletConnector, nonce, projectEnvironmentId, displaySiweStatement, siweStatement) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    if (publicWalletAddress === undefined) {
        throw new utils.DynamicError('Unable to fetch the public address from the wallet', 'missing_public_address');
    }
    if (!walletConnector.connectedChain) {
        throw new utils.DynamicError('Wallet is not connected', 'wallet_is_not_connected');
    }
    const chainId = yield getChainIdForMessage(walletConnector);
    const parsedAddress = walletConnector.parseAddress(publicWalletAddress);
    let currentUrl;
    // For native mobile, getDisplayOrigin() returns the app's web origin (e.g., "https://demo.dynamic.xyz")
    // which is needed for valid SIWE messages instead of the deep link scheme we get from getUrl()
    const displayOrigin = utils.PlatformService.getDisplayOrigin();
    if (utils.PlatformService.isNativeMobile && displayOrigin) {
        currentUrl = new URL(displayOrigin);
    }
    else {
        // Fallback to currentUrl for non-native mobile (browser/web)
        currentUrl = utils.PlatformService.getUrl();
    }
    const messageToSign = multiWallet.generateMessageToSign({
        blockchain: walletConnector.connectedChain,
        chainId: chainId,
        domain: currentUrl.host,
        nonce,
        // The SIWE parser library used to validate signed messages for EVM/Ethereum
        // checks that an address is in EIP55 format, so make sure we do that here.
        // see: https://eips.ethereum.org/EIPS/eip-55
        publicKey: parsedAddress,
        requestId: projectEnvironmentId,
        resources: walletConnector.providerResources,
        statement: displaySiweStatement
            ? siweStatement.replace(/(\r\n|\n|\r)/gm, ' ').trim()
            : undefined,
        uri: currentUrl.origin + currentUrl.pathname,
    });
    logger.logger.debug('[generateMessages] Generated SIWE message:', {
        messageToSign,
    });
    const signedMessage = yield walletConnector.proveOwnership(parsedAddress, messageToSign);
    if (!signedMessage) {
        throw new utils.DynamicError('Unable to sign the message', 'sign_message_failed');
    }
    return { messageToSign, signedMessage };
});

exports.generateMessages = generateMessages;
