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/PisellTabbar/template/Template1/PisellTabbar.tsx
var PisellTabbar_exports = {};
__export(PisellTabbar_exports, {
  default: () => PisellTabbar_default
});
module.exports = __toCommonJS(PisellTabbar_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_PisellCards = __toESM(require("../../../PisellCards"));
var import_PisellSuperTabs = __toESM(require("../../../PisellSuperTabs"));
var import_iconfont = __toESM(require("../../../iconfont"));
var import_pisellFind = require("../../../pisellFind");
var import_PisellTabbar = require("../../PisellTabbar.less");
var import_hooks = require("../../hooks");
var import_mock = __toESM(require("../../mock"));
var import_constants = require("./constants");
var import_utils = require("./utils");
var tabbarInstanceSequence = 0;
function createTabbarInstanceId() {
  tabbarInstanceSequence += 1;
  return `sku-list-tabbar-temp1-${tabbarInstanceSequence}`;
}
function renderCategoryIcon(icon, size) {
  if (/^(?:https?:\/\/|data:|blob:|\/)/i.test(icon)) {
    return /* @__PURE__ */ import_react.default.createElement(
      "img",
      {
        alt: "",
        src: icon,
        style: {
          width: size,
          height: size,
          borderRadius: 6,
          objectFit: "cover"
        }
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: icon, style: { fontSize: size } });
}
var PisellTabbar = (props, ref) => {
  var _a, _b;
  const {
    dataSource = (props == null ? void 0 : props.__designMode) ? import_mock.default : import_mock.default,
    value,
    onChange,
    className,
    style,
    levelConfig: customLevelConfig,
    expandConfig: customExpandConfig,
    maxLevel = 3,
    onSearch,
    searchProps,
    level1Actions,
    direction = "horizontal",
    showAllItem = true,
    showSearchItem = true,
    scrollActiveItemIntoView = false
  } = props;
  const isVertical = direction === "vertical";
  const containerRef = (0, import_react.useRef)(null);
  const level1TabsRef = (0, import_react.useRef)(null);
  const searchRef = (0, import_react.useRef)(null);
  const firstCategoryKey = ((_a = dataSource[0]) == null ? void 0 : _a.id) ?? ((_b = dataSource[0]) == null ? void 0 : _b.key);
  const initialActiveKey = value ?? (showAllItem ? (0, import_utils.getDefaultActiveKey)(dataSource, "id") : firstCategoryKey !== void 0 ? [firstCategoryKey] : []);
  const tabbarIdRef = (0, import_react.useRef)();
  if (!tabbarIdRef.current) {
    tabbarIdRef.current = createTabbarInstanceId();
  }
  const tabbarId = tabbarIdRef.current;
  const {
    setActiveKey,
    getActiveKey,
    _setActiveKey,
    activeKey,
    setCacheActiveKey,
    resetActiveKey
  } = (0, import_hooks.useActiveKey)({
    initialActiveKey,
    onChange
  });
  const { expand, toggleExpand, getExpand } = (0, import_hooks.useExpand)();
  const expandConfig = customExpandConfig || (0, import_constants.getExpandConfig)();
  const level1Value = (0, import_react.useMemo)(() => {
    return getActiveKey(0);
  }, [activeKey]);
  const level2Value = (0, import_react.useMemo)(() => {
    return getActiveKey(1);
  }, [activeKey]);
  const level3Value = (0, import_react.useMemo)(() => {
    return getActiveKey(2);
  }, [activeKey]);
  const [isSearchOpen, setIsSearchOpen] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    if (!scrollActiveItemIntoView || level1Value == null) return;
    const animationFrame = requestAnimationFrame(() => {
      var _a2, _b2;
      (_b2 = (_a2 = level1TabsRef.current) == null ? void 0 : _a2.scrollItemByRowKey) == null ? void 0 : _b2.call(_a2, String(level1Value));
    });
    return () => cancelAnimationFrame(animationFrame);
  }, [level1Value, scrollActiveItemIntoView]);
  const openSearchMode = () => {
    setCacheActiveKey(activeKey);
    _setActiveKey([]);
    onChange == null ? void 0 : onChange([]);
    setIsSearchOpen(true);
  };
  const closeSearchMode = () => {
    onSearch == null ? void 0 : onSearch("");
    resetActiveKey();
    setIsSearchOpen(false);
  };
  (0, import_react.useImperativeHandle)(ref, () => {
    if (props == null ? void 0 : props.__designMode) {
      return containerRef.current;
    }
    return {
      closeSearchMode: () => {
        var _a2;
        return (_a2 = searchRef.current) == null ? void 0 : _a2.close();
      }
    };
  });
  (0, import_react.useEffect)(() => {
    if (value) {
      _setActiveKey(value);
    }
  }, [JSON.stringify(value)]);
  const getLevelConfig = (0, import_react.useCallback)(
    (level) => {
      return {
        ...import_constants.DEFAULT_LEVEL_CONFIG[level],
        ...customLevelConfig == null ? void 0 : customLevelConfig[level]
      };
    },
    [customLevelConfig]
  );
  const handleLevel1Change = (0, import_react.useCallback)(
    (key, item) => {
      var _a2;
      setActiveKey(
        0,
        key,
        showAllItem && ((_a2 = item == null ? void 0 : item.children) == null ? void 0 : _a2.length) ? 0 : void 0
      );
    },
    [setActiveKey, activeKey, showAllItem]
  );
  const handleLevel2Change = (0, import_react.useCallback)(
    (key, item) => {
      var _a2;
      setActiveKey(
        1,
        key,
        showAllItem && ((_a2 = item == null ? void 0 : item.children) == null ? void 0 : _a2.length) ? 0 : void 0
      );
    },
    [setActiveKey, activeKey, showAllItem]
  );
  const handleLevel3Change = (0, import_react.useCallback)(
    (key) => {
      setActiveKey(2, key);
    },
    [setActiveKey, activeKey]
  );
  const renderExpandCard = (0, import_react.useCallback)(
    (cardProps, level, isExpanded) => {
      var _a2;
      const config = expandConfig[isExpanded ? "active" : "inactive"];
      const levelCfg = getLevelConfig(level);
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellCards.default.TextCard,
        {
          ...cardProps,
          text: config.text,
          variant: "outlined",
          widthMode: "auto",
          prefixIcon: /* @__PURE__ */ import_react.default.createElement(
            import_iconfont.default,
            {
              type: config.icon,
              style: { color: "#000", fontSize: 20 }
            }
          ),
          textConfig: {
            textAlign: "center",
            color: "#000000",
            flex: "none"
          },
          style: {
            borderRadius: "20px",
            background: "#ffffff",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            height: levelCfg.cardHeight,
            minWidth: 100
          },
          width: "auto",
          active: {
            ...cardProps.active,
            style: {
              ...(_a2 = cardProps.active) == null ? void 0 : _a2.style,
              color: "#ffffff",
              background: "#7f56d9"
            }
          }
        }
      );
    },
    [expandConfig, activeKey, getLevelConfig]
  );
  const renderSearch = (0, import_react.useCallback)(
    (itemProps, level) => {
      return /* @__PURE__ */ import_react.default.createElement(
        import_pisellFind.PisellFind,
        {
          showHistory: false,
          modalWidth: "100%",
          ...searchProps,
          visible: isSearchOpen,
          enableScanner: Boolean(searchProps == null ? void 0 : searchProps.enableScanner) && isSearchOpen,
          ref: searchRef,
          onSearch: (e) => {
            onSearch == null ? void 0 : onSearch(e);
          },
          getContainer: () => {
            var _a2;
            const customContainer = (_a2 = searchProps == null ? void 0 : searchProps.getContainer) == null ? void 0 : _a2.call(searchProps);
            return customContainer || document.querySelector(`#${tabbarId}`) || document.body;
          },
          modalStyle: {
            width: "100%"
          },
          onOpen: () => {
            var _a2;
            openSearchMode();
            (_a2 = searchProps == null ? void 0 : searchProps.onOpen) == null ? void 0 : _a2.call(searchProps);
          },
          onClose: () => {
            var _a2;
            closeSearchMode();
            (_a2 = searchProps == null ? void 0 : searchProps.onClose) == null ? void 0 : _a2.call(searchProps);
          },
          trigger: /* @__PURE__ */ import_react.default.createElement(
            import_PisellCards.default.MultilevelCard,
            {
              ...itemProps,
              card: (cardProps) => {
                let _cardProps = {
                  ...cardProps,
                  text: /* @__PURE__ */ import_react.default.createElement(
                    "div",
                    {
                      style: {
                        display: "flex",
                        alignItems: "center",
                        justifyContent: "center"
                      }
                    },
                    /* @__PURE__ */ import_react.default.createElement(
                      import_iconfont.default,
                      {
                        type: "pisell2-search-lg",
                        style: { color: "#000", fontSize: 20 }
                      }
                    )
                  )
                };
                return renderNormalCard(_cardProps, level);
              }
            }
          )
        }
      );
    },
    [activeKey, tabbarId, isSearchOpen, isVertical, onSearch, searchProps]
  );
  const renderNormalCard = (0, import_react.useCallback)(
    (cardProps, level) => {
      var _a2;
      const { dataSource: itemData } = cardProps;
      const levelCfg = getLevelConfig(level);
      const textAlign = isVertical ? levelCfg.textAlign ?? "center" : "center";
      const iconSize = levelCfg.iconSize ?? 28;
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellCards.default.TextCard,
        {
          text: itemData.name,
          ...cardProps,
          variant: "outlined",
          widthMode: isVertical ? "full" : "auto",
          prefixIcon: levelCfg.showIcon && itemData.icon ? renderCategoryIcon(itemData.icon, iconSize) : void 0,
          textConfig: {
            textAlign,
            color: "#000",
            maxLines: 2,
            ellipsis: true
          },
          style: {
            ...isVertical ? { minHeight: levelCfg.cardHeight } : { height: levelCfg.cardHeight },
            borderRadius: isVertical ? levelCfg.borderRadius ?? "999px" : "20px",
            background: levelCfg.cardBackground ?? "#ffffff",
            minWidth: isVertical ? 0 : 100,
            justifyContent: isVertical && textAlign === "left" ? "flex-start" : "center"
          },
          width: isVertical ? "100%" : "auto",
          active: {
            ...cardProps.active,
            style: {
              ...(_a2 = cardProps.active) == null ? void 0 : _a2.style,
              color: "#fff",
              background: levelCfg.activeBackground ?? "#7f56d9"
            }
          }
        }
      );
    },
    [getLevelConfig, isVertical]
  );
  const level1DataSource = (0, import_react.useMemo)(
    () => {
      const categoryItems = dataSource.length ? (0, import_utils.addDataSourceItemsByKey)(
        dataSource,
        import_constants.LEVEL_ADD_ITEMS.level1.filter(
          (itemKey) => (showAllItem || itemKey !== "all") && (showSearchItem || itemKey !== "search")
        )
      ) : [];
      const actionItems = (level1Actions || []).map((action, index) => ({
        id: `__level1-action-${String(action.key)}-${index}`,
        key: `__level1-action-${String(action.key)}-${index}`,
        name: "",
        customClick: true,
        __action: action
      }));
      const full = [...categoryItems, ...actionItems];
      if (isSearchOpen) {
        return full.filter((item) => (item == null ? void 0 : item.key) === "search");
      }
      return isVertical ? full.filter((item) => (item == null ? void 0 : item.key) !== "expand") : full;
    },
    [
      dataSource,
      isSearchOpen,
      isVertical,
      level1Actions,
      showAllItem,
      showSearchItem
    ]
  );
  const renderLevel1Item = (0, import_react.useCallback)(
    (itemProps) => {
      var _a2;
      const { dataSource: itemData } = itemProps;
      const isExpanded = getExpand("level1");
      const action = itemData == null ? void 0 : itemData.__action;
      if (action) {
        return /* @__PURE__ */ import_react.default.createElement(
          import_PisellCards.default.MultilevelCard,
          {
            ...itemProps,
            "aria-label": action.ariaLabel,
            onClick: action.onClick,
            card: (cardProps) => renderNormalCard(
              {
                ...cardProps,
                text: action.label ?? action.icon
              },
              "level1"
            )
          }
        );
      }
      if (itemData == null ? void 0 : itemData.customClick) {
        if (itemProps.dataSource.key === "search") {
          return renderSearch(itemProps, "level1");
        }
        if (itemProps.dataSource.key === "expand") {
          return /* @__PURE__ */ import_react.default.createElement(
            import_PisellCards.default.MultilevelCard,
            {
              ...itemProps,
              card: (cardProps) => renderExpandCard(cardProps, "level1", isExpanded),
              onClick: () => toggleExpand("level1")
            }
          );
        }
        return null;
      }
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellCards.default.MultilevelCard,
        {
          ...itemProps,
          className: (0, import_classnames.default)(itemProps.className, {
            "pisell-tabbar__category-group--level1": isVertical && ((_a2 = itemData == null ? void 0 : itemData.children) == null ? void 0 : _a2.length)
          }),
          card: (cardProps) => renderNormalCard(cardProps, "level1"),
          panel: (panelProps) => {
            var _a3, _b2;
            if (((_b2 = (_a3 = panelProps == null ? void 0 : panelProps.dataSource) == null ? void 0 : _a3.children) == null ? void 0 : _b2.length) && maxLevel >= 2) {
              return renderLevel2Panel(panelProps);
            }
            return null;
          },
          panelClassName: isVertical ? "pisell-tabbar__inline-panel" : void 0,
          panelContainer: isVertical ? void 0 : () => document.querySelector(
            `.pisell-super-tabs-slot-bottom-${tabbarId}`
          )
        }
      );
    },
    [
      getLevelConfig,
      getExpand,
      toggleExpand,
      maxLevel,
      activeKey,
      tabbarId,
      isVertical
    ]
  );
  const renderLevel2Panel = (0, import_react.useCallback)(
    (panelProps) => {
      if (maxLevel < 2) return null;
      let _dataSource = panelProps.dataSource.children || [];
      if (_dataSource.length) {
        _dataSource = (0, import_utils.addDataSourceItemsByKey)(
          _dataSource,
          import_constants.LEVEL_ADD_ITEMS.level2.filter(
            (itemKey) => showAllItem || itemKey !== "all"
          )
        );
        if (isVertical) {
          _dataSource = _dataSource.filter((item) => item.key !== "expand");
        }
      }
      const level2Config = getLevelConfig("level2");
      const isExpanded2 = getExpand("level2");
      const skuId = `sku-list-tabbar-${panelProps.dataSource.id}`;
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellSuperTabs.default,
        {
          id: skuId,
          className: (0, import_classnames.default)(
            "pisell-tabbar__level",
            "pisell-tabbar__level--2"
          ),
          isControlled: true,
          tabItemStyle: isVertical ? { width: "100%", height: "auto" } : {},
          dataSource: _dataSource,
          value: level2Value,
          onChange: handleLevel2Change,
          rowKey: "id",
          style: {
            flex: isVertical ? "none" : 1,
            height: isVertical ? "auto" : void 0,
            background: level2Config.superTabsBg
          },
          gridProps: {
            direction,
            gap: 4,
            wrap: !isVertical && isExpanded2 ? "wrap" : "nowrap",
            startSlot: isVertical ? void 0 : /* @__PURE__ */ import_react.default.createElement("div", { style: { paddingLeft: level2Config.paddingLeft } }, /* @__PURE__ */ import_react.default.createElement(
              import_iconfont.default,
              {
                type: "pisell2-trend-down-01",
                style: { color: "#fff", fontSize: 24, marginRight: 8 }
              }
            ))
          },
          scrollViewProps: {
            style: {
              height: isVertical ? "auto" : void 0,
              padding: isVertical ? "4px 0 4px 12px" : "4px 8px"
            },
            overflow: isVertical ? "hidden" : !isExpanded2 ? "x" : "hidden",
            scrollbarStyle: "none",
            scrollActionsConfig: {
              type: isVertical ? "tbArrows" : "lrArrows",
              show: !isVertical
            }
          },
          renderItem: (itemProps) => {
            var _a2;
            const { dataSource: itemData } = itemProps;
            if (itemData == null ? void 0 : itemData.customClick) {
              return /* @__PURE__ */ import_react.default.createElement(
                import_PisellCards.default.MultilevelCard,
                {
                  ...itemProps,
                  card: (cardProps) => renderExpandCard(cardProps, "level2", isExpanded2),
                  onClick: () => toggleExpand("level2")
                }
              );
            }
            return /* @__PURE__ */ import_react.default.createElement(
              import_PisellCards.default.MultilevelCard,
              {
                ...itemProps,
                className: (0, import_classnames.default)(itemProps.className, {
                  "pisell-tabbar__category-group--level2": isVertical && ((_a2 = itemData == null ? void 0 : itemData.children) == null ? void 0 : _a2.length)
                }),
                card: (cardProps) => renderNormalCard(cardProps, "level2"),
                panel: (panelProps2) => {
                  var _a3, _b2;
                  if (((_b2 = (_a3 = panelProps2 == null ? void 0 : panelProps2.dataSource) == null ? void 0 : _a3.children) == null ? void 0 : _b2.length) && maxLevel >= 3) {
                    return renderLevel3Panel(panelProps2);
                  }
                  return null;
                },
                panelClassName: isVertical ? "pisell-tabbar__inline-panel" : void 0,
                panelContainer: isVertical ? void 0 : () => document.querySelector(
                  `.pisell-super-tabs-slot-bottom-${skuId}`
                )
              }
            );
          }
        }
      );
    },
    [
      level2Value,
      maxLevel,
      activeKey,
      getActiveKey,
      handleLevel2Change,
      getExpand,
      toggleExpand,
      getLevelConfig,
      renderExpandCard,
      renderNormalCard,
      showAllItem,
      direction,
      isVertical
    ]
  );
  const renderLevel3Panel = (0, import_react.useCallback)(
    (panelProps2) => {
      if (maxLevel < 3) return null;
      let _dataSource2 = panelProps2.dataSource.children || [];
      if (_dataSource2.length) {
        _dataSource2 = (0, import_utils.addDataSourceItemsByKey)(
          _dataSource2,
          import_constants.LEVEL_ADD_ITEMS.level3.filter(
            (itemKey) => showAllItem || itemKey !== "all"
          )
        );
        if (isVertical) {
          _dataSource2 = _dataSource2.filter((item) => item.key !== "expand");
        }
      }
      const level3Config = getLevelConfig("level3");
      const isExpanded3 = getExpand("level3");
      const skuId = `sku-list-tabbar-${panelProps2.dataSource.id}`;
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellSuperTabs.default,
        {
          id: skuId,
          className: (0, import_classnames.default)(
            "pisell-tabbar__level",
            "pisell-tabbar__level--3"
          ),
          isControlled: true,
          tabItemStyle: isVertical ? { width: "100%", height: "auto" } : {},
          dataSource: _dataSource2,
          value: level3Value,
          onChange: handleLevel3Change,
          rowKey: "id",
          style: {
            flex: isVertical ? "none" : 1,
            height: isVertical ? "auto" : void 0,
            background: level3Config.superTabsBg
          },
          gridProps: {
            direction,
            gap: 4,
            wrap: !isVertical && isExpanded3 ? "wrap" : "nowrap",
            startSlot: isVertical ? void 0 : /* @__PURE__ */ import_react.default.createElement("div", { style: { paddingLeft: level3Config.paddingLeft } }, /* @__PURE__ */ import_react.default.createElement(
              import_iconfont.default,
              {
                type: "pisell2-trend-down-01",
                style: { color: "#fff", fontSize: 24, marginRight: 8 }
              }
            ))
          },
          scrollViewProps: {
            style: {
              height: isVertical ? "auto" : void 0,
              padding: isVertical ? "4px 0 4px 12px" : "4px 8px"
            },
            overflow: isVertical ? "hidden" : !isExpanded3 ? "x" : "hidden",
            scrollbarStyle: "none",
            scrollActionsConfig: {
              type: isVertical ? "tbArrows" : "lrArrows",
              show: !isVertical
            }
          },
          renderItem: (itemProps) => {
            const { dataSource: itemData } = itemProps;
            if (itemData == null ? void 0 : itemData.customClick) {
              return /* @__PURE__ */ import_react.default.createElement(
                import_PisellCards.default.MultilevelCard,
                {
                  ...itemProps,
                  card: (cardProps) => renderExpandCard(cardProps, "level3", isExpanded3),
                  onClick: () => toggleExpand("level3")
                }
              );
            }
            return /* @__PURE__ */ import_react.default.createElement(
              import_PisellCards.default.MultilevelCard,
              {
                ...itemProps,
                card: (cardProps) => renderNormalCard(cardProps, "level3")
              }
            );
          }
        }
      );
    },
    [
      level3Value,
      maxLevel,
      activeKey,
      getActiveKey,
      handleLevel3Change,
      getExpand,
      toggleExpand,
      getLevelConfig,
      renderExpandCard,
      renderNormalCard,
      showAllItem,
      direction,
      isVertical
    ]
  );
  const level1Config = getLevelConfig("level1");
  const isExpanded1 = getExpand("level1");
  return level1DataSource.length ? /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-tabbar", className, {
        "pisell-tabbar--vertical": isVertical
      }),
      style,
      ref: containerRef
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_PisellSuperTabs.default,
      {
        ref: level1TabsRef,
        isControlled: true,
        id: tabbarId,
        className: (0, import_classnames.default)(
          "pisell-tabbar__level",
          "pisell-tabbar__level--1"
        ),
        tabItemStyle: isVertical ? { width: "100%", height: "auto" } : {},
        dataSource: level1DataSource,
        value: level1Value,
        onChange: handleLevel1Change,
        rowKey: "id",
        style: {
          flex: 1,
          minHeight: isVertical ? 0 : void 0,
          background: level1Config.superTabsBg
        },
        gridProps: {
          direction,
          gap: 4,
          wrap: !isVertical && isExpanded1 ? "wrap" : "nowrap"
        },
        scrollViewProps: {
          style: {
            height: isVertical ? "100%" : void 0,
            padding: isVertical ? 8 : "4px 8px"
          },
          overflow: isVertical ? "y" : !isExpanded1 ? "x" : "hidden",
          scrollbarStyle: "none",
          scrollActionsConfig: {
            type: isVertical ? "tbArrows" : "lrArrows",
            show: true
          }
        },
        renderItem: renderLevel1Item
      }
    )
  ) : null;
};
PisellTabbar.displayName = "PisellTabbar";
var PisellTabbar_default = (0, import_react.forwardRef)(PisellTabbar);
