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;
};
import { PublicKey, SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, SystemProgram, } from "@solana/web3.js";
import { createReadOnlyWormholeProgramInterface } from "../program";
import { deriveWormholeBridgeDataKey, deriveGuardianSetKey, derivePostedVaaKey, } from "../accounts";
import { isBytes, parseVaa } from "../../../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
 */
export function createPostVaaInstruction(wormholeProgramId, payer, vaa, signatureSet) {
    var parsed = isBytes(vaa) ? parseVaa(vaa) : vaa;
    var methods = 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,
        }]));
}
export function getPostVaaAccounts(wormholeProgramId, payer, signatureSet, vaa) {
    var parsed = isBytes(vaa) ? parseVaa(vaa) : vaa;
    return {
        guardianSet: deriveGuardianSetKey(wormholeProgramId, parsed.guardianSetIndex),
        bridge: deriveWormholeBridgeDataKey(wormholeProgramId),
        signatureSet: new PublicKey(signatureSet),
        vaa: derivePostedVaaKey(wormholeProgramId, parsed.hash),
        payer: new PublicKey(payer),
        clock: SYSVAR_CLOCK_PUBKEY,
        rent: SYSVAR_RENT_PUBKEY,
        systemProgram: SystemProgram.programId,
    };
}
