/**
 * 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,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import {
  findEventAuthorityPda,
  findSenderAuthorityPdaPda,
  findTokenMessengerPda,
  findTokenMinterPda,
} from "../pdas";
import { TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const INITIALIZE_DISCRIMINATOR = new Uint8Array([
  175, 175, 109, 31, 13, 152, 155, 237,
]);

export function getInitializeDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(INITIALIZE_DISCRIMINATOR);
}

export type InitializeInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountUpgradeAuthority extends string | AccountMeta<string> = string,
  TAccountAuthorityPda extends string | AccountMeta<string> = string,
  TAccountTokenMessenger extends string | AccountMeta<string> = string,
  TAccountTokenMinter extends string | AccountMeta<string> = string,
  TAccountTokenMessengerMinterProgramData extends string | AccountMeta<string> =
    string,
  TAccountTokenMessengerMinterProgram extends string | AccountMeta<string> =
    "CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe",
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountUpgradeAuthority extends string
        ? ReadonlySignerAccount<TAccountUpgradeAuthority> &
            AccountSignerMeta<TAccountUpgradeAuthority>
        : TAccountUpgradeAuthority,
      TAccountAuthorityPda extends string
        ? ReadonlyAccount<TAccountAuthorityPda>
        : TAccountAuthorityPda,
      TAccountTokenMessenger extends string
        ? WritableAccount<TAccountTokenMessenger>
        : TAccountTokenMessenger,
      TAccountTokenMinter extends string
        ? WritableAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountTokenMessengerMinterProgramData extends string
        ? ReadonlyAccount<TAccountTokenMessengerMinterProgramData>
        : TAccountTokenMessengerMinterProgramData,
      TAccountTokenMessengerMinterProgram extends string
        ? ReadonlyAccount<TAccountTokenMessengerMinterProgram>
        : TAccountTokenMessengerMinterProgram,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type InitializeInstructionData = {
  discriminator: ReadonlyUint8Array;
  tokenController: Address;
  denylister: Address;
  feeRecipient: Address;
  minFeeController: Address;
  minFee: number;
  messageBodyVersion: number;
};

export type InitializeInstructionDataArgs = {
  tokenController: Address;
  denylister: Address;
  feeRecipient: Address;
  minFeeController: Address;
  minFee: number;
  messageBodyVersion: number;
};

export function getInitializeInstructionDataEncoder(): FixedSizeEncoder<InitializeInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["tokenController", getAddressEncoder()],
      ["denylister", getAddressEncoder()],
      ["feeRecipient", getAddressEncoder()],
      ["minFeeController", getAddressEncoder()],
      ["minFee", getU32Encoder()],
      ["messageBodyVersion", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: INITIALIZE_DISCRIMINATOR }),
  );
}

export function getInitializeInstructionDataDecoder(): FixedSizeDecoder<InitializeInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["tokenController", getAddressDecoder()],
    ["denylister", getAddressDecoder()],
    ["feeRecipient", getAddressDecoder()],
    ["minFeeController", getAddressDecoder()],
    ["minFee", getU32Decoder()],
    ["messageBodyVersion", getU32Decoder()],
  ]);
}

export function getInitializeInstructionDataCodec(): FixedSizeCodec<
  InitializeInstructionDataArgs,
  InitializeInstructionData
> {
  return combineCodec(
    getInitializeInstructionDataEncoder(),
    getInitializeInstructionDataDecoder(),
  );
}

export type InitializeAsyncInput<
  TAccountPayer extends string = string,
  TAccountUpgradeAuthority extends string = string,
  TAccountAuthorityPda extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountTokenMessengerMinterProgramData extends string = string,
  TAccountTokenMessengerMinterProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  upgradeAuthority: TransactionSigner<TAccountUpgradeAuthority>;
  authorityPda?: Address<TAccountAuthorityPda>;
  tokenMessenger?: Address<TAccountTokenMessenger>;
  tokenMinter?: Address<TAccountTokenMinter>;
  tokenMessengerMinterProgramData: Address<TAccountTokenMessengerMinterProgramData>;
  tokenMessengerMinterProgram?: Address<TAccountTokenMessengerMinterProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  tokenController: InitializeInstructionDataArgs["tokenController"];
  denylister: InitializeInstructionDataArgs["denylister"];
  feeRecipient: InitializeInstructionDataArgs["feeRecipient"];
  minFeeController: InitializeInstructionDataArgs["minFeeController"];
  minFee: InitializeInstructionDataArgs["minFee"];
  messageBodyVersion: InitializeInstructionDataArgs["messageBodyVersion"];
};

export async function getInitializeInstructionAsync<
  TAccountPayer extends string,
  TAccountUpgradeAuthority extends string,
  TAccountAuthorityPda extends string,
  TAccountTokenMessenger extends string,
  TAccountTokenMinter extends string,
  TAccountTokenMessengerMinterProgramData extends string,
  TAccountTokenMessengerMinterProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: InitializeAsyncInput<
    TAccountPayer,
    TAccountUpgradeAuthority,
    TAccountAuthorityPda,
    TAccountTokenMessenger,
    TAccountTokenMinter,
    TAccountTokenMessengerMinterProgramData,
    TAccountTokenMessengerMinterProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  InitializeInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountUpgradeAuthority,
    TAccountAuthorityPda,
    TAccountTokenMessenger,
    TAccountTokenMinter,
    TAccountTokenMessengerMinterProgramData,
    TAccountTokenMessengerMinterProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    upgradeAuthority: {
      value: input.upgradeAuthority ?? null,
      isWritable: false,
    },
    authorityPda: { value: input.authorityPda ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: true },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: true },
    tokenMessengerMinterProgramData: {
      value: input.tokenMessengerMinterProgramData ?? null,
      isWritable: false,
    },
    tokenMessengerMinterProgram: {
      value: input.tokenMessengerMinterProgram ?? 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 as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  // Resolve default values.
  if (!accounts.authorityPda.value) {
    accounts.authorityPda.value = await findSenderAuthorityPdaPda();
  }
  if (!accounts.tokenMessenger.value) {
    accounts.tokenMessenger.value = await findTokenMessengerPda();
  }
  if (!accounts.tokenMinter.value) {
    accounts.tokenMinter.value = await findTokenMinterPda();
  }
  if (!accounts.tokenMessengerMinterProgram.value) {
    accounts.tokenMessengerMinterProgram.value =
      "CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe" as Address<"CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.upgradeAuthority),
      getAccountMeta(accounts.authorityPda),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.tokenMessengerMinterProgramData),
      getAccountMeta(accounts.tokenMessengerMinterProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getInitializeInstructionDataEncoder().encode(
      args as InitializeInstructionDataArgs,
    ),
    programAddress,
  } as InitializeInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountUpgradeAuthority,
    TAccountAuthorityPda,
    TAccountTokenMessenger,
    TAccountTokenMinter,
    TAccountTokenMessengerMinterProgramData,
    TAccountTokenMessengerMinterProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type InitializeInput<
  TAccountPayer extends string = string,
  TAccountUpgradeAuthority extends string = string,
  TAccountAuthorityPda extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountTokenMessengerMinterProgramData extends string = string,
  TAccountTokenMessengerMinterProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  upgradeAuthority: TransactionSigner<TAccountUpgradeAuthority>;
  authorityPda: Address<TAccountAuthorityPda>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  tokenMinter: Address<TAccountTokenMinter>;
  tokenMessengerMinterProgramData: Address<TAccountTokenMessengerMinterProgramData>;
  tokenMessengerMinterProgram?: Address<TAccountTokenMessengerMinterProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  tokenController: InitializeInstructionDataArgs["tokenController"];
  denylister: InitializeInstructionDataArgs["denylister"];
  feeRecipient: InitializeInstructionDataArgs["feeRecipient"];
  minFeeController: InitializeInstructionDataArgs["minFeeController"];
  minFee: InitializeInstructionDataArgs["minFee"];
  messageBodyVersion: InitializeInstructionDataArgs["messageBodyVersion"];
};

export function getInitializeInstruction<
  TAccountPayer extends string,
  TAccountUpgradeAuthority extends string,
  TAccountAuthorityPda extends string,
  TAccountTokenMessenger extends string,
  TAccountTokenMinter extends string,
  TAccountTokenMessengerMinterProgramData extends string,
  TAccountTokenMessengerMinterProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: InitializeInput<
    TAccountPayer,
    TAccountUpgradeAuthority,
    TAccountAuthorityPda,
    TAccountTokenMessenger,
    TAccountTokenMinter,
    TAccountTokenMessengerMinterProgramData,
    TAccountTokenMessengerMinterProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): InitializeInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountUpgradeAuthority,
  TAccountAuthorityPda,
  TAccountTokenMessenger,
  TAccountTokenMinter,
  TAccountTokenMessengerMinterProgramData,
  TAccountTokenMessengerMinterProgram,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    upgradeAuthority: {
      value: input.upgradeAuthority ?? null,
      isWritable: false,
    },
    authorityPda: { value: input.authorityPda ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: true },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: true },
    tokenMessengerMinterProgramData: {
      value: input.tokenMessengerMinterProgramData ?? null,
      isWritable: false,
    },
    tokenMessengerMinterProgram: {
      value: input.tokenMessengerMinterProgram ?? 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 as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.upgradeAuthority),
      getAccountMeta(accounts.authorityPda),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.tokenMessengerMinterProgramData),
      getAccountMeta(accounts.tokenMessengerMinterProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getInitializeInstructionDataEncoder().encode(
      args as InitializeInstructionDataArgs,
    ),
    programAddress,
  } as InitializeInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountUpgradeAuthority,
    TAccountAuthorityPda,
    TAccountTokenMessenger,
    TAccountTokenMinter,
    TAccountTokenMessengerMinterProgramData,
    TAccountTokenMessengerMinterProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedInitializeInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payer: TAccountMetas[0];
    upgradeAuthority: TAccountMetas[1];
    authorityPda: TAccountMetas[2];
    tokenMessenger: TAccountMetas[3];
    tokenMinter: TAccountMetas[4];
    tokenMessengerMinterProgramData: TAccountMetas[5];
    tokenMessengerMinterProgram: TAccountMetas[6];
    systemProgram: TAccountMetas[7];
    eventAuthority: TAccountMetas[8];
    program: TAccountMetas[9];
  };
  data: InitializeInstructionData;
};

export function parseInitializeInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedInitializeInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 10) {
    // 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: {
      payer: getNextAccount(),
      upgradeAuthority: getNextAccount(),
      authorityPda: getNextAccount(),
      tokenMessenger: getNextAccount(),
      tokenMinter: getNextAccount(),
      tokenMessengerMinterProgramData: getNextAccount(),
      tokenMessengerMinterProgram: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getInitializeInstructionDataDecoder().decode(instruction.data),
  };
}
