var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ticketBooking/components/ProductDisplayAdapter/ProductDisplayAdapter.tsx
var ProductDisplayAdapter_exports = {};
__export(ProductDisplayAdapter_exports, {
  default: () => ProductDisplayAdapter_default
});
module.exports = __toCommonJS(ProductDisplayAdapter_exports);
var import_react = __toESM(require("react"));
var import_productSelect = __toESM(require("../../../../plus/productSelect"));
var import_List = __toESM(require("../../../list/List"));
var import_ProductCard = __toESM(require("../../../../plus/productSelect/ProductCard"));
var import_utils = require("./utils");
var import_responsive = require("../../../../responsive");
var import_index = require("./index.less");
var import_utils2 = require("@pisell/utils");
var ProductDisplayAdapter = (props, ref) => {
  const {
    config,
    dataSource,
    loading = false,
    currentProductId,
    onSelectProduct,
    emptyDescription,
    style,
    className
  } = props;
  const { displayMode = "product-select" } = config;
  const listRef = (0, import_react.useRef)(null);
  const [keyword, setKeyword] = (0, import_react.useState)("");
  const [activeTab, setActiveTab] = (0, import_react.useState)(void 0);
  const onActiveTabChange = (0, import_react.useCallback)((tab) => {
    setActiveTab(tab);
  }, []);
  const renderProductSelect = (0, import_react.useMemo)(() => {
    const {
      showSearch = true,
      showTab = true,
      compact = false,
      layout = { grid: true, config: "12:12" },
      productListSetting = {
        isProductCover: 1,
        isTitle: 1,
        isSubtitle: 1,
        isPrice: 1,
        isOriginPrice: 0,
        isStock: 0
      }
    } = config.productSelectConfig || {};
    return /* @__PURE__ */ import_react.default.createElement(
      import_productSelect.default,
      {
        tabSwitchMode: "switch",
        dataSource,
        layout,
        onChange: onSelectProduct,
        value: currentProductId,
        loading,
        productListSetting,
        showSearch,
        showTab,
        emptyDescription,
        style,
        compact,
        scenario: "booking2"
      }
    );
  }, [
    config.productSelectConfig,
    dataSource,
    currentProductId,
    onSelectProduct,
    loading,
    emptyDescription,
    style
  ]);
  const _filteredList = (0, import_react.useMemo)(() => {
    if (Array.isArray(activeTab) && activeTab.length === 0 && !keyword) {
      return [];
    }
    if (!keyword) return dataSource;
    return dataSource.filter((product) => {
      if (typeof (product == null ? void 0 : product.title) !== "string") return false;
      return product.title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
    });
  }, [keyword, dataSource, activeTab]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    getTabbarRef: () => {
      var _a;
      return (_a = listRef.current) == null ? void 0 : _a.getTabbarRef();
    }
  }));
  const tabData = (0, import_react.useMemo)(() => {
    const { listConfig = {} } = config.skuListConfig || {};
    const enableTab = listConfig.showTab || false;
    const tabStyle = listConfig.tabStyle || "switch";
    const isTreeTab = listConfig.isTreeTab ?? true;
    return enableTab ? (0, import_utils.generateProductTabs)(dataSource, tabStyle, isTreeTab) : void 0;
  }, [dataSource]);
  const productConfig = (0, import_react.useMemo)(() => {
    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" }
    };
  }, []);
  const renderSkuList = (0, import_react.useMemo)(() => {
    const { listConfig = {}, skuCardAllConfig = {} } = config.skuListConfig || {};
    const displayStyle = listConfig.layout || "grid";
    const columns = listConfig.columns || 2;
    const columnGap = listConfig.columnGap ?? 12;
    const rowGap = listConfig.rowGap ?? 12;
    const skeletonConfig = listConfig.skeletonConfig;
    const paginationType = listConfig.showPagination ? "pager" : "all";
    const enableTab = listConfig.showTab || false;
    const tabStyle = listConfig.tabStyle || "switch";
    const isTreeTab = listConfig.isTreeTab ?? true;
    const stickyTop = listConfig.stickyTop !== void 0 ? listConfig.stickyTop : true;
    const filteredData = _filteredList.map((item) => {
      var _a, _b;
      return {
        ...item,
        category: ((_b = (_a = item.category) == null ? void 0 : _a.map) == null ? void 0 : _b.call(_a, (category) => `${category.id}`)) || []
      };
    });
    if (!isTreeTab && tabData && Array.isArray(tabData)) {
      (0, import_utils.sortProductsByTabOrder)(filteredData, tabData);
    }
    let emptyText = emptyDescription;
    if (Array.isArray(activeTab) && activeTab.length === 0 && keyword === "") {
      emptyText = import_utils2.locales.getText("pisell2.ticket-booking.input-search-to-show-data");
    }
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `sku-list-container ${className || ""}`,
        style
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_List.default,
        {
          ref: listRef,
          className: "sku-list",
          style: { maxHeight: "100%", overflow: "auto" },
          data: filteredData,
          renderSearch: () => null,
          loading,
          onTabSearch: setKeyword,
          displayStyle,
          columns,
          columnGap,
          rowGap,
          paginationType,
          layoutDirection: "vertical",
          tabStyle: enableTab ? tabStyle : "none",
          isTreeTab,
          tabData,
          onActiveTabChange,
          tabGroup: "category",
          stickyTop,
          emptyConfig: {
            show: !loading && filteredData.length === 0,
            text: emptyText
          },
          skeletonConfig,
          renderItem: (item, index) => (
            // <SkuCard
            //   key={item.id}
            //   {...skuCardAllConfig}
            //   dataSource={formatSkuDataSource(item)}
            //   onCardClick={() => onSelectProduct(item)}
            //   isSelected={item.id === currentProductId}
            // />
            /* @__PURE__ */ import_react.default.createElement(
              import_ProductCard.default,
              {
                key: item.id,
                index,
                item,
                onSelectCover: () => onSelectProduct(item),
                onSelectInfo: () => onSelectProduct(item),
                isSelected: item.id === currentProductId,
                scenario: "booking2",
                customSkuCardAllConfig: skuCardAllConfig
              }
            )
          )
        }
      )
    );
  }, [
    config.skuListConfig,
    dataSource,
    loading,
    currentProductId,
    onSelectProduct,
    emptyDescription,
    style,
    className,
    _filteredList,
    tabData,
    productConfig,
    activeTab,
    keyword
  ]);
  if (displayMode === "sku-list") {
    return renderSkuList;
  }
  return renderProductSelect;
};
var ForwardedProductDisplayAdapter = (0, import_react.forwardRef)(ProductDisplayAdapter);
ForwardedProductDisplayAdapter.displayName = `ProductDisplayAdapter`;
var ProductDisplayAdapter_default = (0, import_responsive.withResponsive)(ForwardedProductDisplayAdapter);
