'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var web3_js = require('@solarti/web3.js');
var Operation = require('../../../types/Operation.cjs');
var Amount = require('../../../types/Amount.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

// -----------------
// 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 = Operation.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;
  Amount.assertSol(amount);
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: web3_js.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'
  });
};

exports.transferSolBuilder = transferSolBuilder;
exports.transferSolOperation = transferSolOperation;
exports.transferSolOperationHandler = transferSolOperationHandler;
//# sourceMappingURL=transferSol.cjs.map
