import React__default, { useContext, useRef } from "react";
import Trigger, { EventsByTriggerNeed } from "../Trigger/index.js";
import Button from "./button.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import cs from "../_util/classNames.js";
import useMergeValue from "../_util/hooks/useMergeValue.js";
import omit from "../_util/omit.js";
import pick, { pickDataAttributes } from "../_util/pick.js";
import useMergeProps from "../_util/hooks/useMergeProps.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 __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
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 __values = globalThis && globalThis.__values || function(o) {
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  if (m)
    return m.call(o);
  if (o && typeof o.length === "number")
    return {
      next: function() {
        if (o && i >= o.length)
          o = void 0;
        return {
          value: o && o[i++],
          done: !o
        };
      }
    };
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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 defaultProps = {
  position: "bl",
  trigger: "hover",
  unmountOnExit: true
};
function Dropdown(baseProps, _) {
  var _a;
  var _b = useContext(ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Button);
  var trigger = props.trigger, droplist = props.droplist, children = props.children, position = props.position, disabled = props.disabled, unmountOnExit = props.unmountOnExit, triggerProps = props.triggerProps, getPopupContainer = props.getPopupContainer, onVisibleChange = props.onVisibleChange, rest = __rest(props, ["trigger", "droplist", "children", "position", "disabled", "unmountOnExit", "triggerProps", "getPopupContainer", "onVisibleChange"]);
  var prefixCls = getPrefixCls("dropdown");
  var triggerRef = useRef(null);
  var _c = __read(useMergeValue(false, {
    defaultValue: props.defaultPopupVisible,
    value: props.popupVisible
  }), 2), popupVisible = _c[0], setPopupVisible = _c[1];
  var getPopupContent = function() {
    return React__default.Children.only(droplist || /* @__PURE__ */ jsx("span", {}));
  };
  var changePopupVisible = function(visible) {
    setPopupVisible(visible);
    onVisibleChange && onVisibleChange(visible);
    triggerProps && triggerProps.onVisibleChange && triggerProps.onVisibleChange(visible);
  };
  var handleVisibleChange = function(visible) {
    if (visible !== popupVisible) {
      changePopupVisible(visible);
    }
  };
  var renderPopup = function() {
    var e_1, _a2, _b2;
    var _c2;
    var content = getPopupContent();
    if ((_c2 = content === null || content === void 0 ? void 0 : content.props) === null || _c2 === void 0 ? void 0 : _c2.isMenu) {
      var isEmpty = true;
      try {
        for (var _d = __values(React__default.Children.toArray(content.props.children)), _e = _d.next(); !_e.done; _e = _d.next()) {
          var child = _e.value;
          if (child !== null && child !== void 0) {
            isEmpty = false;
            break;
          }
        }
      } catch (e_1_1) {
        e_1 = {
          error: e_1_1
        };
      } finally {
        try {
          if (_e && !_e.done && (_a2 = _d.return))
            _a2.call(_d);
        } finally {
          if (e_1)
            throw e_1.error;
        }
      }
      return React__default.cloneElement(content, {
        prefixCls: cs(prefixCls + "-menu", (_b2 = {}, _b2[prefixCls + "-menu-hidden"] = isEmpty, _b2)),
        inDropdown: true,
        selectable: false,
        onClickMenuItem: function() {
          var _a3;
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
          }
          var returnValueOfOnClickMenuItem = null;
          var content2 = getPopupContent();
          if (content2.props.onClickMenuItem) {
            returnValueOfOnClickMenuItem = (_a3 = content2.props).onClickMenuItem.apply(_a3, __spreadArray([], __read(args), false));
          }
          var child2 = triggerRef.current && triggerRef.current.getRootElement();
          child2 && child2.focus && child2.focus();
          if (returnValueOfOnClickMenuItem instanceof Promise) {
            returnValueOfOnClickMenuItem.finally(function() {
              return changePopupVisible(false);
            });
          } else if (returnValueOfOnClickMenuItem !== false) {
            changePopupVisible(false);
          }
        }
      });
    }
    return content;
  };
  return /* @__PURE__ */ jsx(Trigger, {
    ...__assign({
      ref: function(ref) {
        return triggerRef.current = ref;
      },
      classNames: "slideDynamicOrigin",
      childrenPrefix: prefixCls,
      trigger,
      popup: renderPopup,
      mouseEnterDelay: 400,
      mouseLeaveDelay: 400,
      disabled,
      unmountOnExit,
      position,
      popupVisible,
      popupAlign: {
        left: 4,
        right: 4,
        top: 4,
        bottom: 4
      },
      getPopupContainer,
      alignPoint: trigger === "contextMenu"
    }, pick(rest, EventsByTriggerNeed), pickDataAttributes(rest), omit(triggerProps, ["onVisibleChange"]), {
      onVisibleChange: handleVisibleChange
    }),
    children: React__default.isValidElement(children) ? React__default.cloneElement(children, __assign(__assign({}, typeof disabled === "boolean" ? {
      disabled
    } : {}), {
      className: cs((_a = {}, _a[prefixCls + "-popup-visible"] = popupVisible, _a[[prefixCls] + "-rtl"] = rtl, _a), children.props.className)
    })) : children
  });
}
var ForwardRefDropdown = React__default.forwardRef(Dropdown);
var DropdownComponent = ForwardRefDropdown;
DropdownComponent.displayName = "Dropdown";
DropdownComponent.Button = Button;
const Dropdown$1 = DropdownComponent;
export {
  Dropdown$1 as default
};
//# sourceMappingURL=index.js.map
