/**
 * 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 CreateAccountWithSeedInstructionAccounts = {
  payer?: Signer;
  newAccount: PublicKey | Pda;
  baseAccount: Signer;
};

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

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

export function getCreateAccountWithSeedInstructionDataSerializer(): Serializer<
  CreateAccountWithSeedInstructionDataArgs,
  CreateAccountWithSeedInstructionData
> {
  return mapSerializer<
    CreateAccountWithSeedInstructionDataArgs,
    any,
    CreateAccountWithSeedInstructionData
  >(
    struct<CreateAccountWithSeedInstructionData>(
      [
        ['discriminator', u32()],
        ['base', publicKeySerializer()],
        ['seed', string()],
        ['amount', u64()],
        ['space', u64()],
        ['programAddress', publicKeySerializer()],
      ],
      { description: 'CreateAccountWithSeedInstructionData' }
    ),
    (value) => ({ ...value, discriminator: 3 })
  ) as Serializer<
    CreateAccountWithSeedInstructionDataArgs,
    CreateAccountWithSeedInstructionData
  >;
}

// Args.
export type CreateAccountWithSeedInstructionArgs =
  CreateAccountWithSeedInstructionDataArgs;

// Instruction.
export function createAccountWithSeed(
  context: Pick<Context, 'payer' | 'programs'>,
  input: CreateAccountWithSeedInstructionAccounts &
    CreateAccountWithSeedInstructionArgs
): TransactionBuilder {
  // Program ID.
  const programId = context.programs.getPublicKey(
    'system',
    '11111111111111111111111111111111'
  );

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

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

  // Default values.
  if (!resolvedAccounts.payer.value) {
    resolvedAccounts.payer.value = context.payer;
  }

  // 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 = getCreateAccountWithSeedInstructionDataSerializer().serialize(
    resolvedArgs as CreateAccountWithSeedInstructionDataArgs
  );

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

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