/**
 * 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 {
  Context,
  Pda,
  PublicKey,
  Signer,
  TransactionBuilder,
  transactionBuilder,
} from '@metaplex-foundation/umi';
import {
  Serializer,
  mapSerializer,
  publicKey as publicKeySerializer,
  string,
  struct,
  u32,
  u64,
} from '@metaplex-foundation/umi/serializers';
import {
  ResolvedAccount,
  ResolvedAccountsWithIndices,
  getAccountMetasAndSigners,
} from '../shared';

// Accounts.
export type AllocateWithSeedInstructionAccounts = {
  newAccount: PublicKey | Pda;
  baseAccount: Signer;
};

// Data.
export type AllocateWithSeedInstructionData = {
  discriminator: number;
  base: PublicKey;
  seed: string;
  space: bigint;
  programAddress: PublicKey;
};

export type AllocateWithSeedInstructionDataArgs = {
  base: PublicKey;
  seed: string;
  space: number | bigint;
  programAddress: PublicKey;
};

export function getAllocateWithSeedInstructionDataSerializer(): Serializer<
  AllocateWithSeedInstructionDataArgs,
  AllocateWithSeedInstructionData
> {
  return mapSerializer<
    AllocateWithSeedInstructionDataArgs,
    any,
    AllocateWithSeedInstructionData
  >(
    struct<AllocateWithSeedInstructionData>(
      [
        ['discriminator', u32()],
        ['base', publicKeySerializer()],
        ['seed', string()],
        ['space', u64()],
        ['programAddress', publicKeySerializer()],
      ],
      { description: 'AllocateWithSeedInstructionData' }
    ),
    (value) => ({ ...value, discriminator: 9 })
  ) as Serializer<
    AllocateWithSeedInstructionDataArgs,
    AllocateWithSeedInstructionData
  >;
}

// Args.
export type AllocateWithSeedInstructionArgs =
  AllocateWithSeedInstructionDataArgs;

// Instruction.
export function allocateWithSeed(
  context: Pick<Context, 'programs'>,
  input: AllocateWithSeedInstructionAccounts & AllocateWithSeedInstructionArgs
): TransactionBuilder {
  // Program ID.
  const programId = context.programs.getPublicKey(
    'system',
    '11111111111111111111111111111111'
  );

  // Accounts.
  const resolvedAccounts = {
    newAccount: {
      index: 0,
      isWritable: true as boolean,
      value: input.newAccount ?? null,
    },
    baseAccount: {
      index: 1,
      isWritable: false as boolean,
      value: input.baseAccount ?? null,
    },
  } satisfies ResolvedAccountsWithIndices;

  // Arguments.
  const resolvedArgs: AllocateWithSeedInstructionArgs = { ...input };

  // Accounts in order.
  const orderedAccounts: ResolvedAccount[] = Object.values(
    resolvedAccounts
  ).sort((a, b) => a.index - b.index);

  // Keys and Signers.
  const [keys, signers] = getAccountMetasAndSigners(
    orderedAccounts,
    'programId',
    programId
  );

  // Data.
  const data = getAllocateWithSeedInstructionDataSerializer().serialize(
    resolvedArgs as AllocateWithSeedInstructionDataArgs
  );

  // Bytes Created On Chain.
  const bytesCreatedOnChain = 0;

  return transactionBuilder([
    { instruction: { keys, programId, data }, signers, bytesCreatedOnChain },
  ]);
}
