"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminCheckpointIngress = void 0;
exports.buildCheckpointContract = buildCheckpointContract;
const ethers_1 = require("ethers");
const leafCodec_1 = require("../leafCodec");
const CHECKPOINT_HEADER_TUPLE = 'tuple(uint64 batchId,uint64 previousBatchId,uint64 chainId,uint256 checkpointBlock,uint256 startBlock,uint256 endBlock,bytes32 blockHash,bytes32 stateRoot,bytes32 paymentsRoot,bytes32 receiptsRoot,uint16 txCount,uint32 flags,uint64 submittedAt,address submitter,bytes32 contentURI)';
const PAYMENT_LEAF_TUPLE = 'tuple(bytes32,address,address,uint256,uint256,uint64,uint256,bool)';
const PAYMENT_LEAF_V2_TUPLE = 'tuple(bytes32,address,address,address,uint256,uint256,uint64,uint256,bool,uint32)';
function headerToTuple(h) {
    return [
        h.batchId,
        h.previousBatchId,
        h.chainId,
        h.checkpointBlock,
        h.startBlock,
        h.endBlock,
        h.blockHash,
        h.stateRoot,
        h.paymentsRoot,
        h.receiptsRoot,
        h.txCount,
        h.flags,
        h.submittedAt,
        h.submitter,
        h.contentURI,
    ];
}
class AdminCheckpointIngress {
    checkpoint;
    mode;
    constructor(checkpoint, mode) {
        this.checkpoint = checkpoint;
        this.mode = mode;
    }
    async submit(ctx) {
        const header = headerToTuple(ctx.header);
        const validatorSignatures = ctx.validatorSignatures ?? '0x01';
        switch (this.mode) {
            case 'commitment': {
                const tx = await this.checkpoint.submitCheckpointCommitment(header, validatorSignatures, ctx.contentURI, ctx.extensionData);
                return { hash: tx.hash };
            }
            case 'leaves': {
                const tx = await this.checkpoint.submitCheckpointWithLeaves(header, validatorSignatures, ctx.txHashes, (0, leafCodec_1.leavesToTuples)(ctx.leaves));
                return { hash: tx.hash };
            }
            case 'leaves-v2':
                throw new Error('leaves-v2 requires hub submitCheckpointWithLeavesV2 (deploy separate module or use hashes)');
            case 'hashes':
            default: {
                const tx = await this.checkpoint.submitCheckpoint(header, validatorSignatures, ctx.txHashes, ctx.extensionData);
                return { hash: tx.hash };
            }
        }
    }
    static extensionDataForMode(mode, leaves) {
        if (mode === 'leaves-v2')
            return (0, leafCodec_1.encodeExtensionLeavesV2)(leaves);
        return (0, leafCodec_1.encodeExtensionLeavesV1)(leaves);
    }
}
exports.AdminCheckpointIngress = AdminCheckpointIngress;
function buildCheckpointContract(wallet, proxy, mode) {
    const fns = [
        `function submitCheckpoint(${CHECKPOINT_HEADER_TUPLE} header, bytes validatorSignatures, bytes32[] txHashes, bytes extensionData) external`,
        `function submitCheckpointCommitment(${CHECKPOINT_HEADER_TUPLE} header, bytes validatorSignatures, bytes32 contentURI, bytes extensionData) external`,
        `function submitCheckpointWithLeaves(${CHECKPOINT_HEADER_TUPLE} header, bytes validatorSignatures, bytes32[] txHashes, ${PAYMENT_LEAF_TUPLE}[] leaves) external`,
        'function getLatestBatchId() view returns (uint64)',
        'function IMPLEMENTATION_VERSION() view returns (uint256)',
        'function isTxIncluded(bytes32 txHash) view returns (bool included, uint64 batchId)',
    ];
    return new ethers_1.ethers.Contract(proxy, fns, wallet);
}
