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, { useState, useEffect, useRef } from 'react';
import useStyle from "./style";
import classNames from 'classnames';
import _ from 'lodash';
//
// const useDebounce = (value, wait: number) => {
//   const [debounceValue, setDebounceValue] = useState(value)
//   useEffect(() => {
//     const timer = setTimeout(() => setDebounceValue(value), wait)
//     return () => clearTimeout(timer)
//   }, [value, wait])
//   return debounceValue
// }

var Navigation = function Navigation(props) {
  var componentName = 'spec-navigation';
  var _useStyle = useStyle(componentName),
    _useStyle2 = _slicedToArray(_useStyle, 2),
    wrapSSR = _useStyle2[0],
    hashId = _useStyle2[1];
  var items = props.items;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    visibleItems = _useState2[0],
    setVisibleItems = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    hiddenItems = _useState4[0],
    setHiddenItems = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    showMore = _useState6[0],
    setShowMore = _useState6[1];
  var listRef = useRef(null);
  var containerRef = useRef(null);
  var itemsRef = useRef([]);
  var moreButtonRef = useRef(null);
  var _useState7 = useState(0),
    _useState8 = _slicedToArray(_useState7, 2),
    containerWidth = _useState8[0],
    setContainerWidth = _useState8[1];
  var _useState9 = useState(0),
    _useState10 = _slicedToArray(_useState9, 2),
    moreButtonWidth = _useState10[0],
    setMoreButtonWidth = _useState10[1];
  // 使用lodash的debounce函数
  var debouncedHandleChange = _.debounce(function (_ref) {
    var visible = _ref.visible,
      hidden = _ref.hidden;
    // console.log(event.target.value);
    setVisibleItems(visible);
    setHiddenItems(hidden);
  }, 100); // 延迟500毫秒执行

  useEffect(function () {
    var calculateLayout = function calculateLayout() {
      if (!containerRef.current) return;

      // const containerWidth = containerRef.current.offsetWidth;
      // const moreButtonWidth = moreButtonRef.current?.offsetWidth || 0;
      var totalWidth = 0;
      var visible = []; // 显示的
      var hidden = []; // 隐藏的

      itemsRef.current.forEach(function (itemEl, index) {
        if (!itemEl) return;
        var itemWidth = itemEl.offsetWidth;
        var remainingWidth = containerWidth - totalWidth - moreButtonWidth;
        if (remainingWidth >= itemWidth && !hidden.length) {
          totalWidth += itemWidth;
          visible.push(items[index]);
        } else {
          hidden.push(items[index]);
        }
      });
      debouncedHandleChange({
        visible: visible,
        hidden: hidden
      });
      // setVisibleItems(visible);
      // setHiddenItems(hidden);
    };

    // 初始计算
    calculateLayout();

    // 添加窗口resize监听
    var handleResize = function handleResize() {
      calculateLayout();
      setShowMore(false); // 窗口resize时关闭下拉菜单
    };
    window.addEventListener('resize', handleResize);
    return function () {
      return window.removeEventListener('resize', handleResize);
    };
  }, [items, containerWidth, moreButtonWidth]);
  useEffect(function () {
    var container = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current; // ?.offsetWidth;
    setContainerWidth((container === null || container === void 0 ? void 0 : container.clientWidth) || 0);
    var button = moreButtonRef === null || moreButtonRef === void 0 ? void 0 : moreButtonRef.current; // ?.offsetWidth;
    setMoreButtonWidth((button === null || button === void 0 ? void 0 : button.clientWidth) || 0);
  }, [containerRef === null || containerRef === void 0 ? void 0 : containerRef.current, moreButtonRef === null || moreButtonRef === void 0 ? void 0 : moreButtonRef.current, hiddenItems]);
  // useEffect(() => {
  //   const button = moreButtonRef?.current // ?.offsetWidth;
  //   setMoreButtonWidth(button?.clientWidth || 0)
  // }, [moreButtonRef?.current, hiddenItems])
  // console.log(containerWidth, moreButtonWidth)
  return wrapSSR( /*#__PURE__*/React.createElement("div", {
    style: {
      width: '100%'
    }
  }, /*#__PURE__*/React.createElement("div", {
    ref: listRef,
    style: {
      width: '100%',
      height: '20px',
      backgroundColor: '#ff1133'
    }
  }), /*#__PURE__*/React.createElement("div", {
    ref: containerRef,
    className: classNames(componentName, hashId, "spec-navigation-container"),
    style: {
      // display: 'flex',
      width: '100%'
      // // overflow: 'hidden',
      // boxSizing: "border-box",
      // position: 'relative',
      // paddingRight: '80px', // 为更多按钮预留空间
    }
  }, items.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      className: 'spec-navigation-item',
      key: item.id,
      ref: function ref(el) {
        return el && (itemsRef.current[index] = el);
      },
      style: {
        opacity: visibleItems.includes(item) ? '1' : '0'
      }
    }, item.label);
  }), /*#__PURE__*/React.createElement("div", {
    className: 'spec-navigation-item more-item',
    style: {
      position: 'relative',
      flexShrink: 0,
      display: hiddenItems.length > 0 ? 'block' : 'none'
    }
  }, /*#__PURE__*/React.createElement("button", {
    ref: moreButtonRef,
    onClick: function onClick() {
      return setShowMore(!showMore);
    },
    style: {
      marginLeft: '8px'
    }
  }, "\u66F4\u591A"), showMore && /*#__PURE__*/React.createElement("div", {
    style: {
      position: 'absolute',
      top: '100%',
      right: 0,
      backgroundColor: 'white',
      border: '1px solid #ccc',
      boxShadow: '0 2px 4px rgba(0,0,0,0.1)',
      zIndex: 100
    }
  }, hiddenItems.map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      key: item.id,
      style: {
        padding: '8px 16px',
        whiteSpace: 'nowrap'
      }
    }, item.label);
  }))))));
};
export default Navigation;