/**
 * 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 {
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const CLOSE_FILL_PDA_DISCRIMINATOR = new Uint8Array([
  224, 39, 208, 68, 8, 226, 23, 214,
]);

export function getCloseFillPdaDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    CLOSE_FILL_PDA_DISCRIMINATOR,
  );
}

export type CloseFillPdaInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountFillStatus extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountState extends string
        ? ReadonlyAccount<TAccountState>
        : TAccountState,
      TAccountFillStatus extends string
        ? WritableAccount<TAccountFillStatus>
        : TAccountFillStatus,
      ...TRemainingAccounts,
    ]
  >;

export type CloseFillPdaInstructionData = { discriminator: ReadonlyUint8Array };

export type CloseFillPdaInstructionDataArgs = {};

export function getCloseFillPdaInstructionDataEncoder(): FixedSizeEncoder<CloseFillPdaInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]),
    (value) => ({ ...value, discriminator: CLOSE_FILL_PDA_DISCRIMINATOR }),
  );
}

export function getCloseFillPdaInstructionDataDecoder(): FixedSizeDecoder<CloseFillPdaInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
  ]);
}

export function getCloseFillPdaInstructionDataCodec(): FixedSizeCodec<
  CloseFillPdaInstructionDataArgs,
  CloseFillPdaInstructionData
> {
  return combineCodec(
    getCloseFillPdaInstructionDataEncoder(),
    getCloseFillPdaInstructionDataDecoder(),
  );
}

export type CloseFillPdaInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountFillStatus extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  state: Address<TAccountState>;
  fillStatus: Address<TAccountFillStatus>;
};

export function getCloseFillPdaInstruction<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountFillStatus extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: CloseFillPdaInput<TAccountSigner, TAccountState, TAccountFillStatus>,
  config?: { programAddress?: TProgramAddress },
): CloseFillPdaInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountState,
  TAccountFillStatus
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    fillStatus: { value: input.fillStatus ?? null, isWritable: true },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.fillStatus),
    ],
    data: getCloseFillPdaInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseFillPdaInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountFillStatus
  >);
}

export type ParsedCloseFillPdaInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    state: TAccountMetas[1];
    fillStatus: TAccountMetas[2];
  };
  data: CloseFillPdaInstructionData;
};

export function parseCloseFillPdaInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedCloseFillPdaInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 3) {
    // TODO: Coded error.
    throw new Error("Not enough accounts");
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      signer: getNextAccount(),
      state: getNextAccount(),
      fillStatus: getNextAccount(),
    },
    data: getCloseFillPdaInstructionDataDecoder().decode(instruction.data),
  };
}
