"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.FILL_RELAY_DISCRIMINATOR = void 0;
exports.getFillRelayDiscriminatorBytes = getFillRelayDiscriminatorBytes;
exports.getFillRelayInstructionDataEncoder = getFillRelayInstructionDataEncoder;
exports.getFillRelayInstructionDataDecoder = getFillRelayInstructionDataDecoder;
exports.getFillRelayInstructionDataCodec = getFillRelayInstructionDataCodec;
exports.getFillRelayInstructionAsync = getFillRelayInstructionAsync;
exports.getFillRelayInstruction = getFillRelayInstruction;
exports.parseFillRelayInstruction = parseFillRelayInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
const types_1 = require("../types");
exports.FILL_RELAY_DISCRIMINATOR = new Uint8Array([
    100, 84, 222, 90, 106, 209, 58, 222,
]);
function getFillRelayDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.FILL_RELAY_DISCRIMINATOR);
}
function getFillRelayInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['relayHash', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
        ['relayData', (0, kit_1.getOptionEncoder)((0, types_1.getRelayDataEncoder)())],
        ['repaymentChainId', (0, kit_1.getOptionEncoder)((0, kit_1.getU64Encoder)())],
        ['repaymentAddress', (0, kit_1.getOptionEncoder)((0, kit_1.getAddressEncoder)())],
    ]), (value) => ({ ...value, discriminator: exports.FILL_RELAY_DISCRIMINATOR }));
}
function getFillRelayInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['relayHash', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
        ['relayData', (0, kit_1.getOptionDecoder)((0, types_1.getRelayDataDecoder)())],
        ['repaymentChainId', (0, kit_1.getOptionDecoder)((0, kit_1.getU64Decoder)())],
        ['repaymentAddress', (0, kit_1.getOptionDecoder)((0, kit_1.getAddressDecoder)())],
    ]);
}
function getFillRelayInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getFillRelayInstructionDataEncoder(), getFillRelayInstructionDataDecoder());
}
async function getFillRelayInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        instructionParams: {
            value: input.instructionParams ?? null,
            isWritable: true,
        },
        state: { value: input.state ?? null, isWritable: false },
        delegate: { value: input.delegate ?? null, isWritable: false },
        mint: { value: input.mint ?? null, isWritable: false },
        relayerTokenAccount: {
            value: input.relayerTokenAccount ?? null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: input.recipientTokenAccount ?? null,
            isWritable: true,
        },
        fillStatus: { value: input.fillStatus ?? null, isWritable: true },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        associatedTokenProgram: {
            value: input.associatedTokenProgram ?? null,
            isWritable: false,
        },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.instructionParams.value) {
        accounts.instructionParams.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    105, 110, 115, 116, 114, 117, 99, 116, 105, 111, 110, 95, 112, 97,
                    114, 97, 109, 115,
                ])),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.signer.value)),
            ],
        });
    }
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.recipientTokenAccount.value) {
        accounts.recipientTokenAccount.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getOptionEncoder)((0, types_1.getRelayDataEncoder)()).encode((0, shared_1.expectSome)(args.relayData)),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
            ],
        });
    }
    if (!accounts.fillStatus.value) {
        accounts.fillStatus.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([102, 105, 108, 108, 115])),
                (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32).encode((0, shared_1.expectSome)(args.relayHash)),
            ],
        });
    }
    if (!accounts.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    if (!accounts.eventAuthority.value) {
        accounts.eventAuthority.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    95, 95, 101, 118, 101, 110, 116, 95, 97, 117, 116, 104, 111, 114,
                    105, 116, 121,
                ])),
            ],
        });
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.instructionParams),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.delegate),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.relayerTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.fillStatus),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getFillRelayInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getFillRelayInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        instructionParams: {
            value: input.instructionParams ?? null,
            isWritable: true,
        },
        state: { value: input.state ?? null, isWritable: false },
        delegate: { value: input.delegate ?? null, isWritable: false },
        mint: { value: input.mint ?? null, isWritable: false },
        relayerTokenAccount: {
            value: input.relayerTokenAccount ?? null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: input.recipientTokenAccount ?? null,
            isWritable: true,
        },
        fillStatus: { value: input.fillStatus ?? null, isWritable: true },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        associatedTokenProgram: {
            value: input.associatedTokenProgram ?? null,
            isWritable: false,
        },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
    }
    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.state),
            getAccountMeta(accounts.delegate),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.relayerTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.fillStatus),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getFillRelayInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseFillRelayInstruction(instruction) {
    if (instruction.accounts.length < 13) {
        // TODO: Coded error.
        throw new Error('Not enough accounts');
    }
    let accountIndex = 0;
    const getNextAccount = () => {
        const accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    const getNextOptionalAccount = () => {
        const accountMeta = getNextAccount();
        return accountMeta.address === programs_1.SVM_SPOKE_PROGRAM_ADDRESS
            ? undefined
            : accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            signer: getNextAccount(),
            instructionParams: getNextOptionalAccount(),
            state: getNextAccount(),
            delegate: getNextAccount(),
            mint: getNextAccount(),
            relayerTokenAccount: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            fillStatus: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getFillRelayInstructionDataDecoder().decode(instruction.data),
    };
}
