/**
 * 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,
  getAddressEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getProgramDerivedAddress,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type AccountMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
} from '@solana/kit';
import { WEN_TRANSFER_GUARD_PROGRAM_ADDRESS } from '../programs';
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from '../shared';

export const EXECUTE_DISCRIMINATOR = new Uint8Array([
  105, 37, 101, 197, 75, 251, 102, 26,
]);

export function getExecuteDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(EXECUTE_DISCRIMINATOR);
}

export type ExecuteInstruction<
  TProgram extends string = typeof WEN_TRANSFER_GUARD_PROGRAM_ADDRESS,
  TAccountSourceAccount extends string | AccountMeta<string> = string,
  TAccountMint extends string | AccountMeta<string> = string,
  TAccountDestinationAccount extends string | AccountMeta<string> = string,
  TAccountOwnerDelegate extends string | AccountMeta<string> = string,
  TAccountExtraMetasAccount extends string | AccountMeta<string> = string,
  TAccountGuard extends string | AccountMeta<string> = string,
  TAccountInstructionSysvarAccount extends
    | string
    | AccountMeta<string> = 'Sysvar1nstructions1111111111111111111111111',
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSourceAccount extends string
        ? ReadonlyAccount<TAccountSourceAccount>
        : TAccountSourceAccount,
      TAccountMint extends string
        ? ReadonlyAccount<TAccountMint>
        : TAccountMint,
      TAccountDestinationAccount extends string
        ? ReadonlyAccount<TAccountDestinationAccount>
        : TAccountDestinationAccount,
      TAccountOwnerDelegate extends string
        ? ReadonlyAccount<TAccountOwnerDelegate>
        : TAccountOwnerDelegate,
      TAccountExtraMetasAccount extends string
        ? ReadonlyAccount<TAccountExtraMetasAccount>
        : TAccountExtraMetasAccount,
      TAccountGuard extends string
        ? ReadonlyAccount<TAccountGuard>
        : TAccountGuard,
      TAccountInstructionSysvarAccount extends string
        ? ReadonlyAccount<TAccountInstructionSysvarAccount>
        : TAccountInstructionSysvarAccount,
      ...TRemainingAccounts,
    ]
  >;

export type ExecuteInstructionData = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
};

export type ExecuteInstructionDataArgs = { amount: number | bigint };

export function getExecuteInstructionDataEncoder(): FixedSizeEncoder<ExecuteInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ['discriminator', fixEncoderSize(getBytesEncoder(), 8)],
      ['amount', getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: EXECUTE_DISCRIMINATOR })
  ) as FixedSizeEncoder<ExecuteInstructionDataArgs>;
}

export function getExecuteInstructionDataDecoder(): FixedSizeDecoder<ExecuteInstructionData> {
  return getStructDecoder([
    ['discriminator', fixDecoderSize(getBytesDecoder(), 8)],
    ['amount', getU64Decoder()],
  ]) as FixedSizeDecoder<ExecuteInstructionData>;
}

export function getExecuteInstructionDataCodec(): FixedSizeCodec<
  ExecuteInstructionDataArgs,
  ExecuteInstructionData
> {
  return combineCodec(
    getExecuteInstructionDataEncoder(),
    getExecuteInstructionDataDecoder()
  ) as FixedSizeCodec<ExecuteInstructionDataArgs, ExecuteInstructionData>;
}

export type ExecuteAsyncInput<
  TAccountSourceAccount extends string = string,
  TAccountMint extends string = string,
  TAccountDestinationAccount extends string = string,
  TAccountOwnerDelegate extends string = string,
  TAccountExtraMetasAccount extends string = string,
  TAccountGuard extends string = string,
  TAccountInstructionSysvarAccount extends string = string,
> = {
  sourceAccount: Address<TAccountSourceAccount>;
  mint: Address<TAccountMint>;
  destinationAccount: Address<TAccountDestinationAccount>;
  ownerDelegate: Address<TAccountOwnerDelegate>;
  extraMetasAccount?: Address<TAccountExtraMetasAccount>;
  guard: Address<TAccountGuard>;
  instructionSysvarAccount?: Address<TAccountInstructionSysvarAccount>;
  amount: ExecuteInstructionDataArgs['amount'];
};

export async function getExecuteInstructionAsync<
  TAccountSourceAccount extends string,
  TAccountMint extends string,
  TAccountDestinationAccount extends string,
  TAccountOwnerDelegate extends string,
  TAccountExtraMetasAccount extends string,
  TAccountGuard extends string,
  TAccountInstructionSysvarAccount extends string,
  TProgramAddress extends Address = typeof WEN_TRANSFER_GUARD_PROGRAM_ADDRESS,
>(
  input: ExecuteAsyncInput<
    TAccountSourceAccount,
    TAccountMint,
    TAccountDestinationAccount,
    TAccountOwnerDelegate,
    TAccountExtraMetasAccount,
    TAccountGuard,
    TAccountInstructionSysvarAccount
  >,
  config?: { programAddress?: TProgramAddress }
): Promise<
  ExecuteInstruction<
    TProgramAddress,
    TAccountSourceAccount,
    TAccountMint,
    TAccountDestinationAccount,
    TAccountOwnerDelegate,
    TAccountExtraMetasAccount,
    TAccountGuard,
    TAccountInstructionSysvarAccount
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? WEN_TRANSFER_GUARD_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    sourceAccount: { value: input.sourceAccount ?? null, isWritable: false },
    mint: { value: input.mint ?? null, isWritable: false },
    destinationAccount: {
      value: input.destinationAccount ?? null,
      isWritable: false,
    },
    ownerDelegate: { value: input.ownerDelegate ?? null, isWritable: false },
    extraMetasAccount: {
      value: input.extraMetasAccount ?? null,
      isWritable: false,
    },
    guard: { value: input.guard ?? null, isWritable: false },
    instructionSysvarAccount: {
      value: input.instructionSysvarAccount ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  // Resolve default values.
  if (!accounts.extraMetasAccount.value) {
    accounts.extraMetasAccount.value = await getProgramDerivedAddress({
      programAddress,
      seeds: [
        getBytesEncoder().encode(
          new Uint8Array([
            101, 120, 116, 114, 97, 45, 97, 99, 99, 111, 117, 110, 116, 45, 109,
            101, 116, 97, 115,
          ])
        ),
        getAddressEncoder().encode(expectAddress(accounts.mint.value)),
      ],
    });
  }
  if (!accounts.instructionSysvarAccount.value) {
    accounts.instructionSysvarAccount.value =
      'Sysvar1nstructions1111111111111111111111111' as Address<'Sysvar1nstructions1111111111111111111111111'>;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  const instruction = {
    accounts: [
      getAccountMeta(accounts.sourceAccount),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.destinationAccount),
      getAccountMeta(accounts.ownerDelegate),
      getAccountMeta(accounts.extraMetasAccount),
      getAccountMeta(accounts.guard),
      getAccountMeta(accounts.instructionSysvarAccount),
    ],
    programAddress,
    data: getExecuteInstructionDataEncoder().encode(
      args as ExecuteInstructionDataArgs
    ),
  } as ExecuteInstruction<
    TProgramAddress,
    TAccountSourceAccount,
    TAccountMint,
    TAccountDestinationAccount,
    TAccountOwnerDelegate,
    TAccountExtraMetasAccount,
    TAccountGuard,
    TAccountInstructionSysvarAccount
  >;

  return instruction;
}

export type ExecuteInput<
  TAccountSourceAccount extends string = string,
  TAccountMint extends string = string,
  TAccountDestinationAccount extends string = string,
  TAccountOwnerDelegate extends string = string,
  TAccountExtraMetasAccount extends string = string,
  TAccountGuard extends string = string,
  TAccountInstructionSysvarAccount extends string = string,
> = {
  sourceAccount: Address<TAccountSourceAccount>;
  mint: Address<TAccountMint>;
  destinationAccount: Address<TAccountDestinationAccount>;
  ownerDelegate: Address<TAccountOwnerDelegate>;
  extraMetasAccount: Address<TAccountExtraMetasAccount>;
  guard: Address<TAccountGuard>;
  instructionSysvarAccount?: Address<TAccountInstructionSysvarAccount>;
  amount: ExecuteInstructionDataArgs['amount'];
};

export function getExecuteInstruction<
  TAccountSourceAccount extends string,
  TAccountMint extends string,
  TAccountDestinationAccount extends string,
  TAccountOwnerDelegate extends string,
  TAccountExtraMetasAccount extends string,
  TAccountGuard extends string,
  TAccountInstructionSysvarAccount extends string,
  TProgramAddress extends Address = typeof WEN_TRANSFER_GUARD_PROGRAM_ADDRESS,
>(
  input: ExecuteInput<
    TAccountSourceAccount,
    TAccountMint,
    TAccountDestinationAccount,
    TAccountOwnerDelegate,
    TAccountExtraMetasAccount,
    TAccountGuard,
    TAccountInstructionSysvarAccount
  >,
  config?: { programAddress?: TProgramAddress }
): ExecuteInstruction<
  TProgramAddress,
  TAccountSourceAccount,
  TAccountMint,
  TAccountDestinationAccount,
  TAccountOwnerDelegate,
  TAccountExtraMetasAccount,
  TAccountGuard,
  TAccountInstructionSysvarAccount
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? WEN_TRANSFER_GUARD_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    sourceAccount: { value: input.sourceAccount ?? null, isWritable: false },
    mint: { value: input.mint ?? null, isWritable: false },
    destinationAccount: {
      value: input.destinationAccount ?? null,
      isWritable: false,
    },
    ownerDelegate: { value: input.ownerDelegate ?? null, isWritable: false },
    extraMetasAccount: {
      value: input.extraMetasAccount ?? null,
      isWritable: false,
    },
    guard: { value: input.guard ?? null, isWritable: false },
    instructionSysvarAccount: {
      value: input.instructionSysvarAccount ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  // Resolve default values.
  if (!accounts.instructionSysvarAccount.value) {
    accounts.instructionSysvarAccount.value =
      'Sysvar1nstructions1111111111111111111111111' as Address<'Sysvar1nstructions1111111111111111111111111'>;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  const instruction = {
    accounts: [
      getAccountMeta(accounts.sourceAccount),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.destinationAccount),
      getAccountMeta(accounts.ownerDelegate),
      getAccountMeta(accounts.extraMetasAccount),
      getAccountMeta(accounts.guard),
      getAccountMeta(accounts.instructionSysvarAccount),
    ],
    programAddress,
    data: getExecuteInstructionDataEncoder().encode(
      args as ExecuteInstructionDataArgs
    ),
  } as ExecuteInstruction<
    TProgramAddress,
    TAccountSourceAccount,
    TAccountMint,
    TAccountDestinationAccount,
    TAccountOwnerDelegate,
    TAccountExtraMetasAccount,
    TAccountGuard,
    TAccountInstructionSysvarAccount
  >;

  return instruction;
}

export type ParsedExecuteInstruction<
  TProgram extends string = typeof WEN_TRANSFER_GUARD_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    sourceAccount: TAccountMetas[0];
    mint: TAccountMetas[1];
    destinationAccount: TAccountMetas[2];
    ownerDelegate: TAccountMetas[3];
    extraMetasAccount: TAccountMetas[4];
    guard: TAccountMetas[5];
    instructionSysvarAccount: TAccountMetas[6];
  };
  data: ExecuteInstructionData;
};

export function parseExecuteInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>
): ParsedExecuteInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 7) {
    // TODO: Coded error.
    throw new Error('Not enough accounts');
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = instruction.accounts![accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      sourceAccount: getNextAccount(),
      mint: getNextAccount(),
      destinationAccount: getNextAccount(),
      ownerDelegate: getNextAccount(),
      extraMetasAccount: getNextAccount(),
      guard: getNextAccount(),
      instructionSysvarAccount: getNextAccount(),
    },
    data: getExecuteInstructionDataDecoder().decode(instruction.data),
  };
}
