"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const DropdownMenu = require("../dropdown-menu/DropdownMenu.js");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useExpose = require("../composables/use-expose.js");
const index$1 = require("../cell/index.js");
const index$2 = require("../icon/index.js");
const index = require("../popup/index.js");
const dom = require("../utils/dom.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const format = require("../utils/format.js");
const [name, bem] = create.createNamespace("dropdown-item");
const dropdownItemProps = {
  title: String,
  options: props.makeArrayProp(),
  disabled: Boolean,
  teleport: [String, Object],
  lazyRender: props.truthProp,
  modelValue: props.unknownProp,
  titleClass: props.unknownProp
};
var stdin_default = Vue.defineComponent({
  name,
  inheritAttrs: false,
  props: dropdownItemProps,
  emits: ["open", "opened", "close", "closed", "change", "update:modelValue"],
  setup(props2, {
    emit,
    slots,
    attrs
  }) {
    const state = Vue.reactive({
      showPopup: false,
      transition: true,
      showWrapper: false
    });
    const wrapperRef = Vue.ref();
    const {
      parent,
      index: index$3
    } = index_esm.useParent(DropdownMenu.DROPDOWN_KEY);
    if (!parent) {
      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 (props2.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 (props2.title) {
        return props2.title;
      }
      const match = props2.options.find((option) => option.value === props2.modelValue);
      return match ? match.text : "";
    };
    const renderOption = (option) => {
      const {
        activeColor
      } = parent.props;
      const {
        disabled
      } = option;
      const active = option.value === props2.modelValue;
      const onClick = () => {
        if (disabled) {
          return;
        }
        state.showPopup = false;
        if (option.value !== props2.modelValue) {
          emit("update:modelValue", option.value);
          emit("change", option.value);
        }
      };
      const renderIcon = () => {
        if (active) {
          return Vue.createVNode(index$2.Icon, {
            "class": bem("icon"),
            "color": disabled ? void 0 : activeColor,
            "name": "success"
          }, null);
        }
      };
      return Vue.createVNode(index$1.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 = format.getZIndexStyle(zIndex);
      let offsetValue = offset.value;
      if (autoLocate && wrapperRef.value) {
        const offsetParent = dom.getContainingBlock(wrapperRef.value);
        if (offsetParent) {
          offsetValue -= index_esm.useRect(offsetParent).top;
        }
      }
      if (direction === "down") {
        style.top = `${offsetValue}px`;
      } else {
        style.bottom = `${offsetValue}px`;
      }
      return Vue.withDirectives(Vue.createVNode("div", Vue.mergeProps({
        "ref": wrapperRef,
        "style": style,
        "class": bem([direction]),
        "onClick": onClickWrapper
      }, attrs), [Vue.createVNode(index.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": props2.lazyRender,
        "overlayStyle": {
          position: "absolute"
        },
        "aria-labelledby": `${parent.id}-${index$3.value}`,
        "data-allow-mismatch": "attribute",
        "closeOnClickOverlay": closeOnClickOverlay,
        "onOpen": onOpen,
        "onClose": onClose,
        "onOpened": onOpened,
        "onClosed": onClosed
      }, {
        default: () => {
          var _a;
          return [props2.options.map(renderOption), (_a = slots.default) == null ? void 0 : _a.call(slots)];
        }
      })]), [[Vue.vShow, state.showWrapper]]);
    };
    useExpose.useExpose({
      state,
      toggle,
      renderTitle
    });
    return () => {
      if (props2.teleport) {
        return Vue.createVNode(Vue.Teleport, {
          "to": props2.teleport
        }, {
          default: () => [renderContent()]
        });
      }
      return renderContent();
    };
  }
});
exports.default = stdin_default;
exports.dropdownItemProps = dropdownItemProps;
