import { createFreezeAccountInstruction } from '@solarti/spl-token';
import { useOperation } from '../../../types/Operation.mjs';
import { isSigner } from '../../../types/Signer.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

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

const Key = 'FreezeTokensOperation';

/**
 * Freezes a token account.
 *
 * ```ts
 * await metaplex.tokens().freeze({ mintAddress, freezeAuthority });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const freezeTokensOperation = useOperation(Key);

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

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

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

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

/**
 * Freezes a token account.
 *
 * ```ts
 * const transactionBuilder = metaplex.tokens().builders().freeze({ mintAddress, freezeAuthority });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const freezeTokensBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    tokenOwner = metaplex.identity().publicKey,
    tokenAddress,
    multiSigners = [],
    freezeAuthority
  } = params;
  const [authorityPublicKey, signers] = isSigner(freezeAuthority) ? [freezeAuthority.publicKey, [freezeAuthority]] : [freezeAuthority, multiSigners];
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenAddressOrAta = tokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: tokenOwner,
    programs
  });
  return TransactionBuilder.make().setFeePayer(payer).add({
    instruction: createFreezeAccountInstruction(tokenAddressOrAta, mintAddress, authorityPublicKey, multiSigners, tokenProgram.address),
    signers,
    key: params.instructionKey ?? 'freezeTokens'
  });
};

export { freezeTokensBuilder, freezeTokensOperation, freezeTokensOperationHandler };
//# sourceMappingURL=freezeTokens.mjs.map
