import { PublicKey, SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, SystemProgram, } from '@solana/web3.js';
import { deriveWormholeBridgeDataKey, deriveFeeCollectorKey, deriveEmitterSequenceKey, getEmitterKeys, } from '../accounts';
import { createReadOnlyWormholeProgramInterface } from '../program';
export function createPostMessageInstruction(connection, wormholeProgramId, payer, messageAccount, payload, nonce, consistencyLevel) {
    const methods = 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,
    });
}
export function getPostMessageAccounts(wormholeProgramId, payer, message, emitter) {
    let sequence;
    if (emitter) {
        ({ emitter, sequence } = getEmitterKeys(emitter, wormholeProgramId));
    }
    else {
        emitter = payer;
        sequence = deriveEmitterSequenceKey(emitter, wormholeProgramId);
    }
    return {
        bridge: deriveWormholeBridgeDataKey(wormholeProgramId),
        message: new PublicKey(message),
        emitter: new PublicKey(emitter),
        sequence,
        payer: new PublicKey(payer),
        feeCollector: deriveFeeCollectorKey(wormholeProgramId),
        clock: SYSVAR_CLOCK_PUBKEY,
        rent: SYSVAR_RENT_PUBKEY,
        systemProgram: SystemProgram.programId,
    };
}
//# sourceMappingURL=postMessage.js.map