"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSequenceFromLogAlgorand = exports.parseSequencesFromLogSolana = exports.parseSequenceFromLogSolana = exports.parseSequencesFromLogEth = exports.parseSequenceFromLogEth = void 0;
const ethers_1 = require("ethers");
const ethers_contracts_1 = require("../ethers-contracts");
function parseSequenceFromLogEth(receipt, bridgeAddress) {
    // TODO: dangerous!(?)
    const bridgeLog = receipt.logs.filter((l) => {
        return l.address === bridgeAddress;
    })[0];
    const { args: { sequence }, } = ethers_contracts_1.Implementation__factory.createInterface().parseLog(bridgeLog);
    return sequence.toString();
}
exports.parseSequenceFromLogEth = parseSequenceFromLogEth;
function parseSequencesFromLogEth(receipt, bridgeAddress) {
    // TODO: dangerous!(?)
    const bridgeLogs = receipt.logs.filter((l) => {
        return l.address === bridgeAddress;
    });
    return bridgeLogs.map((bridgeLog) => {
        const { args: { sequence }, } = ethers_contracts_1.Implementation__factory.createInterface().parseLog(bridgeLog);
        return sequence.toString();
    });
}
exports.parseSequencesFromLogEth = parseSequencesFromLogEth;
const SOLANA_SEQ_LOG = "Program log: Sequence: ";
function parseSequenceFromLogSolana(info) {
    var _a, _b, _c, _d;
    // TODO: better parsing, safer
    const sequence = (_d = (_c = (_b = (_a = info.meta) === null || _a === void 0 ? void 0 : _a.logMessages) === null || _b === void 0 ? void 0 : _b.filter((msg) => msg.startsWith(SOLANA_SEQ_LOG))) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.replace(SOLANA_SEQ_LOG, "");
    if (!sequence) {
        throw new Error("sequence not found");
    }
    return sequence.toString();
}
exports.parseSequenceFromLogSolana = parseSequenceFromLogSolana;
function parseSequencesFromLogSolana(info) {
    var _a, _b;
    // TODO: better parsing, safer
    return (_b = (_a = info.meta) === null || _a === void 0 ? void 0 : _a.logMessages) === null || _b === void 0 ? void 0 : _b.filter((msg) => msg.startsWith(SOLANA_SEQ_LOG)).map((msg) => msg.replace(SOLANA_SEQ_LOG, ""));
}
exports.parseSequencesFromLogSolana = parseSequencesFromLogSolana;
function parseSequenceFromLogAlgorand(result) {
    let sequence = "";
    if (result["inner-txns"]) {
        const innerTxns = result["inner-txns"];
        class iTxn {
        }
        innerTxns.forEach((txn) => {
            if (txn.logs) {
                sequence = ethers_1.BigNumber.from(txn.logs[0].slice(0, 8)).toString();
            }
        });
    }
    return sequence;
}
exports.parseSequenceFromLogAlgorand = parseSequenceFromLogAlgorand;
