/**
 * 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,
  getAddressDecoder,
  getAddressEncoder,
  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 { findStatePda } from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const SET_SIGNER_DISCRIMINATOR = new Uint8Array([
  127, 120, 252, 184, 97, 4, 88, 68,
]);

export function getSetSignerDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(SET_SIGNER_DISCRIMINATOR);
}

export type SetSignerInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountProgramData extends string | AccountMeta<string> = string,
  TAccountThisProgram extends string | AccountMeta<string> =
    "FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountState extends string
        ? WritableAccount<TAccountState>
        : TAccountState,
      TAccountProgramData extends string
        ? ReadonlyAccount<TAccountProgramData>
        : TAccountProgramData,
      TAccountThisProgram extends string
        ? ReadonlyAccount<TAccountThisProgram>
        : TAccountThisProgram,
      ...TRemainingAccounts,
    ]
  >;

export type SetSignerInstructionData = {
  discriminator: ReadonlyUint8Array;
  newSigner: Address;
};

export type SetSignerInstructionDataArgs = { newSigner: Address };

export function getSetSignerInstructionDataEncoder(): FixedSizeEncoder<SetSignerInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newSigner", getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: SET_SIGNER_DISCRIMINATOR }),
  );
}

export function getSetSignerInstructionDataDecoder(): FixedSizeDecoder<SetSignerInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newSigner", getAddressDecoder()],
  ]);
}

export function getSetSignerInstructionDataCodec(): FixedSizeCodec<
  SetSignerInstructionDataArgs,
  SetSignerInstructionData
> {
  return combineCodec(
    getSetSignerInstructionDataEncoder(),
    getSetSignerInstructionDataDecoder(),
  );
}

export type SetSignerAsyncInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  state?: Address<TAccountState>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  newSigner: SetSignerInstructionDataArgs["newSigner"];
};

export async function getSetSignerInstructionAsync<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: SetSignerAsyncInput<
    TAccountSigner,
    TAccountState,
    TAccountProgramData,
    TAccountThisProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  SetSignerInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountProgramData,
    TAccountThisProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

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

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.state.value) {
    accounts.state.value = await findStatePda();
  }
  if (!accounts.thisProgram.value) {
    accounts.thisProgram.value =
      "FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS" as Address<"FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
    ],
    data: getSetSignerInstructionDataEncoder().encode(
      args as SetSignerInstructionDataArgs,
    ),
    programAddress,
  } as SetSignerInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountProgramData,
    TAccountThisProgram
  >);
}

export type SetSignerInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  state: Address<TAccountState>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  newSigner: SetSignerInstructionDataArgs["newSigner"];
};

export function getSetSignerInstruction<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: SetSignerInput<
    TAccountSigner,
    TAccountState,
    TAccountProgramData,
    TAccountThisProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetSignerInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountState,
  TAccountProgramData,
  TAccountThisProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

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

  // Original args.
  const args = { ...input };

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
    ],
    data: getSetSignerInstructionDataEncoder().encode(
      args as SetSignerInstructionDataArgs,
    ),
    programAddress,
  } as SetSignerInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountProgramData,
    TAccountThisProgram
  >);
}

export type ParsedSetSignerInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    state: TAccountMetas[1];
    programData: TAccountMetas[2];
    thisProgram: TAccountMetas[3];
  };
  data: SetSignerInstructionData;
};

export function parseSetSignerInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetSignerInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 4) {
    // 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(),
      programData: getNextAccount(),
      thisProgram: getNextAccount(),
    },
    data: getSetSignerInstructionDataDecoder().decode(instruction.data),
  };
}
