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_locales = require("../../../../locales");
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 tabbarId = "sku-list-tabbar-temp1";
var PisellTabbar = (props, ref) => {
  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
  } = props;
  const containerRef = (0, import_react.useRef)(null);
  const searchRef = (0, import_react.useRef)(null);
  const {
    setActiveKey,
    getActiveKey,
    _setActiveKey,
    activeKey,
    setCacheActiveKey,
    resetActiveKey
  } = (0, import_hooks.useActiveKey)({
    initialActiveKey: value || (0, import_utils.getDefaultActiveKey)(dataSource, "id"),
    onChange
  });
  console.log("getActiveKey", activeKey);
  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 openSearchMode = () => {
    setCacheActiveKey(activeKey);
    _setActiveKey([]);
    onChange == null ? void 0 : onChange([]);
  };
  const closeSearchMode = () => {
    onSearch == null ? void 0 : onSearch("");
    resetActiveKey();
  };
  (0, import_react.useImperativeHandle)(ref, () => {
    var _a;
    if (props == null ? void 0 : props.__designMode) {
      return containerRef.current;
    }
    return {
      closeSearchMode: (_a = searchRef == null ? void 0 : searchRef.current) == null ? void 0 : _a.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 _a;
      setActiveKey(0, key, ((_a = item == null ? void 0 : item.children) == null ? void 0 : _a.length) ? 0 : void 0);
    },
    [setActiveKey, activeKey]
  );
  const handleLevel2Change = (0, import_react.useCallback)(
    (key, item) => {
      var _a;
      setActiveKey(1, key, ((_a = item == null ? void 0 : item.children) == null ? void 0 : _a.length) ? 0 : void 0);
    },
    [setActiveKey, activeKey]
  );
  const handleLevel3Change = (0, import_react.useCallback)(
    (key) => {
      setActiveKey(2, key);
    },
    [setActiveKey, activeKey]
  );
  const renderExpandCard = (0, import_react.useCallback)(
    (cardProps, level, isExpanded) => {
      var _a;
      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: {
              ...(_a = cardProps.active) == null ? void 0 : _a.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%",
          ref: searchRef,
          onSearch: (e) => {
            onSearch == null ? void 0 : onSearch(e);
          },
          getContainer: () => {
            return document.querySelector(`#${tabbarId}`) || document.body;
          },
          modalStyle: {
            width: "100%"
          },
          onOpen: openSearchMode,
          onClose: closeSearchMode,
          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",
                        gap: 4
                      }
                    },
                    /* @__PURE__ */ import_react.default.createElement(
                      import_iconfont.default,
                      {
                        type: "pisell2-search-lg",
                        style: { color: "#000", fontSize: 20 }
                      }
                    ),
                    /* @__PURE__ */ import_react.default.createElement(
                      import_iconfont.default,
                      {
                        type: "pisell2-scan",
                        style: { color: "#000", fontSize: 20 }
                      }
                    ),
                    /* @__PURE__ */ import_react.default.createElement("span", null, (0, import_locales.getText)("table-filter-find"))
                  )
                };
                return renderNormalCard(_cardProps, level);
              }
            }
          )
        }
      );
    },
    [activeKey]
  );
  const renderNormalCard = (0, import_react.useCallback)(
    (cardProps, level) => {
      var _a;
      const { dataSource: itemData } = cardProps;
      const levelCfg = getLevelConfig(level);
      return /* @__PURE__ */ import_react.default.createElement(
        import_PisellCards.default.TextCard,
        {
          text: itemData.name,
          ...cardProps,
          variant: "outlined",
          widthMode: "auto",
          textConfig: {
            textAlign: "center",
            color: "#000",
            maxLines: 2,
            ellipsis: true
          },
          style: {
            height: levelCfg.cardHeight,
            borderRadius: "20px",
            background: "#ffffff",
            minWidth: 100
          },
          width: "auto",
          active: {
            ...cardProps.active,
            style: {
              ...(_a = cardProps.active) == null ? void 0 : _a.style,
              color: "#fff",
              background: "#7f56d9"
            }
          }
        }
      );
    },
    [getLevelConfig]
  );
  const level1DataSource = (0, import_react.useMemo)(
    () => (0, import_utils.addDataSourceItemsByKey)(dataSource, import_constants.LEVEL_ADD_ITEMS.level1),
    [dataSource]
  );
  const renderLevel1Item = (0, import_react.useCallback)(
    (itemProps) => {
      const { dataSource: itemData } = itemProps;
      const isExpanded = getExpand("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,
          card: (cardProps) => renderNormalCard(cardProps, "level1"),
          panel: (panelProps) => {
            var _a, _b;
            if (((_b = (_a = panelProps == null ? void 0 : panelProps.dataSource) == null ? void 0 : _a.children) == null ? void 0 : _b.length) && maxLevel >= 2) {
              return renderLevel2Panel(panelProps);
            }
            return null;
          },
          panelContainer: () => document.querySelector(`.pisell-super-tabs-slot-bottom-${tabbarId}`)
        }
      );
    },
    [getLevelConfig, getExpand, toggleExpand, maxLevel, activeKey]
  );
  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
        );
      }
      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,
          isControlled: true,
          tabItemStyle: {},
          dataSource: _dataSource,
          value: level2Value,
          onChange: handleLevel2Change,
          rowKey: "id",
          style: {
            flex: 1,
            background: level2Config.superTabsBg
          },
          gridProps: {
            direction: "horizontal",
            gap: 4,
            wrap: isExpanded2 ? "wrap" : "nowrap",
            startSlot: /* @__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: {
              padding: "4px 8px"
            },
            overflow: !isExpanded2 ? "x" : "hidden",
            scrollbarStyle: "none",
            scrollActionsConfig: {
              type: "lrArrows",
              show: true
            }
          },
          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, "level2", isExpanded2),
                  onClick: () => toggleExpand("level2")
                }
              );
            }
            return /* @__PURE__ */ import_react.default.createElement(
              import_PisellCards.default.MultilevelCard,
              {
                ...itemProps,
                card: (cardProps) => renderNormalCard(cardProps, "level2"),
                panel: (panelProps2) => {
                  var _a, _b;
                  if (((_b = (_a = panelProps2 == null ? void 0 : panelProps2.dataSource) == null ? void 0 : _a.children) == null ? void 0 : _b.length) && maxLevel >= 3) {
                    return renderLevel3Panel(panelProps2);
                  }
                  return null;
                },
                panelContainer: () => document.querySelector(
                  `.pisell-super-tabs-slot-bottom-${skuId}`
                )
              }
            );
          }
        }
      );
    },
    [
      level2Value,
      maxLevel,
      activeKey,
      getActiveKey,
      handleLevel2Change,
      getExpand,
      toggleExpand,
      getLevelConfig,
      renderExpandCard,
      renderNormalCard
    ]
  );
  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
        );
      }
      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,
          isControlled: true,
          tabItemStyle: {},
          dataSource: _dataSource2,
          value: level3Value,
          onChange: handleLevel3Change,
          rowKey: "id",
          style: {
            flex: 1,
            background: level3Config.superTabsBg
          },
          gridProps: {
            direction: "horizontal",
            gap: 4,
            wrap: isExpanded3 ? "wrap" : "nowrap",
            startSlot: /* @__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: {
              padding: "4px 8px"
            },
            overflow: !isExpanded3 ? "x" : "hidden",
            scrollbarStyle: "none",
            scrollActionsConfig: {
              type: "lrArrows",
              show: true
            }
          },
          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
    ]
  );
  const level1Config = getLevelConfig("level1");
  const isExpanded1 = getExpand("level1");
  return dataSource.length ? /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-tabbar", className),
      style,
      ref: containerRef
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_PisellSuperTabs.default,
      {
        isControlled: true,
        id: tabbarId,
        tabItemStyle: {},
        dataSource: level1DataSource,
        value: level1Value,
        onChange: handleLevel1Change,
        rowKey: "id",
        style: {
          flex: 1,
          background: level1Config.superTabsBg
        },
        gridProps: {
          direction: "horizontal",
          gap: 4,
          wrap: isExpanded1 ? "wrap" : "nowrap"
        },
        scrollViewProps: {
          style: {
            padding: "4px 8px"
          },
          overflow: !isExpanded1 ? "x" : "hidden",
          scrollbarStyle: "none",
          scrollActionsConfig: {
            type: "lrArrows",
            show: true
          }
        },
        renderItem: renderLevel1Item
      }
    )
  ) : null;
};
PisellTabbar.displayName = "PisellTabbar";
var PisellTabbar_default = (0, import_react.forwardRef)(PisellTabbar);
