import { pollAsync } from '@hyperlane-xyz/utils';
import { createDeployProgramPlan, executeDeployPlan, } from './program-deployer.js';
async function waitForProgramDeployment(rpc, programAddress) {
    await pollAsync(async () => {
        const account = await rpc
            .getAccountInfo(programAddress, { encoding: 'base64' })
            .send();
        if (!account.value) {
            throw new Error(`Program ${programAddress} not yet visible`);
        }
    }, 1000, 30);
}
export async function resolveProgram(target, signer, rpc, useExactDataLen) {
    if ('programId' in target) {
        return { programAddress: target.programId, receipts: [] };
    }
    const plan = await createDeployProgramPlan({
        payer: signer.signer,
        programBytes: target.programBytes,
        getMinimumBalanceForRentExemption: (size) => rpc.getMinimumBalanceForRentExemption(BigInt(size)).send(),
        // Exact data len minimizes rent cost. Programs can still be upgraded to
        // the same size binary. Use the 2x default (undefined) if future upgrades
        // may grow the binary beyond the current size.
        maxDataLen: useExactDataLen
            ? BigInt(target.programBytes.length)
            : undefined,
    });
    const executeStage = async (stage) => signer.send({
        instructions: stage.instructions,
        additionalSigners: stage.additionalSigners,
    });
    const receipts = await executeDeployPlan({ plan, executeStage });
    // Some SVM chains acknowledge deployment before the new program can be
    // immediately queried or invoked. Wait until the program account is visible
    // before returning so follow-up init/config txs do not race the deploy.
    await waitForProgramDeployment(rpc, plan.programAddress);
    return { programAddress: plan.programAddress, receipts };
}
//# sourceMappingURL=resolve-program.js.map