function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useMemo } from 'react';
import { ProductCard } from '@pisell/materials';
// import ProductCard from '../../../../../../materials/src/components/productCard';
import useClearCart from "../../hooks/useClearCart";
import { useContext } from "../../hooks/useContext";
import useAddService from "../../hooks/useAddService";
import { useMemoizedFn } from 'ahooks';
import { isNormalProductByDurationSchedule } from "../../../booking/info/service2/utils";
import PisellNumberSelector from "../../../../pro/pisellNumberSelector";
var Product = function Product(props) {
  var id = props.id,
    lists = props.lists,
    _props$isAdd = props.isAdd,
    isAdd = _props$isAdd === void 0 ? true : _props$isAdd;
  var _useContext = useContext(),
    state = _useContext.state,
    _onAction = _useContext.onAction,
    onChange = _useContext.onChange;
  var disabledEdit = state.disabledEdit,
    isShowPackageNote = state.isShowPackageNote,
    isShowNote = state.isShowNote,
    hideAddBtn = state.hideAddBtn,
    platform = state.platform,
    isShowChangeResource = state.isShowChangeResource,
    isShowDelete = state.isShowDelete,
    isShowChangeHolder = state.isShowChangeHolder,
    isShowHolder = state.isShowHolder,
    isShowAddHolderButton = state.isShowAddHolderButton,
    isShowEditProduct = state.isShowEditProduct,
    _state$productCardSty = state.productCardStyleConfig,
    productCardStyleConfig = _state$productCardSty === void 0 ? {} : _state$productCardSty;
  var clearCart = useClearCart(id);

  /**
   * @title: 更新商品数据
   * @description:
   * @param {any} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-11-28 22:02
   */
  var handleChange = useMemoizedFn(function (item, type, other) {
    if (disabledEdit && !(other !== null && other !== void 0 && other.disabled)) return;
    onChange(item, type, other);
  });
  var productParams = useMemo(function () {
    return {
      isShowNote: isShowNote,
      isShowPackageNote: isShowPackageNote,
      disabledEdit: disabledEdit,
      isShowChangeResource: isShowChangeResource,
      isShowChangeHolder: isShowChangeHolder,
      isShowDelete: isShowDelete,
      isShowHolder: isShowHolder,
      isShowAddHolderButton: isShowAddHolderButton,
      isShowEditProduct: isShowEditProduct
    };
  }, [isShowNote, isShowPackageNote, disabledEdit, isShowChangeResource, isShowDelete, isShowHolder, isShowAddHolderButton, isShowChangeHolder, isShowEditProduct]);
  var getProductCardStyleProps = useMemoizedFn(function (type) {
    var conf = productCardStyleConfig["".concat(type, "_product_card_style")];
    if (state.scenario !== 'booking2') return {};
    return {
      isShowImage: conf === null || conf === void 0 ? void 0 : conf.show_product_image,
      isShowDelete: conf === null || conf === void 0 ? void 0 : conf.show_remove_button,
      disabledEdit: true
    };
  });
  return useMemo(function () {
    console.log('render cart');
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__shopping-cart-content"
    }, clearCart, lists === null || lists === void 0 ? void 0 : lists.map(function (item) {
      var Comp = ProductCard;
      var isRetail;
      if (state.scenario === 'booking2') {
        isRetail = isNormalProductByDurationSchedule(item === null || item === void 0 ? void 0 : item._origin);
        Comp = ProductCard.V2;
      }
      return /*#__PURE__*/React.createElement(Comp, _extends({
        type: isRetail ? 'a2' : 'a5',
        rightActions: [{
          key: 'update_number',
          text: /*#__PURE__*/React.createElement(PisellNumberSelector, {
            enablePriceAdjust: true,
            min: 1,
            className: "pisell-sku-card__pisell-number-setter",
            value: item === null || item === void 0 ? void 0 : item.num,
            onChange: function onChange(value) {
              if (value < 1) {
                handleChange(item, 'num', {
                  num: 1
                });
              } else {
                handleChange(item, 'num', {
                  num: value
                });
              }
            }
          }),
          color: '#667085'
        }],
        escapeDom: ".pisell-number-selector-popover",
        key: item._id,
        dataSource: item,
        onAction: function onAction(e) {
          return _onAction === null || _onAction === void 0 ? void 0 : _onAction(e);
        },
        onDelete: function onDelete(e) {
          return handleChange(e, 'delete', item);
        },
        onNote: function onNote(e) {
          return handleChange(e, 'note', {
            note: e.note
          });
        },
        onLike: function onLike(e) {
          return handleChange(e, 'like', {
            like_status: e.like_status
          });
        },
        onEditProduct: function onEditProduct(e) {
          return handleChange === null || handleChange === void 0 ? void 0 : handleChange(e, 'edit_product');
        },
        onCard: function onCard(e) {
          return handleChange === null || handleChange === void 0 ? void 0 : handleChange(e, 'view');
        },
        onChangeResource: function onChangeResource(e) {
          return handleChange(item, 'change_resource', e);
        },
        onChangeHolder: function onChangeHolder(e) {
          return handleChange(item, 'change_holder', e);
        },
        onAddHolder: function onAddHolder(e) {
          return handleChange(item, 'add_holder', e);
        }
      }, productParams, getProductCardStyleProps(isRetail ? 'retail' : 'appointment'), {
        scenario: state.scenario
      }));
    }), !hideAddBtn && platform !== 'h5' && isAdd ? useAddService(id, 'more') : null);
  }, [clearCart, lists, productParams, hideAddBtn, isAdd, platform]);
};
export default Product;