/**
 * 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 type { Address } from '@solana/addresses';
import {
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  transformEncoder,
  type Codec,
  type Decoder,
  type Encoder,
  type ReadonlyUint8Array,
} from '@solana/codecs';
import {
  type IAccountSignerMeta,
  type TransactionSigner,
} from '@solana/signers';

import { POD_COM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

import type {
  IAccountMeta,
  IInstruction,
  IInstructionWithAccounts,
  IInstructionWithData,
} from '@solana/instructions';

// Define missing types for compatibility
type ReadonlyAccount<T> = T;
type WritableAccount<T> = T;
type WritableSignerAccount<T> = T;

export const ADD_PARTICIPANT_DISCRIMINATOR = new Uint8Array([
  201, 23, 89, 155, 12, 47, 199, 233,
]);

export function getAddParticipantDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ADD_PARTICIPANT_DISCRIMINATOR
  );
}

export type AddParticipantInstruction<
  TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS,
  TAccountChannelAccount extends string | IAccountMeta<string> = string,
  TAccountAdmin extends string | IAccountMeta<string> = string,
  TAccountNewParticipant extends string | IAccountMeta<string> = string,
  TAccountSystemProgram extends
    | string
    | IAccountMeta<string> = '11111111111111111111111111111111',
  TRemainingAccounts extends ReadonlyArray<IAccountMeta<string>> = [],
> = IInstruction<TProgram> &
  IInstructionWithData<Uint8Array> &
  IInstructionWithAccounts<
    [
      TAccountChannelAccount extends string
        ? WritableAccount<TAccountChannelAccount>
        : TAccountChannelAccount,
      TAccountAdmin extends string
        ? WritableSignerAccount<TAccountAdmin> &
            IAccountSignerMeta<TAccountAdmin>
        : TAccountAdmin,
      TAccountNewParticipant extends string
        ? ReadonlyAccount<TAccountNewParticipant>
        : TAccountNewParticipant,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      ...TRemainingAccounts,
    ]
  >;

export interface AddParticipantInstructionData {
  discriminator: ReadonlyUint8Array;
}

export interface AddParticipantInstructionDataArgs {}

export function getAddParticipantInstructionDataEncoder(): Encoder<AddParticipantInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)]]),
    value => ({ ...value, discriminator: ADD_PARTICIPANT_DISCRIMINATOR })
  );
}

export function getAddParticipantInstructionDataDecoder(): Decoder<AddParticipantInstructionData> {
  return getStructDecoder([
    ['discriminator', fixDecoderSize(getBytesDecoder(), 8)],
  ]);
}

export function getAddParticipantInstructionDataCodec(): Codec<
  AddParticipantInstructionDataArgs,
  AddParticipantInstructionData
> {
  return combineCodec(
    getAddParticipantInstructionDataEncoder(),
    getAddParticipantInstructionDataDecoder()
  );
}

export interface AddParticipantInput<
  TAccountChannelAccount extends string = string,
  TAccountAdmin extends string = string,
  TAccountNewParticipant extends string = string,
  TAccountSystemProgram extends string = string,
> {
  channelAccount: Address<TAccountChannelAccount>;
  admin: TransactionSigner<TAccountAdmin>;
  newParticipant: Address<TAccountNewParticipant>;
  systemProgram?: Address<TAccountSystemProgram>;
}

export function getAddParticipantInstruction<
  TAccountChannelAccount extends string,
  TAccountAdmin extends string,
  TAccountNewParticipant extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address = typeof POD_COM_PROGRAM_ADDRESS,
>(
  input: AddParticipantInput<
    TAccountChannelAccount,
    TAccountAdmin,
    TAccountNewParticipant,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress }
): AddParticipantInstruction<
  TProgramAddress,
  TAccountChannelAccount,
  TAccountAdmin,
  TAccountNewParticipant,
  TAccountSystemProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? POD_COM_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    channelAccount: { value: input.channelAccount ?? null, isWritable: true },
    admin: { value: input.admin ?? null, isWritable: true },
    newParticipant: { value: input.newParticipant ?? null, isWritable: false },
    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');
  const instruction = {
    accounts: [
      getAccountMeta(accounts.channelAccount),
      getAccountMeta(accounts.admin),
      getAccountMeta(accounts.newParticipant),
      getAccountMeta(accounts.systemProgram),
    ],
    programAddress,
    data: getAddParticipantInstructionDataEncoder().encode({}),
  } as AddParticipantInstruction<
    TProgramAddress,
    TAccountChannelAccount,
    TAccountAdmin,
    TAccountNewParticipant,
    TAccountSystemProgram
  >;

  return instruction;
}

export interface ParsedAddParticipantInstruction<
  TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS,
  TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> {
  programAddress: Address<TProgram>;
  accounts: {
    channelAccount: TAccountMetas[0];
    admin: TAccountMetas[1];
    newParticipant: TAccountMetas[2];
    systemProgram: TAccountMetas[3];
  };
  data: AddParticipantInstructionData;
}

export function parseAddParticipantInstruction<
  TProgram extends string,
  TAccountMetas extends readonly IAccountMeta[],
>(
  instruction: IInstruction<TProgram> &
    IInstructionWithAccounts<TAccountMetas> &
    IInstructionWithData<Uint8Array>
): ParsedAddParticipantInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 4) {
    // Specific error for instruction validation
    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: {
      channelAccount: getNextAccount(),
      admin: getNextAccount(),
      newParticipant: getNextAccount(),
      systemProgram: getNextAccount(),
    },
    data: getAddParticipantInstructionDataDecoder().decode(instruction.data),
  };
}
