"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.INITIALIZE_DISCRIMINATOR = void 0;
exports.getInitializeDiscriminatorBytes = getInitializeDiscriminatorBytes;
exports.getInitializeInstructionDataEncoder = getInitializeInstructionDataEncoder;
exports.getInitializeInstructionDataDecoder = getInitializeInstructionDataDecoder;
exports.getInitializeInstructionDataCodec = getInitializeInstructionDataCodec;
exports.getInitializeInstructionAsync = getInitializeInstructionAsync;
exports.getInitializeInstruction = getInitializeInstruction;
exports.parseInitializeInstruction = parseInitializeInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.INITIALIZE_DISCRIMINATOR = new Uint8Array([
    175, 175, 109, 31, 13, 152, 155, 237,
]);
function getInitializeDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.INITIALIZE_DISCRIMINATOR);
}
function getInitializeInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['sourceDomain', (0, kit_1.getU32Encoder)()],
        ['signer', (0, kit_1.getAddressEncoder)()],
    ]), (value) => ({ ...value, discriminator: exports.INITIALIZE_DISCRIMINATOR }));
}
function getInitializeInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['sourceDomain', (0, kit_1.getU32Decoder)()],
        ['signer', (0, kit_1.getAddressDecoder)()],
    ]);
}
function getInitializeInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getInitializeInstructionDataEncoder(), getInitializeInstructionDataDecoder());
}
async function getInitializeInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        programData: { value: input.programData ?? null, isWritable: false },
        thisProgram: { value: input.thisProgram ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input, signer: input.signerArg };
    // Resolve default values.
    if (!accounts.state.value) {
        accounts.state.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([115, 116, 97, 116, 101])),
            ],
        });
    }
    if (!accounts.thisProgram.value) {
        accounts.thisProgram.value =
            'CPr4bRvkVKcSCLyrQpkZrRrwGzQeVAXutFU8WupuBLXq';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.programData),
            getAccountMeta(accounts.thisProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getInitializeInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getInitializeInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        programData: { value: input.programData ?? null, isWritable: false },
        thisProgram: { value: input.thisProgram ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input, signer: input.signerArg };
    // Resolve default values.
    if (!accounts.thisProgram.value) {
        accounts.thisProgram.value =
            'CPr4bRvkVKcSCLyrQpkZrRrwGzQeVAXutFU8WupuBLXq';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.programData),
            getAccountMeta(accounts.thisProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getInitializeInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseInitializeInstruction(instruction) {
    if (instruction.accounts.length < 5) {
        // 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(),
            state: getNextAccount(),
            programData: getNextAccount(),
            thisProgram: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getInitializeInstructionDataDecoder().decode(instruction.data),
    };
}
