"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CHAIN138_ACTIVITY_NOTIFIED_TOPIC0 = void 0;
exports.decodeChain138ActivityNotifiedLogs = decodeChain138ActivityNotifiedLogs;
const ethers_1 = require("ethers");
const chain138Explorer_1 = require("./chain138Explorer");
/** topic0 = Chain138ActivityNotified(address,bytes32,bytes32,uint8,uint64,uint256,uint64,bool) */
exports.CHAIN138_ACTIVITY_NOTIFIED_TOPIC0 = '0x8dfc8206f97c75e58117c233ab5193e49d50c17bd4d035cdafebcf6794168ffe';
function decodeChain138ActivityNotifiedLogs(envelope, explorerBase) {
    if (envelope.status !== '1' || !Array.isArray(envelope.result))
        return [];
    const out = [];
    for (const log of envelope.result) {
        const t = log.topics ?? [];
        if (t.length < 4 || !log.transactionHash)
            continue;
        const participant = t[1] ? ethers_1.ethers.getAddress('0x' + t[1].slice(-40)) : '';
        const chain138TxHash = (0, chain138Explorer_1.chain138TxHashFromLogTopic)(t[2]);
        if (!chain138TxHash)
            continue;
        const instructionId = t[3] ?? '';
        let direction = 0;
        if (log.data && log.data.length >= 66) {
            try {
                const decoded = ethers_1.ethers.AbiCoder.defaultAbiCoder().decode(['uint8', 'uint64', 'uint256', 'uint64', 'bool'], log.data);
                direction = Number(decoded[0]);
            }
            catch {
                /* non-fatal */
            }
        }
        out.push({
            participant,
            chain138TxHash,
            chain138ExplorerUrl: (0, chain138Explorer_1.chain138ExplorerTxUrl)(chain138TxHash, explorerBase),
            instructionId,
            direction,
            mainnetTxHash: log.transactionHash,
            mainnetExplorerUrl: `https://etherscan.io/tx/${log.transactionHash}`,
            blockNumber: String(log.blockNumber ?? ''),
            logIndex: String(log.logIndex ?? ''),
        });
    }
    return out;
}
