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

export const SET_CURRENT_TIME_DISCRIMINATOR = new Uint8Array([
  69, 100, 169, 193, 125, 0, 150, 69,
]);

export function getSetCurrentTimeDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    SET_CURRENT_TIME_DISCRIMINATOR,
  );
}

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

export type SetCurrentTimeInstructionData = {
  discriminator: ReadonlyUint8Array;
  newTime: number;
};

export type SetCurrentTimeInstructionDataArgs = { newTime: number };

export function getSetCurrentTimeInstructionDataEncoder(): FixedSizeEncoder<SetCurrentTimeInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newTime", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: SET_CURRENT_TIME_DISCRIMINATOR }),
  );
}

export function getSetCurrentTimeInstructionDataDecoder(): FixedSizeDecoder<SetCurrentTimeInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newTime", getU32Decoder()],
  ]);
}

export function getSetCurrentTimeInstructionDataCodec(): FixedSizeCodec<
  SetCurrentTimeInstructionDataArgs,
  SetCurrentTimeInstructionData
> {
  return combineCodec(
    getSetCurrentTimeInstructionDataEncoder(),
    getSetCurrentTimeInstructionDataDecoder(),
  );
}

export type SetCurrentTimeInput<
  TAccountState extends string = string,
  TAccountSigner extends string = string,
> = {
  state: Address<TAccountState>;
  signer: TransactionSigner<TAccountSigner>;
  newTime: SetCurrentTimeInstructionDataArgs["newTime"];
};

export function getSetCurrentTimeInstruction<
  TAccountState extends string,
  TAccountSigner extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: SetCurrentTimeInput<TAccountState, TAccountSigner>,
  config?: { programAddress?: TProgramAddress },
): SetCurrentTimeInstruction<TProgramAddress, TAccountState, TAccountSigner> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    state: { value: input.state ?? null, isWritable: true },
    signer: { value: input.signer ?? 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.state), getAccountMeta(accounts.signer)],
    data: getSetCurrentTimeInstructionDataEncoder().encode(
      args as SetCurrentTimeInstructionDataArgs,
    ),
    programAddress,
  } as SetCurrentTimeInstruction<
    TProgramAddress,
    TAccountState,
    TAccountSigner
  >);
}

export type ParsedSetCurrentTimeInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    state: TAccountMetas[0];
    signer: TAccountMetas[1];
  };
  data: SetCurrentTimeInstructionData;
};

export function parseSetCurrentTimeInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetCurrentTimeInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 2) {
    // 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: { state: getNextAccount(), signer: getNextAccount() },
    data: getSetCurrentTimeInstructionDataDecoder().decode(instruction.data),
  };
}
