import { useContext, useState } from "react";
import cs from "../../_util/classNames.js";
import IconRight from "../../../icon/react-icon/IconRight/index.js";
import IconLeft from "../../../icon/react-icon/IconLeft/index.js";
import IconDown from "../../../icon/react-icon/IconDown/index.js";
import { isChildrenSelected } from "../util.js";
import omit from "../../_util/omit.js";
import Dropdown from "../../Dropdown/index.js";
import Menu from "../index.js";
import MenuIndent from "../indent.js";
import MenuContext from "../context.js";
import { ConfigContext } from "../../ConfigProvider/index.js";
import { Enter, ArrowLeft, ArrowRight } from "../../_util/keycode.js";
import useId from "../../_util/hooks/useId.js";
import { jsx, jsxs } 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 SubMenuPop = function(props) {
  var _a;
  var _key = props._key, children = props.children, style = props.style, className = props.className, title = props.title, level = props.level, selectable = props.selectable, forwardedRef = props.forwardedRef, propTriggerProps = props.triggerProps;
  var _b = useContext(MenuContext), menuId = _b.id, prefixCls = _b.prefixCls, mode = _b.mode, inDropdown = _b.inDropdown, levelIndent = _b.levelIndent, _c = _b.selectedKeys, selectedKeys = _c === void 0 ? [] : _c, icons = _b.icons, contextTriggerProps = _b.triggerProps, onClickSubMenu = _b.onClickSubMenu, onClickMenuItem = _b.onClickMenuItem;
  var rtl = useContext(ConfigContext).rtl;
  var triggerProps = __assign(__assign({}, contextTriggerProps), propTriggerProps);
  var _d = __read(useState(false), 2), popupVisible = _d[0], setPopupVisible = _d[1];
  var baseClassName = prefixCls + "-pop";
  var isSelected = selectable && selectedKeys.indexOf(props._key) > -1;
  var needPopOnBottom = mode === "horizontal" && !inDropdown;
  var instanceId = useId(menuId + "-submenu-pop-");
  var renderSuffix = function() {
    var MergedIconRight = icons && icons.popArrowRight ? icons.popArrowRight : rtl ? /* @__PURE__ */ jsx(IconLeft, {}) : /* @__PURE__ */ jsx(IconRight, {});
    var MergedIconDown = icons && icons.horizontalArrowDown ? icons.horizontalArrowDown : /* @__PURE__ */ jsx(IconDown, {});
    return /* @__PURE__ */ jsx("span", {
      className: prefixCls + "-icon-suffix",
      children: needPopOnBottom ? MergedIconDown : MergedIconRight
    });
  };
  var hasSelectedStatus = isChildrenSelected(children, selectedKeys) || isSelected;
  var popPosition = rtl ? ["br", "lt"] : ["bl", "rt"];
  var subMenuClickHandler = function(event) {
    onClickSubMenu(_key, level, "pop");
    selectable && onClickMenuItem(_key, event);
  };
  return /* @__PURE__ */ jsx(Dropdown, {
    trigger: "hover",
    popupVisible,
    onVisibleChange: setPopupVisible,
    droplist: /* @__PURE__ */ jsx(Menu, {
      id: instanceId,
      selectedKeys,
      onClickMenuItem: function(key, event) {
        onClickMenuItem(key, event);
        setPopupVisible(false);
      },
      children
    }),
    triggerProps: __assign({
      position: needPopOnBottom ? popPosition[0] : popPosition[1],
      showArrow: true,
      autoAlignPopupMinWidth: true,
      classNames: "fadeIn",
      duration: 100,
      mouseEnterDelay: 50,
      mouseLeaveDelay: 50,
      className: cs(baseClassName + "-trigger", triggerProps && triggerProps.className)
    }, omit(triggerProps, ["className"])),
    children: /* @__PURE__ */ jsxs("div", {
      tabIndex: 0,
      "aria-haspopup": true,
      "aria-expanded": popupVisible,
      "aria-controls": instanceId,
      ref: forwardedRef,
      style,
      className: cs(baseClassName, baseClassName + "-header", (_a = {}, _a[prefixCls + "-selected"] = hasSelectedStatus, _a), className),
      onClick: subMenuClickHandler,
      onKeyDown: function(event) {
        var keyCode = event.keyCode || event.which;
        if (keyCode === Enter.code) {
          subMenuClickHandler(event);
        } else if (keyCode === ArrowLeft.code) {
          setPopupVisible(false);
        } else if (keyCode === ArrowRight.code) {
          setPopupVisible(true);
        }
      },
      children: [/* @__PURE__ */ jsx(MenuIndent, {
        prefixCls,
        levelIndent,
        level
      }), title, renderSuffix(), hasSelectedStatus && mode === "horizontal" ? /* @__PURE__ */ jsx("div", {
        className: prefixCls + "-selected-label"
      }) : null]
    })
  });
};
const SubMenuPop$1 = SubMenuPop;
export {
  SubMenuPop$1 as default
};
//# sourceMappingURL=pop.js.map
