/**
 * 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 ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { findInstructionParamsPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";

export const CLOSE_INSTRUCTION_PARAMS_DISCRIMINATOR = new Uint8Array([
  224, 44, 254, 10, 216, 8, 172, 96,
]);

export function getCloseInstructionParamsDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    CLOSE_INSTRUCTION_PARAMS_DISCRIMINATOR,
  );
}

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

export type CloseInstructionParamsInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type CloseInstructionParamsInstructionDataArgs = {};

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

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

export function getCloseInstructionParamsInstructionDataCodec(): FixedSizeCodec<
  CloseInstructionParamsInstructionDataArgs,
  CloseInstructionParamsInstructionData
> {
  return combineCodec(
    getCloseInstructionParamsInstructionDataEncoder(),
    getCloseInstructionParamsInstructionDataDecoder(),
  );
}

export type CloseInstructionParamsAsyncInput<
  TAccountSigner extends string = string,
  TAccountInstructionParams extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  instructionParams?: Address<TAccountInstructionParams>;
};

export async function getCloseInstructionParamsInstructionAsync<
  TAccountSigner extends string,
  TAccountInstructionParams extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: CloseInstructionParamsAsyncInput<
    TAccountSigner,
    TAccountInstructionParams
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  CloseInstructionParamsInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountInstructionParams
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

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

  // Resolve default values.
  if (!accounts.instructionParams.value) {
    accounts.instructionParams.value = await findInstructionParamsPda({
      signer: expectAddress(accounts.signer.value),
    });
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.instructionParams),
    ],
    data: getCloseInstructionParamsInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseInstructionParamsInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountInstructionParams
  >);
}

export type CloseInstructionParamsInput<
  TAccountSigner extends string = string,
  TAccountInstructionParams extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  instructionParams: Address<TAccountInstructionParams>;
};

export function getCloseInstructionParamsInstruction<
  TAccountSigner extends string,
  TAccountInstructionParams extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: CloseInstructionParamsInput<TAccountSigner, TAccountInstructionParams>,
  config?: { programAddress?: TProgramAddress },
): CloseInstructionParamsInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountInstructionParams
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    instructionParams: {
      value: input.instructionParams ?? 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.instructionParams),
    ],
    data: getCloseInstructionParamsInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseInstructionParamsInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountInstructionParams
  >);
}

export type ParsedCloseInstructionParamsInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    instructionParams: TAccountMetas[1];
  };
  data: CloseInstructionParamsInstructionData;
};

export function parseCloseInstructionParamsInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedCloseInstructionParamsInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 2) {
    // 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(), instructionParams: getNextAccount() },
    data: getCloseInstructionParamsInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
