var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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));
import React, { forwardRef, useContext, useImperativeHandle, useRef } from "react";
import classnames from "classnames";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import { getZIndexStyle } from "../utils";
import { renderToContainer } from "../utils/dom/renderToContainer";
import { useSetState } from "../hooks";
import useMergedState from "../hooks/use-merged-state";
import { Icon } from "../icon";
import { Cell } from "../cell";
import Popup from "../popup";
import { DropdownMenuContext } from "./DropdownMenu";
const DropdownItem = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("dropdown-item", prefixCls);
  const parent = useContext(DropdownMenuContext);
  const [selectedValue, setSelectedValue] = useMergedState({
    defaultValue: props.defaultValue,
    value: props.value
  });
  const currentValueRef = useRef(selectedValue);
  const [state, setState] = 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;
  };
  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__ */ React.createElement(Icon, {
          className: classnames(bem("icon")),
          color: activeColor,
          name: "success"
        });
      }
      return null;
    };
    return /* @__PURE__ */ React.createElement(Cell, {
      key: option.value,
      icon: option.icon,
      title: option.text,
      className: classnames(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 = getZIndexStyle(zIndex);
    if (direction === "down") {
      style.top = `${offset}px`;
    } else {
      style.bottom = `${offset}px`;
    }
    return /* @__PURE__ */ React.createElement("div", {
      style: __spreadProps(__spreadValues({}, style), {
        display: state.showWrapper ? "block" : "none"
      }),
      className: classnames(bem([direction])),
      onClick: onClickWrapper
    }, /* @__PURE__ */ React.createElement(Popup, {
      visible: state.showPopup,
      className: classnames(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 ? renderToContainer(props.teleport, renderContent()) : renderContent();
});
DropdownItem.defaultProps = {
  options: [],
  disabled: false,
  placeholder: "\u8BF7\u9009\u62E9"
};
DropdownItem.displayName = "DropdownItem";
var stdin_default = DropdownItem;
export {
  stdin_default as default
};
