"use strict";
/**
 * 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR = void 0;
exports.getWriteInstructionParamsFragmentDiscriminatorBytes = getWriteInstructionParamsFragmentDiscriminatorBytes;
exports.getWriteInstructionParamsFragmentInstructionDataEncoder = getWriteInstructionParamsFragmentInstructionDataEncoder;
exports.getWriteInstructionParamsFragmentInstructionDataDecoder = getWriteInstructionParamsFragmentInstructionDataDecoder;
exports.getWriteInstructionParamsFragmentInstructionDataCodec = getWriteInstructionParamsFragmentInstructionDataCodec;
exports.getWriteInstructionParamsFragmentInstructionAsync = getWriteInstructionParamsFragmentInstructionAsync;
exports.getWriteInstructionParamsFragmentInstruction = getWriteInstructionParamsFragmentInstruction;
exports.parseWriteInstructionParamsFragmentInstruction = parseWriteInstructionParamsFragmentInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR = new Uint8Array([
    238, 182, 109, 113, 124, 255, 72, 18,
]);
function getWriteInstructionParamsFragmentDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR);
}
function getWriteInstructionParamsFragmentInstructionDataEncoder() {
    return (0, web3_v2_js_1.transformEncoder)((0, web3_v2_js_1.getStructEncoder)([
        ['discriminator', (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8)],
        ['offset', (0, web3_v2_js_1.getU32Encoder)()],
        ['fragment', (0, web3_v2_js_1.addEncoderSizePrefix)((0, web3_v2_js_1.getBytesEncoder)(), (0, web3_v2_js_1.getU32Encoder)())],
    ]), (value) => ({
        ...value,
        discriminator: exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR,
    }));
}
function getWriteInstructionParamsFragmentInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
        ['offset', (0, web3_v2_js_1.getU32Decoder)()],
        ['fragment', (0, web3_v2_js_1.addDecoderSizePrefix)((0, web3_v2_js_1.getBytesDecoder)(), (0, web3_v2_js_1.getU32Decoder)())],
    ]);
}
function getWriteInstructionParamsFragmentInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getWriteInstructionParamsFragmentInstructionDataEncoder(), getWriteInstructionParamsFragmentInstructionDataDecoder());
}
async function getWriteInstructionParamsFragmentInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        instructionParams: {
            value: input.instructionParams ?? null,
            isWritable: true,
        },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.instructionParams.value) {
        accounts.instructionParams.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getBytesEncoder)().encode(new Uint8Array([
                    105, 110, 115, 116, 114, 117, 99, 116, 105, 111, 110, 95, 112, 97,
                    114, 97, 109, 115,
                ])),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.signer.value)),
            ],
        });
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.instructionParams),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getWriteInstructionParamsFragmentInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getWriteInstructionParamsFragmentInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        instructionParams: {
            value: input.instructionParams ?? null,
            isWritable: true,
        },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.instructionParams),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getWriteInstructionParamsFragmentInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseWriteInstructionParamsFragmentInstruction(instruction) {
    if (instruction.accounts.length < 3) {
        // TODO: Coded error.
        throw new Error('Not enough accounts');
    }
    let accountIndex = 0;
    const getNextAccount = () => {
        const accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            signer: getNextAccount(),
            instructionParams: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getWriteInstructionParamsFragmentInstructionDataDecoder().decode(instruction.data),
    };
}
