import { SystemProgram } from '@solarti/web3.js';
import { useOperation } from '../../../types/Operation.mjs';
import { assertSol } from '../../../types/Amount.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

// -----------------
// Operation
// -----------------

const Key = 'TransferSolOperation';

/**
 * Transfers some SOL from one account to another.
 *
 * ```ts
 * await metaplex
 *   .system()
 *   .transferSol({
 *     to: new PublicKey("..."),
 *     amount: sol(1.5),
 *   };
 * ````
 *
 * @group Operations
 * @category Constructors
 */
const transferSolOperation = useOperation(Key);

/**
 * @group Operations
 * @category Types
 */

/**
 * @group Operations
 * @category Handlers
 */
const transferSolOperationHandler = {
  async handle(operation, metaplex, scope) {
    const builder = transferSolBuilder(metaplex, operation.input, scope);
    return builder.sendAndConfirm(metaplex, scope.confirmOptions);
  }
};

// -----------------
// Builder
// -----------------

/**
 * @group Transaction Builders
 * @category Inputs
 */

/**
 * Transfers some SOL from one account to another.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .system()
 *   .builders()
 *   .transferSol({
 *     to: new PublicKey("..."),
 *     amount: sol(1.5),
 *   });
 * ````
 *
 * @group Transaction Builders
 * @category Constructors
 */
const transferSolBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    from = metaplex.identity(),
    to,
    amount,
    basePubkey,
    seed
  } = params;
  assertSol(amount);
  return TransactionBuilder.make().setFeePayer(payer).add({
    instruction: SystemProgram.transfer({
      fromPubkey: from.publicKey,
      toPubkey: to,
      lamports: amount.basisPoints.toNumber(),
      ...(basePubkey ? {
        basePubkey,
        seed
      } : {}),
      programId: metaplex.programs().getSystem(programs).address
    }),
    signers: [from],
    key: params.instructionKey ?? 'transferSol'
  });
};

export { transferSolBuilder, transferSolOperation, transferSolOperationHandler };
//# sourceMappingURL=transferSol.mjs.map
