/**
 * 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 TransferSolWithSeedInstructionAccounts = {
  source: PublicKey | Pda;
  baseAccount: Signer;
  destination: PublicKey | Pda;
};

// Data.
export type TransferSolWithSeedInstructionData = {
  discriminator: number;
  amount: bigint;
  fromSeed: string;
  fromOwner: PublicKey;
};

export type TransferSolWithSeedInstructionDataArgs = {
  amount: number | bigint;
  fromSeed: string;
  fromOwner: PublicKey;
};

export function getTransferSolWithSeedInstructionDataSerializer(): Serializer<
  TransferSolWithSeedInstructionDataArgs,
  TransferSolWithSeedInstructionData
> {
  return mapSerializer<
    TransferSolWithSeedInstructionDataArgs,
    any,
    TransferSolWithSeedInstructionData
  >(
    struct<TransferSolWithSeedInstructionData>(
      [
        ['discriminator', u32()],
        ['amount', u64()],
        ['fromSeed', string()],
        ['fromOwner', publicKeySerializer()],
      ],
      { description: 'TransferSolWithSeedInstructionData' }
    ),
    (value) => ({ ...value, discriminator: 11 })
  ) as Serializer<
    TransferSolWithSeedInstructionDataArgs,
    TransferSolWithSeedInstructionData
  >;
}

// Args.
export type TransferSolWithSeedInstructionArgs =
  TransferSolWithSeedInstructionDataArgs;

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

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

  // Arguments.
  const resolvedArgs: TransferSolWithSeedInstructionArgs = { ...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 = getTransferSolWithSeedInstructionDataSerializer().serialize(
    resolvedArgs as TransferSolWithSeedInstructionDataArgs
  );

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

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