/**
 * 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, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, transformEncoder, } from "@solana/kit";
import { findAuthorityPdaPda, findEventAuthorityPda } from "../pdas";
import { MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, getAccountMetaFactory, } from "../shared";
export var RECEIVE_MESSAGE_DISCRIMINATOR = new Uint8Array([
    38, 144, 127, 225, 31, 225, 238, 25,
]);
export function getReceiveMessageDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(RECEIVE_MESSAGE_DISCRIMINATOR);
}
export function getReceiveMessageInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["message", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
        ["attestation", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: RECEIVE_MESSAGE_DISCRIMINATOR })); });
}
export function getReceiveMessageInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["message", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
        ["attestation", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
    ]);
}
export function getReceiveMessageInstructionDataCodec() {
    return combineCodec(getReceiveMessageInstructionDataEncoder(), getReceiveMessageInstructionDataDecoder());
}
export function getReceiveMessageInstructionAsync(input, config) {
    return __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;
        return __generator(this, function (_o) {
            switch (_o.label) {
                case 0:
                    programAddress = (_c = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _c !== void 0 ? _c : MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
                    originalAccounts = {
                        payer: { value: (_d = input.payer) !== null && _d !== void 0 ? _d : null, isWritable: true },
                        caller: { value: (_e = input.caller) !== null && _e !== void 0 ? _e : null, isWritable: false },
                        authorityPda: { value: (_f = input.authorityPda) !== null && _f !== void 0 ? _f : null, isWritable: false },
                        messageTransmitter: {
                            value: (_g = input.messageTransmitter) !== null && _g !== void 0 ? _g : null,
                            isWritable: false,
                        },
                        usedNonce: { value: (_h = input.usedNonce) !== null && _h !== void 0 ? _h : null, isWritable: true },
                        receiver: { value: (_j = input.receiver) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        systemProgram: { value: (_k = input.systemProgram) !== 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 },
                    };
                    accounts = originalAccounts;
                    args = __assign({}, input);
                    if (!!accounts.authorityPda.value) return [3 /*break*/, 2];
                    _a = accounts.authorityPda;
                    return [4 /*yield*/, findAuthorityPdaPda({
                            receiver: expectAddress(accounts.receiver.value),
                        })];
                case 1:
                    _a.value = _o.sent();
                    _o.label = 2;
                case 2:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 4];
                    _b = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 3:
                    _b.value = _o.sent();
                    _o.label = 4;
                case 4:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.payer),
                                getAccountMeta(accounts.caller),
                                getAccountMeta(accounts.authorityPda),
                                getAccountMeta(accounts.messageTransmitter),
                                getAccountMeta(accounts.usedNonce),
                                getAccountMeta(accounts.receiver),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getReceiveMessageInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getReceiveMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    // Program address.
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    var originalAccounts = {
        payer: { value: (_b = input.payer) !== null && _b !== void 0 ? _b : null, isWritable: true },
        caller: { value: (_c = input.caller) !== null && _c !== void 0 ? _c : null, isWritable: false },
        authorityPda: { value: (_d = input.authorityPda) !== null && _d !== void 0 ? _d : null, isWritable: false },
        messageTransmitter: {
            value: (_e = input.messageTransmitter) !== null && _e !== void 0 ? _e : null,
            isWritable: false,
        },
        usedNonce: { value: (_f = input.usedNonce) !== null && _f !== void 0 ? _f : null, isWritable: true },
        receiver: { value: (_g = input.receiver) !== null && _g !== void 0 ? _g : null, isWritable: false },
        systemProgram: { value: (_h = input.systemProgram) !== null && _h !== void 0 ? _h : null, isWritable: false },
        eventAuthority: { value: (_j = input.eventAuthority) !== null && _j !== void 0 ? _j : null, isWritable: false },
        program: { value: (_k = input.program) !== null && _k !== void 0 ? _k : null, isWritable: false },
    };
    var accounts = originalAccounts;
    // Original args.
    var args = __assign({}, input);
    // Resolve default values.
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = getAccountMetaFactory(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.caller),
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.usedNonce),
            getAccountMeta(accounts.receiver),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getReceiveMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
export function parseReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 9) {
        // 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: {
            payer: getNextAccount(),
            caller: getNextAccount(),
            authorityPda: getNextAccount(),
            messageTransmitter: getNextAccount(),
            usedNonce: getNextAccount(),
            receiver: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=receiveMessage.js.map