"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.BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR = void 0;
exports.getBridgeTokensToHubPoolDiscriminatorBytes = getBridgeTokensToHubPoolDiscriminatorBytes;
exports.getBridgeTokensToHubPoolInstructionDataEncoder = getBridgeTokensToHubPoolInstructionDataEncoder;
exports.getBridgeTokensToHubPoolInstructionDataDecoder = getBridgeTokensToHubPoolInstructionDataDecoder;
exports.getBridgeTokensToHubPoolInstructionDataCodec = getBridgeTokensToHubPoolInstructionDataCodec;
exports.getBridgeTokensToHubPoolInstructionAsync = getBridgeTokensToHubPoolInstructionAsync;
exports.getBridgeTokensToHubPoolInstruction = getBridgeTokensToHubPoolInstruction;
exports.parseBridgeTokensToHubPoolInstruction = parseBridgeTokensToHubPoolInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR = new Uint8Array([
    1, 83, 255, 59, 232, 55, 64, 216,
]);
function getBridgeTokensToHubPoolDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR);
}
function getBridgeTokensToHubPoolInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['amount', (0, kit_1.getU64Encoder)()],
    ]), (value) => ({
        ...value,
        discriminator: exports.BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR,
    }));
}
function getBridgeTokensToHubPoolInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['amount', (0, kit_1.getU64Decoder)()],
    ]);
}
function getBridgeTokensToHubPoolInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getBridgeTokensToHubPoolInstructionDataEncoder(), getBridgeTokensToHubPoolInstructionDataDecoder());
}
async function getBridgeTokensToHubPoolInstructionAsync(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 },
        payer: { value: input.payer ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: false },
        transferLiability: {
            value: input.transferLiability ?? null,
            isWritable: true,
        },
        vault: { value: input.vault ?? null, isWritable: true },
        tokenMessengerMinterSenderAuthority: {
            value: input.tokenMessengerMinterSenderAuthority ?? null,
            isWritable: false,
        },
        messageTransmitter: {
            value: input.messageTransmitter ?? null,
            isWritable: true,
        },
        tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
        remoteTokenMessenger: {
            value: input.remoteTokenMessenger ?? null,
            isWritable: false,
        },
        tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
        localToken: { value: input.localToken ?? null, isWritable: true },
        cctpEventAuthority: {
            value: input.cctpEventAuthority ?? null,
            isWritable: false,
        },
        messageSentEventData: {
            value: input.messageSentEventData ?? null,
            isWritable: true,
        },
        messageTransmitterProgram: {
            value: input.messageTransmitterProgram ?? null,
            isWritable: false,
        },
        tokenMessengerMinterProgram: {
            value: input.tokenMessengerMinterProgram ?? null,
            isWritable: false,
        },
        tokenProgram: { value: input.tokenProgram ?? 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.transferLiability.value) {
        accounts.transferLiability.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    116, 114, 97, 110, 115, 102, 101, 114, 95, 108, 105, 97, 98, 105,
                    108, 105, 116, 121,
                ])),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
            ],
        });
    }
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.vault.value) {
        accounts.vault.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.state.value)),
                (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.messageTransmitterProgram.value) {
        accounts.messageTransmitterProgram.value =
            'CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd';
    }
    if (!accounts.tokenMessengerMinterProgram.value) {
        accounts.tokenMessengerMinterProgram.value =
            'CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3';
    }
    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.payer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.transferLiability),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.tokenMessengerMinterSenderAuthority),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.tokenMessenger),
            getAccountMeta(accounts.remoteTokenMessenger),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.cctpEventAuthority),
            getAccountMeta(accounts.messageSentEventData),
            getAccountMeta(accounts.messageTransmitterProgram),
            getAccountMeta(accounts.tokenMessengerMinterProgram),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getBridgeTokensToHubPoolInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getBridgeTokensToHubPoolInstruction(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 },
        payer: { value: input.payer ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: false },
        transferLiability: {
            value: input.transferLiability ?? null,
            isWritable: true,
        },
        vault: { value: input.vault ?? null, isWritable: true },
        tokenMessengerMinterSenderAuthority: {
            value: input.tokenMessengerMinterSenderAuthority ?? null,
            isWritable: false,
        },
        messageTransmitter: {
            value: input.messageTransmitter ?? null,
            isWritable: true,
        },
        tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
        remoteTokenMessenger: {
            value: input.remoteTokenMessenger ?? null,
            isWritable: false,
        },
        tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
        localToken: { value: input.localToken ?? null, isWritable: true },
        cctpEventAuthority: {
            value: input.cctpEventAuthority ?? null,
            isWritable: false,
        },
        messageSentEventData: {
            value: input.messageSentEventData ?? null,
            isWritable: true,
        },
        messageTransmitterProgram: {
            value: input.messageTransmitterProgram ?? null,
            isWritable: false,
        },
        tokenMessengerMinterProgram: {
            value: input.tokenMessengerMinterProgram ?? null,
            isWritable: false,
        },
        tokenProgram: { value: input.tokenProgram ?? 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.messageTransmitterProgram.value) {
        accounts.messageTransmitterProgram.value =
            'CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd';
    }
    if (!accounts.tokenMessengerMinterProgram.value) {
        accounts.tokenMessengerMinterProgram.value =
            'CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.transferLiability),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.tokenMessengerMinterSenderAuthority),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.tokenMessenger),
            getAccountMeta(accounts.remoteTokenMessenger),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.cctpEventAuthority),
            getAccountMeta(accounts.messageSentEventData),
            getAccountMeta(accounts.messageTransmitterProgram),
            getAccountMeta(accounts.tokenMessengerMinterProgram),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getBridgeTokensToHubPoolInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseBridgeTokensToHubPoolInstruction(instruction) {
    if (instruction.accounts.length < 20) {
        // 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(),
            payer: getNextAccount(),
            mint: getNextAccount(),
            state: getNextAccount(),
            transferLiability: getNextAccount(),
            vault: getNextAccount(),
            tokenMessengerMinterSenderAuthority: getNextAccount(),
            messageTransmitter: getNextAccount(),
            tokenMessenger: getNextAccount(),
            remoteTokenMessenger: getNextAccount(),
            tokenMinter: getNextAccount(),
            localToken: getNextAccount(),
            cctpEventAuthority: getNextAccount(),
            messageSentEventData: getNextAccount(),
            messageTransmitterProgram: getNextAccount(),
            tokenMessengerMinterProgram: getNextAccount(),
            tokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getBridgeTokensToHubPoolInstructionDataDecoder().decode(instruction.data),
    };
}
