import { useMemo } from 'react';
import Decimal from 'decimal.js';
import { isWalletPassPayment } from "./useCustomerGuard";
export function useDepositStage(_ref) {
  var isDeposit = _ref.isDeposit,
    depositAmount = _ref.depositAmount,
    _ref$paymentItems = _ref.paymentItems,
    paymentItems = _ref$paymentItems === void 0 ? [] : _ref$paymentItems;
  return useMemo(function () {
    var depositTarget = new Decimal(depositAmount || 0);
    var depositPaid = paymentItems.reduce(function (sum, payment) {
      if ((payment === null || payment === void 0 ? void 0 : payment.status) === 'voided') return sum;
      if ((payment === null || payment === void 0 ? void 0 : payment.order_payment_type) !== 'deposit' && (payment === null || payment === void 0 ? void 0 : payment.type) !== 'deposit') {
        return sum;
      }
      return sum.plus(payment.amount || 0).minus(payment.rounding_amount || 0);
    }, new Decimal(0));
    var hasNormalPayment = paymentItems.some(function (payment) {
      if ((payment === null || payment === void 0 ? void 0 : payment.status) === 'voided') return false;
      if (isWalletPassPayment(payment)) return false;
      return (payment === null || payment === void 0 ? void 0 : payment.order_payment_type) !== 'deposit' && (payment === null || payment === void 0 ? void 0 : payment.type) !== 'deposit';
    });
    var isDepositFullyPaid = depositTarget.gt(0) && depositPaid.gte(depositTarget);
    var canUseDepositPayment = !!isDeposit && !hasNormalPayment && !isDepositFullyPaid;
    return {
      depositTarget: depositTarget.toFixed(2),
      depositPaid: depositPaid.toFixed(2),
      isDepositFullyPaid: isDepositFullyPaid,
      canUseDepositPayment: canUseDepositPayment,
      shouldUseNormalPayment: !canUseDepositPayment
    };
  }, [depositAmount, isDeposit, paymentItems]);
}