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, useMemo } from "react";
import Tabs from "./tabs";
import Card from "./card";
import ProductItem from "./productItem";
import "./index.less";
import { useMemoizedFn } from "ahooks";
var io = null;
var TabProduct = function TabProduct(props) {
  var list = props.list,
    onChange = props.onChange,
    _props$amountSymbol = props.amountSymbol,
    amountSymbol = _props$amountSymbol === void 0 ? "$" : _props$amountSymbol,
    scrollDom = props.scrollDom;
  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].id);
      handleObserver();
    }
  }, [list]);

  /**
   * @title: 开启监听
   * @description:
   * @return {*}
   * @Author: chensheng.gu
   * @Date: 2023-07-13 16:31
   */
  var handleObserver = useMemoizedFn(function () {
    var _marginTop = 270;
    var _marginBottom = window.innerHeight - 70 - 200 - 70;
    var _rootMargin = "".concat(_marginTop * -1, "px 0px ").concat(_marginBottom * -1, "px 0px");
    io = new IntersectionObserver(function (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: null,
      rootMargin: _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.id));
          if (_item) {
            io.observe(_item);
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
  });

  /**
   * @title: 取消监听
   * @description:
   * @return {*}
   * @Author: chensheng.gu
   * @Date: 2023-07-13 16:31
   */
  var handleUnobserver = useMemoizedFn(function () {
    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.id));
          if (_item) {
            io.unobserve(_item);
          }
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  });
  var onProductClick = useMemoizedFn(function (productDetail, type) {
    onChange === null || onChange === void 0 || onChange(productDetail, type);
  });
  return useMemo(function () {
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "booking-tab-product"
    }, /*#__PURE__*/React.createElement("div", {
      className: "booking-tabs-wrap",
      style: {
        top: "67px"
      }
    }, /*#__PURE__*/React.createElement(Card, {
      className: "booking-tabs",
      id: "product-category"
    }, /*#__PURE__*/React.createElement(Tabs, {
      ref: tabsRef,
      options: list,
      rowKey: "id",
      tab: current,
      onChange: function onChange(e) {
        var _document, _document2;
        var categoryDom = document.querySelector("#product-category");
        var top = 0;
        if (categoryDom) {
          top = categoryDom.offsetHeight + 16;
        }
        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 - top;
        }
        handleUnobserver();
        setCurrent(e);
        setTimeout(function () {
          handleObserver();
        }, 1000);
      }
    }))), /*#__PURE__*/React.createElement("div", {
      className: "booking-category-list"
    }, list.map(function (d, i) {
      return /*#__PURE__*/React.createElement("div", {
        className: "booking-category-item",
        id: "list-".concat(d.id),
        key: i
      }, /*#__PURE__*/React.createElement("div", {
        className: "booking-category-title"
      }, d.name), /*#__PURE__*/React.createElement("div", {
        className: "booking-product-list"
      }, /*#__PURE__*/React.createElement(Card, {
        className: "booking-card",
        layout: 2
      }, d.products.map(function (item) {
        return /*#__PURE__*/React.createElement(ProductItem, {
          key: item.id,
          item: item,
          onClick: onProductClick,
          amountSymbol: amountSymbol
        });
      }))));
    }))));
  }, [list, current, amountSymbol]);
};
export default TabProduct;