/**
 * 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 ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
} from "@solana/kit";
import { findEventAuthorityPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const SET_CROSS_DOMAIN_ADMIN_DISCRIMINATOR = new Uint8Array([
  102, 206, 237, 106, 63, 141, 42, 248,
]);

export function getSetCrossDomainAdminDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    SET_CROSS_DOMAIN_ADMIN_DISCRIMINATOR,
  );
}

export type SetCrossDomainAdminInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? ReadonlySignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountState extends string
        ? WritableAccount<TAccountState>
        : TAccountState,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type SetCrossDomainAdminInstructionData = {
  discriminator: ReadonlyUint8Array;
  crossDomainAdmin: Address;
};

export type SetCrossDomainAdminInstructionDataArgs = {
  crossDomainAdmin: Address;
};

export function getSetCrossDomainAdminInstructionDataEncoder(): FixedSizeEncoder<SetCrossDomainAdminInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["crossDomainAdmin", getAddressEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: SET_CROSS_DOMAIN_ADMIN_DISCRIMINATOR,
    }),
  );
}

export function getSetCrossDomainAdminInstructionDataDecoder(): FixedSizeDecoder<SetCrossDomainAdminInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["crossDomainAdmin", getAddressDecoder()],
  ]);
}

export function getSetCrossDomainAdminInstructionDataCodec(): FixedSizeCodec<
  SetCrossDomainAdminInstructionDataArgs,
  SetCrossDomainAdminInstructionData
> {
  return combineCodec(
    getSetCrossDomainAdminInstructionDataEncoder(),
    getSetCrossDomainAdminInstructionDataDecoder(),
  );
}

export type SetCrossDomainAdminAsyncInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  state: Address<TAccountState>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  crossDomainAdmin: SetCrossDomainAdminInstructionDataArgs["crossDomainAdmin"];
};

export async function getSetCrossDomainAdminInstructionAsync<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: SetCrossDomainAdminAsyncInput<
    TAccountSigner,
    TAccountState,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  SetCrossDomainAdminInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    state: { value: input.state ?? null, isWritable: true },
    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.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getSetCrossDomainAdminInstructionDataEncoder().encode(
      args as SetCrossDomainAdminInstructionDataArgs,
    ),
    programAddress,
  } as SetCrossDomainAdminInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type SetCrossDomainAdminInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  state: Address<TAccountState>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  crossDomainAdmin: SetCrossDomainAdminInstructionDataArgs["crossDomainAdmin"];
};

export function getSetCrossDomainAdminInstruction<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: SetCrossDomainAdminInput<
    TAccountSigner,
    TAccountState,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetCrossDomainAdminInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountState,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    state: { value: input.state ?? null, isWritable: true },
    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 };

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getSetCrossDomainAdminInstructionDataEncoder().encode(
      args as SetCrossDomainAdminInstructionDataArgs,
    ),
    programAddress,
  } as SetCrossDomainAdminInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedSetCrossDomainAdminInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    state: TAccountMetas[1];
    eventAuthority: TAccountMetas[2];
    program: TAccountMetas[3];
  };
  data: SetCrossDomainAdminInstructionData;
};

export function parseSetCrossDomainAdminInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetCrossDomainAdminInstruction<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(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getSetCrossDomainAdminInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
