function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React, { useMemo } from 'react';
import { isMobile, locales } from '@pisell/utils';
import ShoppingCart from "../../../../../shoppingCart";
import { useCart, useHolder, useSummary, useStep } from "../../../../../Sales";
import SelectHolderModal from "../../SelectHolderModal";
import ContinueButton from "../ContinueButton";
import { SubTotal } from "../../../../../index";
import useEngineContext from "../../../../../../hooks/useEngineContext";
import Deposit from "../Deposit";
import Voucher from "../../../Voucher";
import "./index.less";
var CartItem = function CartItem() {
  var _context$appHelper, _cartData$cartItems2;
  var _useHolder = useHolder(),
    open = _useHolder.open,
    holderData = _useHolder.holderData,
    selectedHolderIds = _useHolder.selectedHolderIds;
  var _useCart = useCart(),
    deleteCartItem = _useCart.deleteCartItem,
    deleteCartItemByHolder = _useCart.deleteCartItemByHolder,
    cartData = _useCart.cartData,
    addNoteToCartItem = _useCart.addNoteToCartItem;
  var _useSummary = useSummary(),
    taxTitle = _useSummary.taxTitle,
    taxRate = _useSummary.taxRate,
    totalTaxFee = _useSummary.totalTaxFee,
    isPriceIncludeTax = _useSummary.isPriceIncludeTax,
    total = _useSummary.total,
    deposit = _useSummary.deposit,
    subtotal = _useSummary.subtotal,
    surcharge = _useSummary.surcharge;
  var _useStep = useStep(),
    isLastStep = _useStep.isLastStep;
  var context = useEngineContext();
  var _ref = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {},
    translation = _ref.translation;
  var handleClickCard = function handleClickCard(e, type, key) {
    if (type === 'note') {
      return addNoteToCartItem(e);
    }

    // 删除
    if (type === 'delete') {
      return deleteCartItem(e);
    }
    if (type === 'delete_holders') {
      return deleteCartItemByHolder(key);
    }
  };

  /**
   * @description: 是否显示定金
   * @param {*} useMemo
   * @return {*}
   */
  var showDeposit = useMemo(function () {
    var _cartData$cartItems;
    return (cartData === null || cartData === void 0 || (_cartData$cartItems = cartData.cartItems) === null || _cartData$cartItems === void 0 ? void 0 : _cartData$cartItems.length) > 0 && (deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit);
  }, [cartData === null || cartData === void 0 || (_cartData$cartItems2 = cartData.cartItems) === null || _cartData$cartItems2 === void 0 ? void 0 : _cartData$cartItems2.length, deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit]);

  /**
   * @description: 结算列表数据
   * @param {*} useMemo
   * @return {*}
   */
  var subTotalList = useMemo(function () {
    var surchargeList = ((surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeList) || []).map(function (d, index) {
      return {
        key: "custom_surcharge_".concat(index),
        label: translation(d === null || d === void 0 ? void 0 : d.name),
        value: d.amount
      };
    });
    return [{
      key: 'deposit',
      value: deposit === null || deposit === void 0 ? void 0 : deposit.total,
      label: locales.getText('pisell2.appointment.deposit'),
      hidden: !showDeposit
    }, {
      key: 'sub_total',
      value: subtotal
    }].concat(_toConsumableArray(surchargeList), [{
      key: 'tax',
      value: totalTaxFee,
      hidden: Number(totalTaxFee) <= 0,
      tax: {
        rate: taxRate,
        is_price_include_tax: isPriceIncludeTax,
        title: taxTitle
      }
    }, {
      key: 'custom_total',
      value: total,
      label: locales.getText('pisell2.event.total'),
      bold: !showDeposit
    }]);
  }, [total, subtotal, surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeList, deposit === null || deposit === void 0 ? void 0 : deposit.total, taxRate, isPriceIncludeTax, totalTaxFee, taxTitle, showDeposit]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "cart-item-wrap ".concat(isMobile() ? 'cart-mobile-item-wrap' : '')
  }, /*#__PURE__*/React.createElement("div", {
    className: "content"
  }, /*#__PURE__*/React.createElement(ShoppingCart, {
    productParams: {
      products: (cartData === null || cartData === void 0 ? void 0 : cartData.cartItems) || [],
      hideAddBtn: false,
      holders: (holderData === null || holderData === void 0 ? void 0 : holderData.selectedHolderItems) || [],
      disabledEdit: isLastStep
    },
    collapseParams: {
      activeKey: selectedHolderIds,
      ghost: true
    },
    platform: "h5",
    emptyText: locales.getText('pisell2.event.empty.service'),
    isOpenGroup: !!(selectedHolderIds !== null && selectedHolderIds !== void 0 && selectedHolderIds.length),
    onChange: function onChange(e, type, key) {
      return handleClickCard(e, type, key);
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "event-footer"
  }, /*#__PURE__*/React.createElement(Voucher, null), /*#__PURE__*/React.createElement(SubTotal, {
    list: subTotalList,
    style: {
      padding: 0
    },
    children: showDeposit ? /*#__PURE__*/React.createElement(Deposit, {
      showTitle: false
    }) : null
  }), /*#__PURE__*/React.createElement(ContinueButton, null))), /*#__PURE__*/React.createElement(SelectHolderModal, {
    open: open
  }));
};
export default CartItem;