"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR = void 0;
exports.getHandleReceiveMessageDiscriminatorBytes = getHandleReceiveMessageDiscriminatorBytes;
exports.getHandleReceiveMessageInstructionDataEncoder = getHandleReceiveMessageInstructionDataEncoder;
exports.getHandleReceiveMessageInstructionDataDecoder = getHandleReceiveMessageInstructionDataDecoder;
exports.getHandleReceiveMessageInstructionDataCodec = getHandleReceiveMessageInstructionDataCodec;
exports.getHandleReceiveMessageInstruction = getHandleReceiveMessageInstruction;
exports.parseHandleReceiveMessageInstruction = parseHandleReceiveMessageInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR = new Uint8Array([
    133, 102, 1, 180, 145, 11, 138, 180,
]);
function getHandleReceiveMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR);
}
function getHandleReceiveMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["remoteDomain", (0, kit_1.getU32Encoder)()],
        ["sender", (0, kit_1.getAddressEncoder)()],
        ["messageBody", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
        ["authorityBump", (0, kit_1.getU8Encoder)()],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR })); });
}
function getHandleReceiveMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["remoteDomain", (0, kit_1.getU32Decoder)()],
        ["sender", (0, kit_1.getAddressDecoder)()],
        ["messageBody", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
        ["authorityBump", (0, kit_1.getU8Decoder)()],
    ]);
}
function getHandleReceiveMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getHandleReceiveMessageInstructionDataEncoder(), getHandleReceiveMessageInstructionDataDecoder());
}
function getHandleReceiveMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS;
    var originalAccounts = {
        authorityPda: { value: (_b = input.authorityPda) !== null && _b !== void 0 ? _b : null, isWritable: false },
        tokenMessenger: { value: (_c = input.tokenMessenger) !== null && _c !== void 0 ? _c : null, isWritable: false },
        remoteTokenMessenger: {
            value: (_d = input.remoteTokenMessenger) !== null && _d !== void 0 ? _d : null,
            isWritable: false,
        },
        tokenMinter: { value: (_e = input.tokenMinter) !== null && _e !== void 0 ? _e : null, isWritable: false },
        localToken: { value: (_f = input.localToken) !== null && _f !== void 0 ? _f : null, isWritable: true },
        tokenPair: { value: (_g = input.tokenPair) !== null && _g !== void 0 ? _g : null, isWritable: false },
        recipientTokenAccount: {
            value: (_h = input.recipientTokenAccount) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        custodyTokenAccount: {
            value: (_j = input.custodyTokenAccount) !== null && _j !== void 0 ? _j : null,
            isWritable: true,
        },
        tokenProgram: { value: (_k = input.tokenProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        eventAuthority: { value: (_l = input.eventAuthority) !== null && _l !== void 0 ? _l : null, isWritable: false },
        program: { value: (_m = input.program) !== null && _m !== void 0 ? _m : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.tokenMessenger),
            getAccountMeta(accounts.remoteTokenMessenger),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.tokenPair),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.custodyTokenAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseHandleReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        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: {
            authorityPda: getNextAccount(),
            tokenMessenger: getNextAccount(),
            remoteTokenMessenger: getNextAccount(),
            tokenMinter: getNextAccount(),
            localToken: getNextAccount(),
            tokenPair: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            custodyTokenAccount: getNextAccount(),
            tokenProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getHandleReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=handleReceiveMessage.js.map