var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
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 __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
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, {
  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_utils = require("../utils");
var import_renderToContainer = require("../utils/dom/renderToContainer");
var import_hooks = require("../hooks");
var import_use_merged_state = __toESM(require("../hooks/use-merged-state"));
var import_icon = require("../icon");
var import_cell = require("../cell");
var import_popup = __toESM(require("../popup"));
var import_DropdownMenu = require("./DropdownMenu");
const DropdownItem = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("dropdown-item", prefixCls);
  const parent = (0, import_react.useContext)(import_DropdownMenu.DropdownMenuContext);
  const [selectedValue, setSelectedValue] = (0, import_use_merged_state.default)({
    defaultValue: props.defaultValue,
    value: props.value
  });
  const currentValueRef = (0, import_react.useRef)(selectedValue);
  const [state, setState] = (0, import_hooks.useSetState)({
    showPopup: false,
    transition: true,
    showWrapper: false
  });
  const onOpen = () => {
    var _a;
    (_a = props.onOpen) == null ? void 0 : _a.call(props);
  };
  const onOpened = () => {
    var _a;
    (_a = props.onOpened) == null ? void 0 : _a.call(props);
  };
  const onClose = () => {
    var _a;
    setState({
      showPopup: false
    });
    parent.close();
    (_a = props.onClose) == null ? void 0 : _a.call(props);
  };
  const onClosed = () => {
    var _a;
    setState({
      showWrapper: false
    });
    (_a = props.onClosed) == null ? void 0 : _a.call(props);
  };
  const onClickWrapper = (event) => {
    if (props.teleport) {
      event.stopPropagation();
    }
  };
  const toggle = (show = !state.showPopup, options = {}) => {
    if (show === state.showPopup) {
      return;
    }
    const newState = {};
    newState.showPopup = show;
    newState.transition = !options.immediate;
    if (show) {
      state.showWrapper = true;
    }
    setState(newState);
  };
  const renderTitle = () => {
    if (props.title) {
      return props.title;
    }
    const match = props.options.find((option) => {
      var _a;
      return option.value === ((_a = currentValueRef.current) == null ? void 0 : _a.value);
    });
    return match ? match.text : props.placeholder;
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    toggle,
    renderTitle,
    state,
    name: props.name,
    titleClass: props.titleClass,
    disabled: props.disabled
  }));
  const renderOption = (option) => {
    var _a;
    const {
      activeColor
    } = parent.props;
    const active = option.value === ((_a = currentValueRef.current) == null ? void 0 : _a.value);
    const onClick = () => {
      var _a2, _b, _c;
      if (option.value !== ((_a2 = currentValueRef.current) == null ? void 0 : _a2.value)) {
        const newValue = __spreadProps(__spreadValues({}, selectedValue), {
          text: option.text,
          value: option.value
        });
        setSelectedValue(newValue);
        currentValueRef.current = newValue;
        (_b = props.onChange) == null ? void 0 : _b.call(props, newValue);
        (_c = parent.onChange) == null ? void 0 : _c.call(parent, newValue);
        onClose();
      }
    };
    const renderIcon = () => {
      if (active) {
        return /* @__PURE__ */ import_react.default.createElement(import_icon.Icon, {
          className: (0, import_classnames.default)(bem("icon")),
          color: activeColor,
          name: "success"
        });
      }
      return null;
    };
    return /* @__PURE__ */ import_react.default.createElement(import_cell.Cell, {
      key: option.value,
      icon: option.icon,
      title: option.text,
      className: (0, import_classnames.default)(bem("option", {
        active
      })),
      style: {
        color: active ? activeColor : ""
      },
      clickable: true,
      onClick
    }, renderIcon());
  };
  const renderContent = () => {
    var _a;
    const {
      offset
    } = parent;
    const {
      zIndex,
      overlay,
      duration,
      direction,
      closeOnClickOverlay
    } = parent.props;
    const style = (0, import_utils.getZIndexStyle)(zIndex);
    if (direction === "down") {
      style.top = `${offset}px`;
    } else {
      style.bottom = `${offset}px`;
    }
    return /* @__PURE__ */ import_react.default.createElement("div", {
      style: __spreadProps(__spreadValues({}, style), {
        display: state.showWrapper ? "block" : "none"
      }),
      className: (0, import_classnames.default)(bem([direction])),
      onClick: onClickWrapper
    }, /* @__PURE__ */ import_react.default.createElement(import_popup.default, {
      visible: state.showPopup,
      className: (0, import_classnames.default)(bem("content")),
      overlay,
      position: direction === "down" ? "top" : "bottom",
      duration: state.transition ? +duration : 0,
      overlayStyle: {
        position: "absolute"
      },
      closeOnClickOverlay,
      teleport: null,
      onOpen,
      onClose,
      onOpened,
      onClosed
    }, (_a = props.options) == null ? void 0 : _a.map(renderOption), props.children));
  };
  return props.teleport ? (0, import_renderToContainer.renderToContainer)(props.teleport, renderContent()) : renderContent();
});
DropdownItem.defaultProps = {
  options: [],
  disabled: false,
  placeholder: "\u8BF7\u9009\u62E9"
};
DropdownItem.displayName = "DropdownItem";
var stdin_default = DropdownItem;
