function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useContext, useMemo } from 'react';
import ShoppingCart from "../../../shoppingCart";
import { CartContext } from "../../../shoppingCart/Provider";
import { Context } from "../model";
import { formatProducts, isEventBooking, isNormalProductByDurationSchedule, updateServices, getSelectedGiftBySourceProductId } from "../service2/utils";
import useHolderConfig from "../hooks/useHolderConfig";
import { useMemoizedFn } from 'ahooks';
import { getProductExtend } from "../../info2/service/addService/utils";
import useAddHolder from "../hooks/useAddHolder";
import useTranslationOriginal from "../../../../hooks/useTranslationOriginal";
import useCartChangeEffect from "../../../ticketBooking/hooks/pisellos/useCartChangeEffect";
import { addService } from "../../../ticketBooking/utils/index";
import { locales } from "@pisell/utils";
var Lists = function Lists(_ref) {
  var _state$service2;
  var onEdit = _ref.onEdit,
    onChange = _ref.onChange,
    onAction = _ref.onAction,
    isParallelResource = _ref.isParallelResource,
    _ref$isShowDelete = _ref.isShowDelete,
    isShowDelete = _ref$isShowDelete === void 0 ? true : _ref$isShowDelete,
    _ref$isShowAmountFoot = _ref.isShowAmountFooter,
    isShowAmountFooter = _ref$isShowAmountFoot === void 0 ? true : _ref$isShowAmountFoot,
    _ref$isShowPromotionA = _ref.isShowPromotionAlert,
    isShowPromotionAlert = _ref$isShowPromotionA === void 0 ? true : _ref$isShowPromotionA;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var _useHolderConfig = useHolderConfig(state),
    isShowChangeHolder = _useHolderConfig.isShowChangeHolder;
  var _useAddHolder = useAddHolder(state, dispatch),
    handleAddHolder = _useAddHolder.handleAddHolder;
  var translationOriginal = useTranslationOriginal();
  var _useCartChangeEffect = useCartChangeEffect(),
    onCartChange = _useCartChangeEffect.onCartChange;
  /**
   * @title: 更新商品数据
   * @description:
   * @param {any} newState
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-11-29 16:35
   */
  var handleChange = useMemoizedFn(function (currentItem, type, other) {
    var _window$closeBookingE, _window2;
    console.time('handleChange');
    console.log(window.pp1 = performance.now(), '打开商品');
    if (state.disabledEdit || state.disableEventBookingEdit) {
      return;
    }
    var products = updateServices(state, currentItem, type, other, {
      handleAddHolder: handleAddHolder
    });

    // 查看
    if (type === 'view' || type === 'edit_product') {
      var product = currentItem._origin;
      var cacheItem = _objectSpread(_objectSpread({}, product), {}, {
        new: 0
      });
      if (isParallelResource && !isNormalProductByDurationSchedule(product)) {
        cacheItem = getProductExtend({
          cacheItem: cacheItem,
          state: state
        });
      }
      if (!isNormalProductByDurationSchedule(cacheItem)) {
        var _window$openBookingEd, _window;
        (_window$openBookingEd = (_window = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window);
        onChange({
          editModal: true,
          cacheItem: cacheItem
        });
      }
      return onEdit === null || onEdit === void 0 ? void 0 : onEdit(cacheItem, type);
    }

    // 营销活动触发更新商品数据
    if (type && ['update_products', 'update_gifts'].includes(type)) {
      var _ref2 = currentItem || {},
        sourceProductId = _ref2.sourceProductId,
        selectedProducts = _ref2.selectedProducts;
      var _products = getSelectedGiftBySourceProductId(sourceProductId, products);
      var newProducts = _toConsumableArray(_products);
      // 数量合并
      if ((selectedProducts === null || selectedProducts === void 0 ? void 0 : selectedProducts.length) > 0) {
        selectedProducts.forEach(function (item) {
          var _addService = addService(newProducts, item, state, false),
            list = _addService.list;
          newProducts = _toConsumableArray(list);
        });
      }
      products = _toConsumableArray(newProducts);
    }
    (_window$closeBookingE = (_window2 = window).closeBookingEditModal) === null || _window$closeBookingE === void 0 || _window$closeBookingE.call(_window2);
    var params = {
      editModal: false,
      value: products,
      edit: true,
      cacheItem: null
    };
    if (type && ['num', 'delete', 'update_products'].includes(type)) {
      onCartChange(type, products);
    }
    onChange(params, undefined, {
      updateType: type
    });
  });

  /**
   * @title: 商品信息
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-12-09 16:07
   */
  var productParams = useMemo(function () {
    var _state$bookingConfig;
    return {
      hideAddBtn: state.isTerminalDayBookingPage || state.isBookingCreatePage,
      isShowNote: state.isBookingTicket2 ? true : isEventBooking(state),
      isShowDelete: isShowDelete,
      isShowAmountFooter: isShowAmountFooter,
      isShowPromotionAlert: isShowPromotionAlert,
      isShowPackageNote: false,
      accounts: [],
      isEdit: !!(state !== null && state !== void 0 && state.bookingId),
      disabledEdit: state === null || state === void 0 ? void 0 : state.disabledEdit,
      maxLength: isEventBooking(state) ? 1 : 999,
      isShowChangeHolder: isShowChangeHolder,
      isShowChangeResource: true,
      isShowAddHolderButton: true,
      isShowEditProduct: !state.isBookingCreatePage,
      productCardStyleConfig: (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.cart_settings_tab,
      productsPerRow: state.secondaryScreenCartProductsPerRow
    };
  }, [state === null || state === void 0 ? void 0 : state.bookingId, state === null || state === void 0 ? void 0 : state.disabledEdit, isShowChangeHolder, state.isBookingCreatePage, state.bookingConfig, state.secondaryScreenCartProductsPerRow]);
  var products = useMemo(function () {
    var _state$service;
    console.time('products');
    var p = formatProducts((state === null || state === void 0 || (_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value) || [], state, true, true, isParallelResource, translationOriginal);
    console.timeEnd('products');
    return p;
  }, [state === null || state === void 0 || (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.value, state, translationOriginal]);
  return useMemo(function () {
    var _state$service3;
    console.log('render List');
    return /*#__PURE__*/React.createElement(CartContext.Provider, {
      value: {
        state: state
      }
    }, /*#__PURE__*/React.createElement(ShoppingCart, {
      scenario: state.isBookingTicket2 ? 'booking2' : undefined,
      isParallelResource: isParallelResource,
      onAction: onAction,
      onChange: handleChange,
      isOpenGroup: false,
      isHideClearCart: true,
      onAddProduct: function onAddProduct() {
        return onChange({
          addModal: true
        });
      },
      emptyText: locales.getText('pisell2.text.shopping-cart.no.selected')
      // onActiveKey={handleActiveKey}
      // collapseParams={CollapseParams}
      ,
      productParams: _objectSpread(_objectSpread({}, productParams), {}, {
        products: products
      }),
      cartInfo: {
        promotions: state === null || state === void 0 || (_state$service3 = state.service) === null || _state$service3 === void 0 ? void 0 : _state$service3.unfulfilledPromotions,
        isShowPromotionAlert: isShowPromotionAlert
      }
    }));
  }, [products, state, productParams]);
};
export default Lists;