"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPostMessageAccounts = exports.createPostMessageInstruction = void 0;
const web3_js_1 = require("@solana/web3.js");
const accounts_1 = require("../accounts");
const program_1 = require("../program");
function createPostMessageInstruction(connection, wormholeProgramId, payer, messageAccount, payload, nonce, consistencyLevel) {
    const methods = (0, program_1.createReadOnlyWormholeProgramInterface)(wormholeProgramId, connection).methods.postMessage(nonce, Buffer.from(payload), consistencyLevel);
    // @ts-ignore
    return methods._ixFn(...methods._args, {
        accounts: getPostMessageAccounts(wormholeProgramId, payer, messageAccount),
        signers: undefined,
        remainingAccounts: undefined,
        preInstructions: undefined,
        postInstructions: undefined,
    });
}
exports.createPostMessageInstruction = createPostMessageInstruction;
function getPostMessageAccounts(wormholeProgramId, payer, message, emitter) {
    let sequence;
    if (emitter) {
        ({ emitter, sequence } = (0, accounts_1.getEmitterKeys)(emitter, wormholeProgramId));
    }
    else {
        emitter = payer;
        sequence = (0, accounts_1.deriveEmitterSequenceKey)(emitter, wormholeProgramId);
    }
    return {
        bridge: (0, accounts_1.deriveWormholeBridgeDataKey)(wormholeProgramId),
        message: new web3_js_1.PublicKey(message),
        emitter: new web3_js_1.PublicKey(emitter),
        sequence,
        payer: new web3_js_1.PublicKey(payer),
        feeCollector: (0, accounts_1.deriveFeeCollectorKey)(wormholeProgramId),
        clock: web3_js_1.SYSVAR_CLOCK_PUBKEY,
        rent: web3_js_1.SYSVAR_RENT_PUBKEY,
        systemProgram: web3_js_1.SystemProgram.programId,
    };
}
exports.getPostMessageAccounts = getPostMessageAccounts;
//# sourceMappingURL=postMessage.js.map