"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPostVaaAccounts = exports.createPostVaaInstruction = void 0;
var web3_js_1 = require("@solana/web3.js");
var program_1 = require("../program");
var accounts_1 = require("../accounts");
var vaa_1 = require("../../../vaa");
/**
 * Make {@link TransactionInstruction} for `post_vaa` instruction.
 *
 * This is used in {@link createPostSignedVaaTransactions}'s last transaction.
 * `signatureSet` is a {@link web3.Keypair} generated outside of this method, which was used
 * to write signatures and the message hash to.
 *
 * https://github.com/certusone/wormhole/blob/main/solana/bridge/program/src/api/post_vaa.rs
 *
 * @param {PublicKeyInitData} wormholeProgramId - wormhole program address
 * @param {PublicKeyInitData} payer - transaction signer address
 * @param {SignedVaa | ParsedVaa} vaa - either signed VAA bytes or parsed VAA (use {@link parseVaa} on signed VAA)
 * @param {PublicKeyInitData} signatureSet - key for signature set account
 */
function createPostVaaInstruction(wormholeProgramId, payer, vaa, signatureSet) {
    var parsed = vaa_1.isBytes(vaa) ? vaa_1.parseVaa(vaa) : vaa;
    var methods = program_1.createReadOnlyWormholeProgramInterface(wormholeProgramId).methods.postVaa(parsed.version, parsed.guardianSetIndex, parsed.timestamp, parsed.nonce, parsed.emitterChain, parsed.emitterAddress, parsed.sequence, parsed.consistencyLevel, parsed.payload);
    // @ts-ignore
    return methods._ixFn.apply(methods, __spreadArray(__spreadArray([], __read(methods._args)), [{
            accounts: getPostVaaAccounts(wormholeProgramId, payer, signatureSet, parsed),
            signers: undefined,
            remainingAccounts: undefined,
            preInstructions: undefined,
            postInstructions: undefined,
        }]));
}
exports.createPostVaaInstruction = createPostVaaInstruction;
function getPostVaaAccounts(wormholeProgramId, payer, signatureSet, vaa) {
    var parsed = vaa_1.isBytes(vaa) ? vaa_1.parseVaa(vaa) : vaa;
    return {
        guardianSet: accounts_1.deriveGuardianSetKey(wormholeProgramId, parsed.guardianSetIndex),
        bridge: accounts_1.deriveWormholeBridgeDataKey(wormholeProgramId),
        signatureSet: new web3_js_1.PublicKey(signatureSet),
        vaa: accounts_1.derivePostedVaaKey(wormholeProgramId, parsed.hash),
        payer: new web3_js_1.PublicKey(payer),
        clock: web3_js_1.SYSVAR_CLOCK_PUBKEY,
        rent: web3_js_1.SYSVAR_RENT_PUBKEY,
        systemProgram: web3_js_1.SystemProgram.programId,
    };
}
exports.getPostVaaAccounts = getPostVaaAccounts;
