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 _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); }
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, { useMemo, useRef } from 'react';
import { useSize } from 'ahooks';
import ProductSelect from "../../../../plus/productSelect";
import List from "../../../list/List";
import SkuCard from "../../../../plus/productSelect/ProductCard/SkuCard";
import { formatSkuDataSource, generateProductTabs } from "./utils";
import "./index.less";
/**
 * 产品展示适配器组件
 * 统一管理 ProductSelect 和 SkuList 两种展示模式
 */
var ProductDisplayAdapter = function ProductDisplayAdapter(props) {
  var config = props.config,
    dataSource = props.dataSource,
    _props$loading = props.loading,
    loading = _props$loading === void 0 ? false : _props$loading,
    currentProductId = props.currentProductId,
    onSelectProduct = props.onSelectProduct,
    emptyDescription = props.emptyDescription,
    style = props.style,
    className = props.className;
  var _config$displayMode = config.displayMode,
    displayMode = _config$displayMode === void 0 ? 'product-select' : _config$displayMode;
  var listRef = useRef(null);
  var size = useSize(listRef);

  // 渲染 ProductSelect 模式
  var renderProductSelect = useMemo(function () {
    var _ref = config.productSelectConfig || {},
      _ref$showSearch = _ref.showSearch,
      showSearch = _ref$showSearch === void 0 ? true : _ref$showSearch,
      _ref$showTab = _ref.showTab,
      showTab = _ref$showTab === void 0 ? true : _ref$showTab,
      _ref$layout = _ref.layout,
      layout = _ref$layout === void 0 ? {
        grid: true,
        config: '12:12'
      } : _ref$layout,
      _ref$productListSetti = _ref.productListSetting,
      productListSetting = _ref$productListSetti === void 0 ? {
        isProductCover: 1,
        isTitle: 1,
        isSubtitle: 1,
        isPrice: 1,
        isOriginPrice: 0,
        isStock: 0
      } : _ref$productListSetti;
    return /*#__PURE__*/React.createElement(ProductSelect, {
      dataSource: dataSource,
      layout: layout,
      onChange: onSelectProduct,
      value: currentProductId,
      loading: loading,
      productListSetting: productListSetting,
      showSearch: showSearch,
      showTab: showTab,
      emptyDescription: emptyDescription,
      style: style
    });
  }, [config.productSelectConfig, dataSource, currentProductId, onSelectProduct, loading, emptyDescription, style]);

  // 渲染 SkuList 模式
  var renderSkuList = useMemo(function () {
    var _ref2 = config.skuListConfig || {},
      _ref2$listConfig = _ref2.listConfig,
      listConfig = _ref2$listConfig === void 0 ? {} : _ref2$listConfig,
      _ref2$skuCardAllConfi = _ref2.skuCardAllConfig,
      skuCardAllConfig = _ref2$skuCardAllConfi === void 0 ? {} : _ref2$skuCardAllConfi;

    // 从配置中获取各项配置
    var displayStyle = listConfig.layout || 'grid';
    var columns = listConfig.columns || 2;
    var paginationType = listConfig.showPagination ? 'pager' : 'all';
    var enableTab = listConfig.showTab || false;
    var tabStyle = listConfig.tabStyle || 'switch';
    var stickyTop = listConfig.stickyTop !== undefined ? listConfig.stickyTop : true;
    var tabData = enableTab ? generateProductTabs(dataSource, tabStyle) : undefined;
    var filteredData = dataSource.map(function (item) {
      var _item$category, _item$category$map;
      return _objectSpread(_objectSpread({}, item), {}, {
        category: ((_item$category = item.category) === null || _item$category === void 0 || (_item$category$map = _item$category.map) === null || _item$category$map === void 0 ? void 0 : _item$category$map.call(_item$category, function (category) {
          return "".concat(category.id);
        })) || []
      });
    });
    return /*#__PURE__*/React.createElement("div", {
      className: "sku-list-container ".concat(className || ''),
      style: style,
      ref: listRef
    }, /*#__PURE__*/React.createElement(List, {
      className: "sku-list",
      style: {
        maxHeight: size === null || size === void 0 ? void 0 : size.height,
        overflow: 'auto'
      },
      data: filteredData,
      loading: loading,
      displayStyle: displayStyle,
      columns: columns,
      paginationType: paginationType,
      layoutDirection: "vertical"
      // Tab 配置
      ,
      tabStyle: enableTab ? tabStyle : 'none',
      tabData: tabData,
      tabGroup: "category",
      stickyTop: stickyTop
      // 空状态配置
      ,
      emptyConfig: {
        show: !loading && filteredData.length === 0,
        text: emptyDescription
      },
      renderItem: function renderItem(item, index) {
        return /*#__PURE__*/React.createElement(SkuCard, _extends({
          key: item.id
        }, skuCardAllConfig, {
          dataSource: formatSkuDataSource(item),
          onCardClick: function onCardClick() {
            return onSelectProduct(item);
          },
          isSelected: item.id === currentProductId
        }));
      }
    }));
  }, [config.skuListConfig, dataSource, loading, currentProductId, onSelectProduct, emptyDescription, style, className, size === null || size === void 0 ? void 0 : size.height]);

  // 根据配置渲染对应模式
  if (displayMode === 'sku-list') {
    return renderSkuList;
  }

  // 默认使用 product-select 模式
  return renderProductSelect;
};
export default ProductDisplayAdapter;