// @ts-ignore - WASM files are in dist/generated/ (checked into git), not src/generated/
import init, { WasmPrePeginTx, WasmPrePeginHtlcConnector, computeMinClaimValue as wasmComputeMinClaimValue, numUtxosForInputLabels as wasmNumUtxosForInputLabels } from "./generated/btc_vault.js";
let wasmInitialized = false;
let wasmInitPromise = null;
export async function initWasm() {
    if (wasmInitialized)
        return;
    if (wasmInitPromise)
        return wasmInitPromise;
    wasmInitPromise = (async () => {
        try {
            await init();
            wasmInitialized = true;
        }
        finally {
            wasmInitPromise = null;
        }
    })();
    return wasmInitPromise;
}
/**
 * Creates an unfunded Pre-PegIn transaction with no inputs and one HTLC output.
 *
 * The HTLC output value (htlcValue) covers the peg-in amount, depositor claim value,
 * and minimum pegin fee — all computed internally from the provided contract parameters.
 *
 * After building the Pre-PegIn transaction, the caller must:
 * 1. Select UTXOs covering htlcValue + network fees
 * 2. Fund the transaction (add inputs and change output)
 * 3. Compute the funded transaction's txid
 * 4. Call buildPeginTxFromPrePegin() to derive the PegIn transaction
 * 5. Sign the PegIn input using the HTLC hashlock leaf (leaf 0)
 *
 * @param params - Pre-PegIn parameters from contract and depositor wallet
 * @returns Unfunded transaction details with HTLC output information
 */
export async function createPrePeginTransaction(params) {
    await initWasm();
    const tx = new WasmPrePeginTx(params.depositorPubkey, params.vaultProviderPubkey, params.vaultKeeperPubkeys, params.universalChallengerPubkeys, params.hashH, params.timelockRefund, params.pegInAmount, params.feeRate, params.numLocalChallengers, params.councilQuorum, params.councilSize, params.network);
    try {
        return {
            txHex: tx.toHex(),
            txid: tx.getTxid(),
            htlcValue: tx.getHtlcValue(),
            htlcScriptPubKey: tx.getHtlcScriptPubKey(),
            htlcAddress: tx.getHtlcAddress(),
            peginAmount: tx.getPeginAmount(),
            depositorClaimValue: tx.getDepositorClaimValue(),
        };
    }
    finally {
        tx.free();
    }
}
/**
 * Derives the PegIn transaction from a funded Pre-PegIn txid.
 *
 * The PegIn transaction has a single input spending Pre-PegIn output 0
 * via the hashlock + all-party script (leaf 0). Since all Pre-PegIn inputs
 * must be SegWit/Taproot, the txid is stable after funding — signing does
 * not change it.
 *
 * @param params - Same PrePeginParams used to create the Pre-PegIn transaction
 * @param timelockPegin - CSV timelock in blocks for the PegIn vault output
 * @param fundedPrePeginTxid - Hex-encoded txid of the funded Pre-PegIn transaction
 * @returns PegIn transaction details including vault output information
 */
export async function buildPeginTxFromPrePegin(params, timelockPegin, fundedPrePeginTxid) {
    await initWasm();
    const prePeginTx = new WasmPrePeginTx(params.depositorPubkey, params.vaultProviderPubkey, params.vaultKeeperPubkeys, params.universalChallengerPubkeys, params.hashH, params.timelockRefund, params.pegInAmount, params.feeRate, params.numLocalChallengers, params.councilQuorum, params.councilSize, params.network);
    let peginTx = null;
    try {
        peginTx = prePeginTx.buildPeginTx(timelockPegin, fundedPrePeginTxid);
        return {
            txHex: peginTx.toHex(),
            txid: peginTx.getTxid(),
            vaultScriptPubKey: peginTx.getVaultScriptPubKey(),
            vaultValue: peginTx.getVaultValue(),
        };
    }
    finally {
        peginTx?.free();
        prePeginTx.free();
    }
}
/**
 * Returns HTLC connector script info for signing the PegIn transaction input.
 *
 * The depositor signs PegIn input 0 using the hashlock leaf (leaf 0) of the
 * Pre-PegIn HTLC output. Use getHashlockScript() and getHashlockControlBlock()
 * to construct the tapLeafScript entry in the PSBT.
 *
 * @param params - HTLC connector parameters (subset of PrePeginParams)
 * @returns Hashlock and refund script info for PSBT construction
 */
export async function getPrePeginHtlcConnectorInfo(params) {
    await initWasm();
    const connector = new WasmPrePeginHtlcConnector(params.depositorPubkey, params.vaultProviderPubkey, params.vaultKeeperPubkeys, params.universalChallengerPubkeys, params.hashH, params.timelockRefund);
    try {
        return {
            hashlockScript: connector.getHashlockScript(),
            hashlockControlBlock: connector.getHashlockControlBlock(),
            refundScript: connector.getRefundScript(),
            refundControlBlock: connector.getRefundControlBlock(),
            address: connector.getAddress(params.network),
            scriptPubKey: connector.getScriptPubKey(params.network),
        };
    }
    finally {
        connector.free();
    }
}
/**
 * Compute the minimum depositor claim value (PegIn output 1) in satoshis.
 *
 * This covers the full downstream tx graph cost (Claim → Assert → Payout)
 * based on the protocol parameters.
 */
export async function computeMinClaimValue(numLocalChallengers, numUniversalChallengers, councilQuorum, councilSize, feeRate) {
    await initWasm();
    return wasmComputeMinClaimValue(numLocalChallengers, numUniversalChallengers, councilQuorum, councilSize, feeRate);
}
/**
 * Returns the protocol constant for the number of UTXOs (Assert outputs)
 * per challenger. Currently 3, derived from Bitcoin's 1000 stack element limit.
 */
export async function numUtxosForInputLabels() {
    await initWasm();
    return wasmNumUtxosForInputLabels();
}
// Export constants
export { TAP_INTERNAL_KEY, tapInternalPubkey } from "./constants.js";
// Export payout connector utilities
export { createPayoutConnector, getPeginPayoutScript } from "./payoutConnector.js";
// Export assert payout/nopayout connector utilities (depositor-as-claimer)
export { getAssertPayoutScriptInfo, getAssertNoPayoutScriptInfo, } from "./assertPayoutNoPayoutConnector.js";
// Export challenge assert connector utilities (depositor-as-claimer)
export { getChallengeAssertScriptInfo } from "./challengeAssertConnector.js";
// Re-export raw WASM types for callers that need direct access
// @ts-ignore - WASM files are in dist/generated/ (checked into git), not src/generated/
export { WasmPeginTx, WasmPeginPayoutConnector, WasmPrePeginTx, WasmPrePeginHtlcConnector } from "./generated/btc_vault.js";
//# sourceMappingURL=index.js.map