/**
 * 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 { findClaimAccountPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";

export const INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR = new Uint8Array([
  22, 247, 214, 191, 90, 74, 87, 216,
]);

export function getInitializeClaimAccountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR,
  );
}

export type InitializeClaimAccountInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountMint extends string | AccountMeta<string> = string,
  TAccountRefundAddress extends string | AccountMeta<string> = string,
  TAccountClaimAccount extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountMint extends string
        ? ReadonlyAccount<TAccountMint>
        : TAccountMint,
      TAccountRefundAddress extends string
        ? ReadonlyAccount<TAccountRefundAddress>
        : TAccountRefundAddress,
      TAccountClaimAccount extends string
        ? WritableAccount<TAccountClaimAccount>
        : TAccountClaimAccount,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      ...TRemainingAccounts,
    ]
  >;

export type InitializeClaimAccountInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type InitializeClaimAccountInstructionDataArgs = {};

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

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

export function getInitializeClaimAccountInstructionDataCodec(): FixedSizeCodec<
  InitializeClaimAccountInstructionDataArgs,
  InitializeClaimAccountInstructionData
> {
  return combineCodec(
    getInitializeClaimAccountInstructionDataEncoder(),
    getInitializeClaimAccountInstructionDataDecoder(),
  );
}

export type InitializeClaimAccountAsyncInput<
  TAccountSigner extends string = string,
  TAccountMint extends string = string,
  TAccountRefundAddress extends string = string,
  TAccountClaimAccount extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  mint: Address<TAccountMint>;
  refundAddress: Address<TAccountRefundAddress>;
  claimAccount?: Address<TAccountClaimAccount>;
  systemProgram?: Address<TAccountSystemProgram>;
};

export async function getInitializeClaimAccountInstructionAsync<
  TAccountSigner extends string,
  TAccountMint extends string,
  TAccountRefundAddress extends string,
  TAccountClaimAccount extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: InitializeClaimAccountAsyncInput<
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  InitializeClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount,
    TAccountSystemProgram
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

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

  // Resolve default values.
  if (!accounts.claimAccount.value) {
    accounts.claimAccount.value = await findClaimAccountPda({
      mint: expectAddress(accounts.mint.value),
      refundAddress: expectAddress(accounts.refundAddress.value),
    });
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.refundAddress),
      getAccountMeta(accounts.claimAccount),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getInitializeClaimAccountInstructionDataEncoder().encode({}),
    programAddress,
  } as InitializeClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount,
    TAccountSystemProgram
  >);
}

export type InitializeClaimAccountInput<
  TAccountSigner extends string = string,
  TAccountMint extends string = string,
  TAccountRefundAddress extends string = string,
  TAccountClaimAccount extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  mint: Address<TAccountMint>;
  refundAddress: Address<TAccountRefundAddress>;
  claimAccount: Address<TAccountClaimAccount>;
  systemProgram?: Address<TAccountSystemProgram>;
};

export function getInitializeClaimAccountInstruction<
  TAccountSigner extends string,
  TAccountMint extends string,
  TAccountRefundAddress extends string,
  TAccountClaimAccount extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: InitializeClaimAccountInput<
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): InitializeClaimAccountInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountMint,
  TAccountRefundAddress,
  TAccountClaimAccount,
  TAccountSystemProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

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

  // Resolve default values.
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.refundAddress),
      getAccountMeta(accounts.claimAccount),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getInitializeClaimAccountInstructionDataEncoder().encode({}),
    programAddress,
  } as InitializeClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount,
    TAccountSystemProgram
  >);
}

export type ParsedInitializeClaimAccountInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    mint: TAccountMetas[1];
    refundAddress: TAccountMetas[2];
    claimAccount: TAccountMetas[3];
    systemProgram: TAccountMetas[4];
  };
  data: InitializeClaimAccountInstructionData;
};

export function parseInitializeClaimAccountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedInitializeClaimAccountInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 5) {
    // 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(),
      mint: getNextAccount(),
      refundAddress: getNextAccount(),
      claimAccount: getNextAccount(),
      systemProgram: getNextAccount(),
    },
    data: getInitializeClaimAccountInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
