"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SEND_MESSAGE_DISCRIMINATOR = void 0;
exports.getSendMessageDiscriminatorBytes = getSendMessageDiscriminatorBytes;
exports.getSendMessageInstructionDataEncoder = getSendMessageInstructionDataEncoder;
exports.getSendMessageInstructionDataDecoder = getSendMessageInstructionDataDecoder;
exports.getSendMessageInstructionDataCodec = getSendMessageInstructionDataCodec;
exports.getSendMessageInstruction = getSendMessageInstruction;
exports.parseSendMessageInstruction = parseSendMessageInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.SEND_MESSAGE_DISCRIMINATOR = new Uint8Array([
    57, 40, 34, 178, 189, 10, 65, 26,
]);
function getSendMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.SEND_MESSAGE_DISCRIMINATOR);
}
function getSendMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["destinationDomain", (0, kit_1.getU32Encoder)()],
        ["recipient", (0, kit_1.getAddressEncoder)()],
        ["destinationCaller", (0, kit_1.getAddressEncoder)()],
        ["minFinalityThreshold", (0, kit_1.getU32Encoder)()],
        ["messageBody", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.SEND_MESSAGE_DISCRIMINATOR })); });
}
function getSendMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["destinationDomain", (0, kit_1.getU32Decoder)()],
        ["recipient", (0, kit_1.getAddressDecoder)()],
        ["destinationCaller", (0, kit_1.getAddressDecoder)()],
        ["minFinalityThreshold", (0, kit_1.getU32Decoder)()],
        ["messageBody", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
    ]);
}
function getSendMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getSendMessageInstructionDataEncoder(), getSendMessageInstructionDataDecoder());
}
function getSendMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    var originalAccounts = {
        eventRentPayer: { value: (_b = input.eventRentPayer) !== null && _b !== void 0 ? _b : null, isWritable: true },
        senderAuthorityPda: {
            value: (_c = input.senderAuthorityPda) !== null && _c !== void 0 ? _c : null,
            isWritable: false,
        },
        messageTransmitter: {
            value: (_d = input.messageTransmitter) !== null && _d !== void 0 ? _d : null,
            isWritable: true,
        },
        messageSentEventData: {
            value: (_e = input.messageSentEventData) !== null && _e !== void 0 ? _e : null,
            isWritable: true,
        },
        senderProgram: { value: (_f = input.senderProgram) !== null && _f !== void 0 ? _f : null, isWritable: false },
        systemProgram: { value: (_g = input.systemProgram) !== null && _g !== void 0 ? _g : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.eventRentPayer),
            getAccountMeta(accounts.senderAuthorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.messageSentEventData),
            getAccountMeta(accounts.senderProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        data: getSendMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseSendMessageInstruction(instruction) {
    if (instruction.accounts.length < 6) {
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            eventRentPayer: getNextAccount(),
            senderAuthorityPda: getNextAccount(),
            messageTransmitter: getNextAccount(),
            messageSentEventData: getNextAccount(),
            senderProgram: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getSendMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=sendMessage.js.map