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/tabProduct/index.tsx
var tabProduct_exports = {};
__export(tabProduct_exports, {
  default: () => tabProduct_default
});
module.exports = __toCommonJS(tabProduct_exports);
var import_react = __toESM(require("react"));
var import_tabs = __toESM(require("./tabs"));
var import_card = __toESM(require("./card"));
var import_classnames = __toESM(require("classnames"));
var import_index = require("./index.less");
var io = null;
var tabsHeight = 92;
var TabProduct = (props) => {
  const {
    list = [],
    scrollDom,
    rowKey = "id",
    tabsWrapClassName,
    categoryWrapClassName,
    listWrapClassName,
    renderItem,
    warpClassName,
    stickyTop = 0,
    name
  } = props;
  const tabsRef = (0, import_react.useRef)();
  const [current, setCurrent] = (0, import_react.useState)(1);
  (0, import_react.useEffect)(() => {
    if (list == null ? void 0 : list.length) {
      setCurrent(list[0][rowKey]);
      handleObserver();
    }
  }, [list]);
  const handleObserver = () => {
    var _a;
    const domHeight = scrollDom ? ((_a = document.querySelector(scrollDom)) == null ? void 0 : _a.clientHeight) || window.innerHeight : window.innerHeight;
    const marginTop = `-${tabsHeight}px`;
    const marginBottom = `-${domHeight - tabsHeight - 10}px`;
    io = new IntersectionObserver(
      (entries) => {
        var _a2, _b, _c, _d;
        console.log(entries);
        if (entries[0].isIntersecting) {
          const [name2, id] = (_c = (_b = (_a2 = entries[0]) == null ? void 0 : _a2.target) == null ? void 0 : _b.id) == null ? void 0 : _c.split("-");
          setCurrent(Number(id));
          (_d = tabsRef == null ? void 0 : tabsRef.current) == null ? void 0 : _d.scroll(id);
        }
      },
      {
        root: scrollDom ? document.querySelector(scrollDom) : null,
        rootMargin: `${marginTop} 0px ${marginBottom}`
        // threshold: 0.5,
        // rootMargin: ,
      }
    );
    if (list == null ? void 0 : list.length) {
      for (const item of list) {
        const _item = document.querySelector(`#list-${item[rowKey]}`);
        if (_item) {
          io.observe(_item);
        }
      }
    }
  };
  const handleUnObserver = () => {
    if ((list == null ? void 0 : list.length) && io) {
      for (const item of list) {
        const _item = document.querySelector(`#list-${item[rowKey]}`);
        if (_item) {
          io.unobserve(_item);
        }
      }
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("tab-product", warpClassName) }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "tabs-wrap",
      style: {
        top: stickyTop
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_card.default,
      {
        className: (0, import_classnames.default)("tabs", tabsWrapClassName),
        id: `${name || "product"}-category`
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_tabs.default,
        {
          ref: tabsRef,
          options: list,
          rowKey,
          tab: current,
          name,
          onChange: (e) => {
            const dom = document == null ? void 0 : document.querySelector("#list-" + e);
            const main = document == null ? void 0 : document.querySelector(scrollDom || "");
            if (dom && main) {
              main.scrollTop = dom.offsetTop - 42;
            }
            handleUnObserver();
            setCurrent(e);
            setTimeout(() => {
              handleObserver();
            }, 1e3);
          }
        }
      )
    )
  ), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("category-list", categoryWrapClassName) }, list.map((d, i) => {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "category-item", id: `list-${d[rowKey]}`, key: i }, /* @__PURE__ */ import_react.default.createElement("div", { className: "category-title" }, d.name), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("product-list", listWrapClassName) }, d.products.map((item, index) => {
      if (renderItem) {
        return renderItem(item);
      }
      return /* @__PURE__ */ import_react.default.createElement("div", { key: index }, index);
    })));
  })));
};
var tabProduct_default = TabProduct;
