var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  DropdownMenuContext: () => DropdownMenuContext,
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_constant = require("../utils/constant");
var import_utils = require("../utils");
var import_use_scroll_parent = __toESM(require("../hooks/use-scroll-parent"));
var import_use_click_away = __toESM(require("../hooks/use-click-away"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_use_refs = __toESM(require("../hooks/use-refs"));
var import_get_rect = __toESM(require("../hooks/get-rect"));
const DropdownMenuContext = /* @__PURE__ */ (0, import_react.createContext)({});
const DropdownMenu = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("dropdown-menu", prefixCls);
  const [titleFlag, setTitleFlag] = (0, import_react.useState)(props.shouldRenderTitle);
  const [showPopupIndex, setShowPopupIndex] = (0, import_react.useState)(null);
  const showPopupIndexRef = (0, import_react.useRef)(null);
  const rect = (0, import_react.useRef)({
    bottom: 0,
    top: 0
  });
  const root = (0, import_react.useRef)();
  const barRef = (0, import_react.useRef)();
  const offsetRef = (0, import_react.useRef)(0);
  const [childrenRefs, setChildrenRefs] = (0, import_use_refs.default)();
  const scrollParent = (0, import_use_scroll_parent.default)(root);
  const opened = (0, import_react.useMemo)(() => {
    return showPopupIndex !== null;
  }, [showPopupIndex]);
  const barStyle = () => {
    if (opened && (0, import_utils.isDef)(props.zIndex)) {
      return {
        zIndex: +props.zIndex + 1
      };
    }
    return {};
  };
  const updateShowPopupIndex = (current) => {
    showPopupIndexRef.current = current;
    setShowPopupIndex(current);
  };
  const updateOffset = () => {
    if (barRef.current) {
      rect.current = (0, import_get_rect.default)(barRef.current);
      if (props.direction === "down") {
        offsetRef.current = rect.current.bottom;
      } else {
        offsetRef.current = window.innerHeight - rect.current.top;
      }
    }
  };
  const closeAll = () => {
    childrenRefs.forEach((item) => {
      item.toggle(false);
    });
    updateShowPopupIndex(null);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    close: closeAll
  }));
  const onClickAway = () => {
    if (props.closeOnClickOutside) {
      closeAll();
    }
  };
  (0, import_react.useEffect)(() => {
    if (barRef.current) {
      updateOffset();
    }
  }, [barRef.current]);
  const onScroll = () => {
    if (opened) {
      updateOffset();
    }
  };
  const showItem = (index) => {
    const item = childrenRefs[index];
    updateOffset();
    updateShowPopupIndex(index);
    item.toggle(true);
  };
  const onToggleItem = (active) => {
    childrenRefs.forEach((child, index) => {
      if (index === active) {
        if (active === showPopupIndexRef.current) {
          updateShowPopupIndex(null);
          child.toggle();
          return;
        }
        showItem(active);
      } else {
        child.toggle(false, {
          immediate: false
        });
      }
    });
  };
  const renderTitle = (item, index) => {
    const showPopup = showPopupIndex === index;
    const {
      disabled,
      titleClass
    } = item;
    return /* @__PURE__ */ import_react.default.createElement("div", {
      key: index,
      role: "button",
      tabIndex: disabled ? -1 : 0,
      className: (0, import_classnames.default)(bem("item", {
        disabled
      }), {
        [import_constant.HAPTICS_FEEDBACK]: !disabled
      }),
      onClick: () => {
        if (!disabled) {
          onToggleItem(index);
        }
      }
    }, /* @__PURE__ */ import_react.default.createElement("span", {
      className: (0, import_classnames.default)(bem("title", {
        down: showPopup === (props.direction === "down"),
        active: showPopup
      }), titleClass),
      style: {
        color: showPopup ? props.activeColor : ""
      }
    }, /* @__PURE__ */ import_react.default.createElement("div", {
      className: "rc-ellipsis"
    }, item.renderTitle())));
  };
  (0, import_use_click_away.default)(root, onClickAway);
  (0, import_use_event_listener.default)("scroll", onScroll, {
    target: scrollParent
  });
  const handleChange = (value) => {
    var _a;
    if (titleFlag) {
      setTitleFlag(false);
    } else {
      setTitleFlag(true);
    }
    updateShowPopupIndex(null);
    (_a = props.onChange) == null ? void 0 : _a.call(props, value);
  };
  return /* @__PURE__ */ import_react.default.createElement(DropdownMenuContext.Provider, {
    value: {
      props,
      offset: offsetRef.current,
      onChange: handleChange,
      close: closeAll
    }
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    className: (0, import_classnames.default)(bem(), props.className),
    style: __spreadValues({}, props.style)
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: barRef,
    style: barStyle(),
    className: (0, import_classnames.default)(bem("bar", {
      opened
    }))
  }, childrenRefs.map(renderTitle)), import_react.default.Children.toArray(props.children).filter(Boolean).map((child, index) => /* @__PURE__ */ import_react.default.cloneElement(child, {
    ref: setChildrenRefs(index)
  }))));
});
DropdownMenu.defaultProps = {
  duration: 200,
  overlay: true,
  direction: "down",
  zIndex: 2e3,
  closeOnClickOutside: true,
  closeOnClickOverlay: true,
  shouldRenderTitle: false
};
DropdownMenu.displayName = "DropdownMenu";
var stdin_default = DropdownMenu;
