import React__default, { useContext, useRef, useState } from "react";
import SubMenu from "./sub-menu/index.js";
import { getStyle } from "../_util/style.js";
import MenuContext from "./context.js";
import ResizeObserver from "../_util/resizeObserver.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var OVERFLOW_THRESHOLD = 10;
function getNodeWidth(node) {
  return node && Math.ceil(+node.getBoundingClientRect().width);
}
function translatePxToNumber(str) {
  var result = Number(str.replace("px", ""));
  return isNaN(result) ? 0 : result;
}
var OverflowWrap = function(props) {
  var children = props.children;
  var prefixCls = useContext(MenuContext).prefixCls;
  var refUl = useRef(null);
  var _a = __read(useState(null), 2), lastVisibleIndex = _a[0], setLastVisibleIndex = _a[1];
  var overflowSubMenuClass = prefixCls + "-overflow-sub-menu";
  var overflowMenuItemClass = prefixCls + "-overflow-hidden-menu-item";
  var overflowSubMenuMirrorClass = prefixCls + "-overflow-sub-menu-mirror";
  function computeLastVisibleIndex() {
    if (!refUl.current) {
      return;
    }
    var ulElement = refUl.current;
    var maxWidth = getNodeWidth(ulElement) - OVERFLOW_THRESHOLD;
    var childNodeList = [].slice.call(ulElement.children);
    var menuItemIndex = 0;
    var currentItemRight = 0;
    var overflowSubMenuWidth = 0;
    for (var i = 0; i < childNodeList.length; i++) {
      var node = childNodeList[i];
      var classNames = node.className.split(" ");
      var isOverflowSubMenu = classNames.indexOf(overflowSubMenuClass) > -1;
      var isOverflowSubMenuMirror = classNames.indexOf(overflowSubMenuMirrorClass) > -1;
      if (isOverflowSubMenu) {
        continue;
      }
      var nodeWidth = getNodeWidth(node) + translatePxToNumber(getStyle(node, "marginLeft")) + translatePxToNumber(getStyle(node, "marginRight"));
      if (isOverflowSubMenuMirror) {
        overflowSubMenuWidth = nodeWidth;
        continue;
      }
      currentItemRight += nodeWidth;
      if (currentItemRight > maxWidth) {
        setLastVisibleIndex(
          menuItemIndex - (currentItemRight - nodeWidth + overflowSubMenuWidth <= maxWidth ? 1 : 2)
        );
        return;
      }
      menuItemIndex++;
    }
    setLastVisibleIndex(null);
  }
  var renderOverflowSubMenu = function(children2, isMirror) {
    if (isMirror === void 0) {
      isMirror = false;
    }
    return /* @__PURE__ */ jsx(SubMenu, {
      ...__assign({
        title: /* @__PURE__ */ jsx("span", {
          children: "\xB7\xB7\xB7"
        }),
        key: "arco-menu-overflow-sub-menu" + (isMirror ? "-mirror" : ""),
        className: isMirror ? overflowSubMenuMirrorClass : overflowSubMenuClass
      }, props, {
        children: children2
      })
    });
  };
  var renderChildren = function() {
    var overflowSubMenu = null;
    var overflowSubMenuMirror = renderOverflowSubMenu(null, true);
    var originMenuItems = React__default.Children.map(children, function(child, index) {
      var item = child;
      if (lastVisibleIndex !== null) {
        if (index > lastVisibleIndex) {
          item = React__default.cloneElement(child, {
            className: overflowMenuItemClass
          });
        }
        if (index === lastVisibleIndex + 1) {
          var overflowedItems = React__default.Children.toArray(children).slice(lastVisibleIndex + 1).map(function(child2) {
            return React__default.cloneElement(child2, {
              key: child2.props._key
            });
          });
          overflowSubMenu = renderOverflowSubMenu(overflowedItems);
        }
      }
      return item;
    });
    return __spreadArray(__spreadArray([overflowSubMenuMirror], __read(originMenuItems), false), [overflowSubMenu], false);
  };
  return /* @__PURE__ */ jsx(ResizeObserver, {
    onResize: computeLastVisibleIndex,
    children: /* @__PURE__ */ jsx("div", {
      className: prefixCls + "-overflow-wrap",
      ref: refUl,
      children: renderChildren()
    })
  });
};
const OverflowWrap$1 = OverflowWrap;
export {
  OverflowWrap$1 as default
};
//# sourceMappingURL=overflow-wrap.js.map
