"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformMoralisTransaction = transformMoralisTransaction;
exports.transformMoralisInternalTx = transformMoralisInternalTx;
exports.transformMoralisTokenTransfer = transformMoralisTokenTransfer;
exports.transformMoralisQueryParams = transformMoralisQueryParams;
exports.buildMoralisQueryString = buildMoralisQueryString;
exports.formatMoralisChainId = formatMoralisChainId;
const crypto_wallet_core_1 = require("@bitpay-labs/crypto-wallet-core");
const transaction_1 = require("../../evm/models/transaction");
/**
 * Shared Moralis transformation and query-building utilities.
 * Used by both MoralisAdapter (multi-provider) and MoralisStateProvider (standalone CSP).
 */
function transformMoralisTransaction(tx) {
    const txid = tx.hash || tx.transaction_hash;
    const transformed = {
        chain: tx.chain,
        network: tx.network,
        txid,
        blockHeight: Number(tx.block_number ?? tx.blockNumber),
        blockHash: tx.block_hash ?? tx.blockHash,
        blockTime: new Date(tx.block_timestamp ?? tx.blockTimestamp),
        blockTimeNormalized: new Date(tx.block_timestamp ?? tx.blockTimestamp),
        value: tx.value,
        gasLimit: tx.gas ?? 0,
        gasPrice: tx.gas_price ?? tx.gasPrice ?? 0,
        fee: Number(tx.receipt_gas_used ?? tx.receiptGasUsed ?? 0) * Number(tx.gas_price ?? tx.gasPrice ?? 0),
        nonce: tx.nonce,
        to: (tx.to_address ?? tx.toAddress)
            ? crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.to_address ?? tx.toAddress)
            : '',
        from: crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.from_address ?? tx.fromAddress),
        data: tx.input,
        internal: [],
        calls: tx?.internal_transactions?.map((t) => transformMoralisInternalTx(t)) || [],
        effects: [],
        category: tx.category,
        wallets: [],
        transactionIndex: tx.transaction_index ?? tx.transactionIndex
    };
    transaction_1.EVMTransactionStorage.addEffectsToTxs([transformed]);
    return transformed;
}
function transformMoralisInternalTx(tx) {
    return {
        from: crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.from),
        to: crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.to),
        gas: tx.gas,
        gasUsed: tx.gas_used,
        input: tx.input,
        output: tx.output,
        type: tx.type,
        value: tx.value,
        abiType: transaction_1.EVMTransactionStorage.abiDecode(tx.input)
    };
}
function transformMoralisTokenTransfer(transfer) {
    const base = transformMoralisTransaction(transfer);
    return {
        ...base,
        transactionHash: transfer.transaction_hash,
        transactionIndex: transfer.transaction_index,
        contractAddress: transfer.contract_address ?? transfer.address,
        name: transfer.token_name
    };
}
function transformMoralisQueryParams(params) {
    const { chainId, args } = params;
    const query = { chain: formatMoralisChainId(chainId) };
    if (args) {
        if (args.startBlock || args.endBlock) {
            if (args.startBlock)
                query.from_block = Number(args.startBlock);
            if (args.endBlock)
                query.to_block = Number(args.endBlock);
        }
        else {
            if (args.startDate)
                query.from_date = args.startDate;
            if (args.endDate)
                query.to_date = args.endDate;
        }
        if (args.direction) {
            query.order = Number(args.direction) > 0 ? 'ASC' : 'DESC';
        }
        if (args.date) {
            query.date = new Date(args.date).getTime();
        }
    }
    return query;
}
function buildMoralisQueryString(params) {
    const query = [];
    for (const [key, value] of Object.entries(params)) {
        if (Array.isArray(value)) {
            for (let i = 0; i < value.length; i++) {
                if (value[i] != null)
                    query.push(`${key}%5B${i}%5D=${value[i]}`);
            }
        }
        else if (value != null) {
            query.push(`${key}=${value}`);
        }
    }
    return query.length ? `?${query.join('&')}` : '';
}
function formatMoralisChainId(chainId) {
    const str = String(chainId);
    if (str.startsWith('0x'))
        return str;
    return '0x' + BigInt(str).toString(16);
}
//# sourceMappingURL=moralis-utils.js.map