/**
 * This code was AUTOGENERATED using the Codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun Codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */
import { __assign, __awaiter, __generator } from "tslib";
import { addDecoderSizePrefix, addEncoderSizePrefix, combineCodec, fixDecoderSize, fixEncoderSize, getAddressDecoder, getAddressEncoder, getBytesDecoder, getBytesEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, getU8Decoder, getU8Encoder, transformEncoder, } from "@solana/kit";
import { findSelfAuthorityPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory } from "../shared";
export var HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR = new Uint8Array([
    133, 102, 1, 180, 145, 11, 138, 180,
]);
export function getHandleReceiveMessageDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR);
}
export function getHandleReceiveMessageInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["remoteDomain", getU32Encoder()],
        ["sender", getAddressEncoder()],
        ["messageBody", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
        ["authorityBump", getU8Encoder()],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR })); });
}
export function getHandleReceiveMessageInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["remoteDomain", getU32Decoder()],
        ["sender", getAddressDecoder()],
        ["messageBody", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
        ["authorityBump", getU8Decoder()],
    ]);
}
export function getHandleReceiveMessageInstructionDataCodec() {
    return combineCodec(getHandleReceiveMessageInstructionDataEncoder(), getHandleReceiveMessageInstructionDataDecoder());
}
export function getHandleReceiveMessageInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, getAccountMeta;
        var _c, _d, _e, _f, _g;
        return __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 : 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 = __assign({}, input);
                    if (!!accounts.authorityPda.value) return [3 /*break*/, 2];
                    _a = accounts.authorityPda;
                    return [4 /*yield*/, getProgramDerivedAddress({
                            programAddress: "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd",
                            seeds: [
                                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,
                                ])),
                                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 /*break*/, 4];
                    _b = accounts.selfAuthority;
                    return [4 /*yield*/, findSelfAuthorityPda()];
                case 3:
                    _b.value = _h.sent();
                    _h.label = 4;
                case 4:
                    if (!accounts.program.value) {
                        accounts.program.value =
                            "DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru";
                    }
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.authorityPda),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.selfAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getHandleReceiveMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e;
    // Program address.
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    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;
    // Original args.
    var args = __assign({}, input);
    // Resolve default values.
    if (!accounts.program.value) {
        accounts.program.value =
            "DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru";
    }
    var getAccountMeta = 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,
    });
}
export function parseHandleReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 4) {
        // TODO: Coded error.
        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