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 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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react';
import ProductSelect from "../../../../plus/productSelect";
import List from "../../../list/List";
import ProductCard from "../../../../plus/productSelect/ProductCard";
import { generateProductTabs, sortProductsByTabOrder } from "./utils";
import { withResponsive } from "../../../../responsive";
import "./index.less";
import { locales } from "@pisell/utils";
/**
 * 产品展示适配器组件
 * 统一管理 ProductSelect 和 SkuList 两种展示模式
 */
var ProductDisplayAdapter = function ProductDisplayAdapter(props, ref) {
  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 _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    keyword = _useState2[0],
    setKeyword = _useState2[1];
  var _useState3 = useState(undefined),
    _useState4 = _slicedToArray(_useState3, 2),
    activeTab = _useState4[0],
    setActiveTab = _useState4[1];
  var onActiveTabChange = useCallback(function (tab) {
    setActiveTab(tab);
  }, []);
  // 渲染 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$compact = _ref.compact,
      compact = _ref$compact === void 0 ? false : _ref$compact,
      _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, {
      tabSwitchMode: "switch",
      dataSource: dataSource,
      layout: layout,
      onChange: onSelectProduct,
      value: currentProductId,
      loading: loading,
      productListSetting: productListSetting,
      showSearch: showSearch,
      showTab: showTab,
      emptyDescription: emptyDescription,
      style: style,
      compact: compact,
      scenario: "booking2"
    });
  }, [config.productSelectConfig, dataSource, currentProductId, onSelectProduct, loading, emptyDescription, style]);
  var _filteredList = useMemo(function () {
    if (Array.isArray(activeTab) && activeTab.length === 0 && !keyword) {
      return [];
    }
    if (!keyword) return dataSource;
    return dataSource.filter(function (product) {
      if (typeof (product === null || product === void 0 ? void 0 : product.title) !== 'string') return false;
      return product.title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
    });
  }, [keyword, dataSource, activeTab]);
  useImperativeHandle(ref, function () {
    return {
      getTabbarRef: function getTabbarRef() {
        var _listRef$current;
        return (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.getTabbarRef();
      }
    };
  });
  var tabData = useMemo(function () {
    var _listConfig$isTreeTab;
    var _ref2 = config.skuListConfig || {},
      _ref2$listConfig = _ref2.listConfig,
      listConfig = _ref2$listConfig === void 0 ? {} : _ref2$listConfig;
    var enableTab = listConfig.showTab || false;
    var tabStyle = listConfig.tabStyle || 'switch';
    var isTreeTab = (_listConfig$isTreeTab = listConfig.isTreeTab) !== null && _listConfig$isTreeTab !== void 0 ? _listConfig$isTreeTab : true;
    return enableTab ? generateProductTabs(dataSource, tabStyle, isTreeTab) : undefined;
  }, [dataSource]);
  var productConfig = useMemo(function () {
    return {
      isProductCover: 1,
      isTitle: 1,
      isSubtitle: 0,
      isPrice: 1,
      isOriginPrice: 1,
      isStock: 0,
      showZeroPrice: true,
      layoutDirection: 'row',
      imgHeight: '126px',
      imgWidth: '100%',
      rightStyle: {
        minHeight: 'auto',
        justifyContent: 'space-between'
      }
    };
  }, []);

  // 渲染 SkuList 模式
  var renderSkuList = useMemo(function () {
    var _listConfig$columnGap, _listConfig$rowGap, _listConfig$isTreeTab2;
    var _ref3 = config.skuListConfig || {},
      _ref3$listConfig = _ref3.listConfig,
      listConfig = _ref3$listConfig === void 0 ? {} : _ref3$listConfig,
      _ref3$skuCardAllConfi = _ref3.skuCardAllConfig,
      skuCardAllConfig = _ref3$skuCardAllConfi === void 0 ? {} : _ref3$skuCardAllConfi;
    // 从配置中获取各项配置
    var displayStyle = listConfig.layout || 'grid';
    var columns = listConfig.columns || 2;
    var columnGap = (_listConfig$columnGap = listConfig.columnGap) !== null && _listConfig$columnGap !== void 0 ? _listConfig$columnGap : 12;
    var rowGap = (_listConfig$rowGap = listConfig.rowGap) !== null && _listConfig$rowGap !== void 0 ? _listConfig$rowGap : 12;
    var skeletonConfig = listConfig.skeletonConfig;
    var paginationType = listConfig.showPagination ? 'pager' : 'all';
    var enableTab = listConfig.showTab || false;
    var tabStyle = listConfig.tabStyle || 'switch';
    var isTreeTab = (_listConfig$isTreeTab2 = listConfig.isTreeTab) !== null && _listConfig$isTreeTab2 !== void 0 ? _listConfig$isTreeTab2 : true;
    var stickyTop = listConfig.stickyTop !== undefined ? listConfig.stickyTop : true;
    var filteredData = _filteredList.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);
        })) || []
      });
    });

    // 当 isTreeTab 为 false 时，按 tabData 分类顺序排序，再按商品 sort 降序
    if (!isTreeTab && tabData && Array.isArray(tabData)) {
      sortProductsByTabOrder(filteredData, tabData);
    }
    var emptyText = emptyDescription;
    if (Array.isArray(activeTab) && activeTab.length === 0 && keyword === '') {
      emptyText = locales.getText('pisell2.ticket-booking.input-search-to-show-data');
    }
    return /*#__PURE__*/React.createElement("div", {
      className: "sku-list-container ".concat(className || ''),
      style: style
    }, /*#__PURE__*/React.createElement(List, {
      ref: listRef,
      className: "sku-list",
      style: {
        maxHeight: '100%',
        overflow: 'auto'
      },
      data: filteredData,
      renderSearch: function renderSearch() {
        return null;
      },
      loading: loading,
      onTabSearch: setKeyword,
      displayStyle: displayStyle,
      columns: columns,
      columnGap: columnGap,
      rowGap: rowGap,
      paginationType: paginationType,
      layoutDirection: "vertical"
      // Tab 配置
      ,
      tabStyle: enableTab ? tabStyle : 'none',
      isTreeTab: isTreeTab,
      tabData: tabData,
      onActiveTabChange: onActiveTabChange,
      tabGroup: "category",
      stickyTop: stickyTop
      // 空状态配置
      ,
      emptyConfig: {
        show: !loading && filteredData.length === 0,
        text: emptyText
      },
      skeletonConfig: skeletonConfig,
      renderItem: function renderItem(item, index) {
        return (
          /*#__PURE__*/
          // <SkuCard
          //   key={item.id}
          //   {...skuCardAllConfig}
          //   dataSource={formatSkuDataSource(item)}
          //   onCardClick={() => onSelectProduct(item)}
          //   isSelected={item.id === currentProductId}
          // />
          React.createElement(ProductCard, {
            key: item.id,
            index: index,
            item: item,
            onSelectCover: function onSelectCover() {
              return onSelectProduct(item);
            },
            onSelectInfo: function onSelectInfo() {
              return onSelectProduct(item);
            },
            isSelected: item.id === currentProductId,
            scenario: "booking2"
            // decorateConfig={{...productConfig}}
            ,
            customSkuCardAllConfig: skuCardAllConfig
          })
        );
      }
    }));
  }, [config.skuListConfig, dataSource, loading, currentProductId, onSelectProduct, emptyDescription, style, className, _filteredList, tabData, productConfig, activeTab, keyword]);

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

  // 默认使用 product-select 模式
  return renderProductSelect;
};
var ForwardedProductDisplayAdapter = /*#__PURE__*/forwardRef(ProductDisplayAdapter);
ForwardedProductDisplayAdapter.displayName = "ProductDisplayAdapter";
export default withResponsive(ForwardedProductDisplayAdapter);