function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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, { useEffect, useState, useRef } from 'react';
import Tabs from "./tabs";
import Card from "./card";
import classNames from 'classnames';
import "./index.less";
var io = null;
var tabsHeight = 92;
var TabProduct = function TabProduct(props) {
  var _props$list = props.list,
    list = _props$list === void 0 ? [] : _props$list,
    scrollDom = props.scrollDom,
    _props$rowKey = props.rowKey,
    rowKey = _props$rowKey === void 0 ? 'id' : _props$rowKey,
    tabsWrapClassName = props.tabsWrapClassName,
    categoryWrapClassName = props.categoryWrapClassName,
    listWrapClassName = props.listWrapClassName,
    renderItem = props.renderItem,
    warpClassName = props.warpClassName,
    _props$stickyTop = props.stickyTop,
    stickyTop = _props$stickyTop === void 0 ? 0 : _props$stickyTop,
    name = props.name;
  var tabsRef = useRef();
  var _useState = useState(1),
    _useState2 = _slicedToArray(_useState, 2),
    current = _useState2[0],
    setCurrent = _useState2[1];
  useEffect(function () {
    if (list !== null && list !== void 0 && list.length) {
      setCurrent(list[0][rowKey]);
      handleObserver();
    }
  }, [list]);
  var handleObserver = function handleObserver() {
    var _document$querySelect;
    var domHeight = scrollDom ? ((_document$querySelect = document.querySelector(scrollDom)) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.clientHeight) || window.innerHeight : window.innerHeight;
    var marginTop = "-".concat(tabsHeight, "px");
    var marginBottom = "-".concat(domHeight - tabsHeight - 10, "px");
    io = new IntersectionObserver(function (entries) {
      console.log(entries);
      if (entries[0].isIntersecting) {
        var _entries$, _tabsRef$current;
        var _entries$0$target$id$ = (_entries$ = entries[0]) === null || _entries$ === void 0 || (_entries$ = _entries$.target) === null || _entries$ === void 0 || (_entries$ = _entries$.id) === null || _entries$ === void 0 ? void 0 : _entries$.split('-'),
          _entries$0$target$id$2 = _slicedToArray(_entries$0$target$id$, 2),
          _name = _entries$0$target$id$2[0],
          id = _entries$0$target$id$2[1];
        setCurrent(Number(id));
        tabsRef === null || tabsRef === void 0 || (_tabsRef$current = tabsRef.current) === null || _tabsRef$current === void 0 || _tabsRef$current.scroll(id);
      }
    }, {
      root: scrollDom ? document.querySelector(scrollDom) : null,
      rootMargin: "".concat(marginTop, " 0px ").concat(marginBottom)
      // threshold: 0.5,
      // rootMargin: ,
    });
    if (list !== null && list !== void 0 && list.length) {
      var _iterator = _createForOfIteratorHelper(list),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var _item2 = _step.value;
          var _item = document.querySelector("#list-".concat(_item2[rowKey]));
          if (_item) {
            io.observe(_item);
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
  };
  var handleUnObserver = function handleUnObserver() {
    if (list !== null && list !== void 0 && list.length && io) {
      var _iterator2 = _createForOfIteratorHelper(list),
        _step2;
      try {
        for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
          var _item3 = _step2.value;
          var _item = document.querySelector("#list-".concat(_item3[rowKey]));
          if (_item) {
            io.unobserve(_item);
          }
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('tab-product', warpClassName)
  }, /*#__PURE__*/React.createElement("div", {
    className: 'tabs-wrap',
    style: {
      top: stickyTop
    }
  }, /*#__PURE__*/React.createElement(Card, {
    className: classNames('tabs', tabsWrapClassName),
    id: "".concat(name || 'product', "-category")
  }, /*#__PURE__*/React.createElement(Tabs, {
    ref: tabsRef,
    options: list,
    rowKey: rowKey,
    tab: current,
    name: name,
    onChange: function onChange(e) {
      var _document, _document2;
      var dom = (_document = document) === null || _document === void 0 ? void 0 : _document.querySelector('#list-' + e);
      var main = (_document2 = document) === null || _document2 === void 0 ? void 0 : _document2.querySelector(scrollDom || '');
      if (dom && main) {
        main.scrollTop = dom.offsetTop - 42;
      }
      handleUnObserver();
      setCurrent(e);
      setTimeout(function () {
        handleObserver();
      }, 1000);
    }
  }))), /*#__PURE__*/React.createElement("div", {
    className: classNames('category-list', categoryWrapClassName)
  }, list.map(function (d, i) {
    return /*#__PURE__*/React.createElement("div", {
      className: 'category-item',
      id: "list-".concat(d[rowKey]),
      key: i
    }, /*#__PURE__*/React.createElement("div", {
      className: 'category-title'
    }, d.name), /*#__PURE__*/React.createElement("div", {
      className: classNames('product-list', listWrapClassName)
    }, d.products.map(function (item, index) {
      if (renderItem) {
        return renderItem(item);
      }
      return /*#__PURE__*/React.createElement("div", {
        key: index
      }, index);
    })));
  })));
};
export default TabProduct;