var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name2 in all)
    __defProp(target, name2, { get: all[name2], 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default,
  dropdownItemProps: () => dropdownItemProps
});
module.exports = __toCommonJS(stdin_exports);
var import_vue = require("vue");
var import_utils = require("../utils");
var import_DropdownMenu = require("../dropdown-menu/DropdownMenu");
var import_use = require("@vant/use");
var import_use_expose = require("../composables/use-expose");
var import_cell = require("../cell");
var import_icon = require("../icon");
var import_popup = require("../popup");
const [name, bem] = (0, import_utils.createNamespace)("dropdown-item");
const dropdownItemProps = {
  title: String,
  options: (0, import_utils.makeArrayProp)(),
  disabled: Boolean,
  teleport: [String, Object],
  lazyRender: import_utils.truthProp,
  modelValue: import_utils.unknownProp,
  titleClass: import_utils.unknownProp
};
var stdin_default = (0, import_vue.defineComponent)({
  name,
  inheritAttrs: false,
  props: dropdownItemProps,
  emits: ["open", "opened", "close", "closed", "change", "update:modelValue"],
  setup(props, {
    emit,
    slots,
    attrs
  }) {
    const state = (0, import_vue.reactive)({
      showPopup: false,
      transition: true,
      showWrapper: false
    });
    const wrapperRef = (0, import_vue.ref)();
    const {
      parent,
      index
    } = (0, import_use.useParent)(import_DropdownMenu.DROPDOWN_KEY);
    if (!parent) {
      if (process.env.NODE_ENV !== "production") {
        console.error("[Vant] <DropdownItem> must be a child component of <DropdownMenu>.");
      }
      return;
    }
    const getEmitter = (name2) => () => emit(name2);
    const onOpen = getEmitter("open");
    const onClose = getEmitter("close");
    const onOpened = getEmitter("opened");
    const onClosed = () => {
      state.showWrapper = false;
      emit("closed");
    };
    const onClickWrapper = (event) => {
      if (props.teleport) {
        event.stopPropagation();
      }
    };
    const toggle = (show = !state.showPopup, options = {}) => {
      if (show === state.showPopup) {
        return;
      }
      state.showPopup = show;
      state.transition = !options.immediate;
      if (show) {
        parent.updateOffset();
        state.showWrapper = true;
      }
    };
    const renderTitle = () => {
      if (slots.title) {
        return slots.title();
      }
      if (props.title) {
        return props.title;
      }
      const match = props.options.find((option) => option.value === props.modelValue);
      return match ? match.text : "";
    };
    const renderOption = (option) => {
      const {
        activeColor
      } = parent.props;
      const {
        disabled
      } = option;
      const active = option.value === props.modelValue;
      const onClick = () => {
        if (disabled) {
          return;
        }
        state.showPopup = false;
        if (option.value !== props.modelValue) {
          emit("update:modelValue", option.value);
          emit("change", option.value);
        }
      };
      const renderIcon = () => {
        if (active) {
          return (0, import_vue.createVNode)(import_icon.Icon, {
            "class": bem("icon"),
            "color": disabled ? void 0 : activeColor,
            "name": "success"
          }, null);
        }
      };
      return (0, import_vue.createVNode)(import_cell.Cell, {
        "role": "menuitem",
        "key": String(option.value),
        "icon": option.icon,
        "title": option.text,
        "class": bem("option", {
          active,
          disabled
        }),
        "style": {
          color: active ? activeColor : ""
        },
        "tabindex": active ? 0 : -1,
        "clickable": !disabled,
        "onClick": onClick
      }, {
        value: renderIcon
      });
    };
    const renderContent = () => {
      const {
        offset
      } = parent;
      const {
        autoLocate,
        zIndex,
        overlay,
        duration,
        direction,
        closeOnClickOverlay
      } = parent.props;
      const style = (0, import_utils.getZIndexStyle)(zIndex);
      let offsetValue = offset.value;
      if (autoLocate && wrapperRef.value) {
        const offsetParent = (0, import_utils.getContainingBlock)(wrapperRef.value);
        if (offsetParent) {
          offsetValue -= (0, import_use.useRect)(offsetParent).top;
        }
      }
      if (direction === "down") {
        style.top = `${offsetValue}px`;
      } else {
        style.bottom = `${offsetValue}px`;
      }
      return (0, import_vue.withDirectives)((0, import_vue.createVNode)("div", (0, import_vue.mergeProps)({
        "ref": wrapperRef,
        "style": style,
        "class": bem([direction]),
        "onClick": onClickWrapper
      }, attrs), [(0, import_vue.createVNode)(import_popup.Popup, {
        "show": state.showPopup,
        "onUpdate:show": ($event) => state.showPopup = $event,
        "role": "menu",
        "class": bem("content"),
        "overlay": overlay,
        "overlayProps": {
          duration: state.transition && !parent.opened.value ? duration : 0
        },
        "position": direction === "down" ? "top" : "bottom",
        "duration": state.transition ? duration : 0,
        "lazyRender": props.lazyRender,
        "overlayStyle": {
          position: "absolute"
        },
        "aria-labelledby": `${parent.id}-${index.value}`,
        "data-allow-mismatch": "attribute",
        "closeOnClickOverlay": closeOnClickOverlay,
        "onOpen": onOpen,
        "onClose": onClose,
        "onOpened": onOpened,
        "onClosed": onClosed
      }, {
        default: () => {
          var _a;
          return [props.options.map(renderOption), (_a = slots.default) == null ? void 0 : _a.call(slots)];
        }
      })]), [[import_vue.vShow, state.showWrapper]]);
    };
    (0, import_use_expose.useExpose)({
      state,
      toggle,
      renderTitle
    });
    return () => {
      if (props.teleport) {
        return (0, import_vue.createVNode)(import_vue.Teleport, {
          "to": props.teleport
        }, {
          default: () => [renderContent()]
        });
      }
      return renderContent();
    };
  }
});
