"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HANDLE_RECEIVE_FINALIZED_MESSAGE_DISCRIMINATOR = void 0;
exports.getHandleReceiveFinalizedMessageDiscriminatorBytes = getHandleReceiveFinalizedMessageDiscriminatorBytes;
exports.getHandleReceiveFinalizedMessageInstructionDataEncoder = getHandleReceiveFinalizedMessageInstructionDataEncoder;
exports.getHandleReceiveFinalizedMessageInstructionDataDecoder = getHandleReceiveFinalizedMessageInstructionDataDecoder;
exports.getHandleReceiveFinalizedMessageInstructionDataCodec = getHandleReceiveFinalizedMessageInstructionDataCodec;
exports.getHandleReceiveFinalizedMessageInstructionAsync = getHandleReceiveFinalizedMessageInstructionAsync;
exports.getHandleReceiveFinalizedMessageInstruction = getHandleReceiveFinalizedMessageInstruction;
exports.parseHandleReceiveFinalizedMessageInstruction = parseHandleReceiveFinalizedMessageInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var pdas_1 = require("../pdas");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
var types_1 = require("../types");
exports.HANDLE_RECEIVE_FINALIZED_MESSAGE_DISCRIMINATOR = new Uint8Array([
    186, 252, 239, 70, 86, 180, 110, 95,
]);
function getHandleReceiveFinalizedMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.HANDLE_RECEIVE_FINALIZED_MESSAGE_DISCRIMINATOR);
}
function getHandleReceiveFinalizedMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["params", (0, types_1.getHandleReceiveMessageParamsEncoder)()],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.HANDLE_RECEIVE_FINALIZED_MESSAGE_DISCRIMINATOR })); });
}
function getHandleReceiveFinalizedMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["params", (0, types_1.getHandleReceiveMessageParamsDecoder)()],
    ]);
}
function getHandleReceiveFinalizedMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getHandleReceiveFinalizedMessageInstructionDataEncoder(), getHandleReceiveFinalizedMessageInstructionDataDecoder());
}
function getHandleReceiveFinalizedMessageInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, getAccountMeta;
        var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
        return tslib_1.__generator(this, function (_r) {
            switch (_r.label) {
                case 0:
                    programAddress = (_c = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _c !== void 0 ? _c : programs_1.TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
                    originalAccounts = {
                        authorityPda: { value: (_d = input.authorityPda) !== null && _d !== void 0 ? _d : null, isWritable: false },
                        tokenMessenger: { value: (_e = input.tokenMessenger) !== null && _e !== void 0 ? _e : null, isWritable: false },
                        remoteTokenMessenger: {
                            value: (_f = input.remoteTokenMessenger) !== null && _f !== void 0 ? _f : null,
                            isWritable: false,
                        },
                        tokenMinter: { value: (_g = input.tokenMinter) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        localToken: { value: (_h = input.localToken) !== null && _h !== void 0 ? _h : null, isWritable: true },
                        tokenPair: { value: (_j = input.tokenPair) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        feeRecipientTokenAccount: {
                            value: (_k = input.feeRecipientTokenAccount) !== null && _k !== void 0 ? _k : null,
                            isWritable: true,
                        },
                        recipientTokenAccount: {
                            value: (_l = input.recipientTokenAccount) !== null && _l !== void 0 ? _l : null,
                            isWritable: true,
                        },
                        custodyTokenAccount: {
                            value: (_m = input.custodyTokenAccount) !== null && _m !== void 0 ? _m : null,
                            isWritable: true,
                        },
                        tokenProgram: { value: (_o = input.tokenProgram) !== null && _o !== void 0 ? _o : null, isWritable: false },
                        eventAuthority: { value: (_p = input.eventAuthority) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        program: { value: (_q = input.program) !== null && _q !== void 0 ? _q : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    if (!!accounts.authorityPda.value) return [3, 2];
                    _a = accounts.authorityPda;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC",
                            seeds: [
                                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                                    109, 101, 115, 115, 97, 103, 101, 95, 116, 114, 97, 110, 115, 109,
                                    105, 116, 116, 101, 114, 95, 97, 117, 116, 104, 111, 114, 105, 116,
                                    121,
                                ])),
                                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                                    166, 95, 200, 29, 15, 239, 168, 134, 12, 179, 184, 63, 8, 155, 2,
                                    36, 190, 138, 102, 135, 183, 174, 73, 245, 148, 192, 185, 180, 215,
                                    233, 56, 147,
                                ])),
                            ],
                        })];
                case 1:
                    _a.value = _r.sent();
                    _r.label = 2;
                case 2:
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 4];
                    _b = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 3:
                    _b.value = _r.sent();
                    _r.label = 4;
                case 4:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.authorityPda),
                                getAccountMeta(accounts.tokenMessenger),
                                getAccountMeta(accounts.remoteTokenMessenger),
                                getAccountMeta(accounts.tokenMinter),
                                getAccountMeta(accounts.localToken),
                                getAccountMeta(accounts.tokenPair),
                                getAccountMeta(accounts.feeRecipientTokenAccount),
                                getAccountMeta(accounts.recipientTokenAccount),
                                getAccountMeta(accounts.custodyTokenAccount),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getHandleReceiveFinalizedMessageInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getHandleReceiveFinalizedMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.TOKEN_MESSENGER_MINTER_V2_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 },
        feeRecipientTokenAccount: {
            value: (_h = input.feeRecipientTokenAccount) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: (_j = input.recipientTokenAccount) !== null && _j !== void 0 ? _j : null,
            isWritable: true,
        },
        custodyTokenAccount: {
            value: (_k = input.custodyTokenAccount) !== null && _k !== void 0 ? _k : null,
            isWritable: true,
        },
        tokenProgram: { value: (_l = input.tokenProgram) !== null && _l !== void 0 ? _l : null, isWritable: false },
        eventAuthority: { value: (_m = input.eventAuthority) !== null && _m !== void 0 ? _m : null, isWritable: false },
        program: { value: (_o = input.program) !== null && _o !== void 0 ? _o : 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.feeRecipientTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.custodyTokenAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getHandleReceiveFinalizedMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseHandleReceiveFinalizedMessageInstruction(instruction) {
    if (instruction.accounts.length < 12) {
        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(),
            feeRecipientTokenAccount: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            custodyTokenAccount: getNextAccount(),
            tokenProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getHandleReceiveFinalizedMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=handleReceiveFinalizedMessage.js.map