"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.getHandleReceiveMessageInstructionAsync = getHandleReceiveMessageInstructionAsync;
exports.getHandleReceiveMessageInstruction = getHandleReceiveMessageInstruction;
exports.parseHandleReceiveMessageInstruction = parseHandleReceiveMessageInstruction;
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");
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 getHandleReceiveMessageInstructionAsync(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;
        return tslib_1.__generator(this, function (_h) {
            switch (_h.label) {
                case 0:
                    programAddress = (_c = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _c !== void 0 ? _c : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        authorityPda: { value: (_d = input.authorityPda) !== null && _d !== void 0 ? _d : null, isWritable: false },
                        state: { value: (_e = input.state) !== null && _e !== void 0 ? _e : null, isWritable: false },
                        selfAuthority: { value: (_f = input.selfAuthority) !== null && _f !== void 0 ? _f : null, isWritable: false },
                        program: { value: (_g = input.program) !== null && _g !== void 0 ? _g : 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: "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd",
                            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([
                                    183, 102, 64, 134, 222, 55, 238, 112, 130, 28, 16, 68, 91, 22, 47,
                                    44, 126, 200, 121, 91, 208, 128, 12, 20, 98, 148, 158, 35, 40, 209,
                                    221, 90,
                                ])),
                            ],
                        })];
                case 1:
                    _a.value = _h.sent();
                    _h.label = 2;
                case 2:
                    if (!!accounts.selfAuthority.value) return [3, 4];
                    _b = accounts.selfAuthority;
                    return [4, (0, pdas_1.findSelfAuthorityPda)()];
                case 3:
                    _b.value = _h.sent();
                    _h.label = 4;
                case 4:
                    if (!accounts.program.value) {
                        accounts.program.value =
                            "DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru";
                    }
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.authorityPda),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.selfAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getHandleReceiveMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    var originalAccounts = {
        authorityPda: { value: (_b = input.authorityPda) !== null && _b !== void 0 ? _b : null, isWritable: false },
        state: { value: (_c = input.state) !== null && _c !== void 0 ? _c : null, isWritable: false },
        selfAuthority: { value: (_d = input.selfAuthority) !== null && _d !== void 0 ? _d : null, isWritable: false },
        program: { value: (_e = input.program) !== null && _e !== void 0 ? _e : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    if (!accounts.program.value) {
        accounts.program.value =
            "DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru";
    }
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.selfAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseHandleReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 4) {
        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(),
            state: getNextAccount(),
            selfAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getHandleReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=handleReceiveMessage.js.map