"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const useId = require("../composables/use-id.js");
const useExpose = require("../composables/use-expose.js");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const dom = require("../utils/dom.js");
const constant = require("../utils/constant.js");
const [name, bem] = create.createNamespace("dropdown-menu");
const dropdownMenuProps = {
  overlay: props.truthProp,
  zIndex: props.numericProp,
  duration: props.makeNumericProp(0.2),
  direction: props.makeStringProp("down"),
  activeColor: String,
  autoLocate: Boolean,
  closeOnClickOutside: props.truthProp,
  closeOnClickOverlay: props.truthProp,
  swipeThreshold: props.numericProp
};
const DROPDOWN_KEY = Symbol(name);
var stdin_default = Vue.defineComponent({
  name,
  props: dropdownMenuProps,
  setup(props2, {
    slots
  }) {
    const id = useId.useId();
    const root = Vue.ref();
    const barRef = Vue.ref();
    const offset = Vue.ref(0);
    const {
      children,
      linkChildren
    } = index_esm.useChildren(DROPDOWN_KEY);
    const scrollParent = index_esm.useScrollParent(root);
    const opened = Vue.computed(() => children.some((item) => item.state.showWrapper));
    const scrollable = Vue.computed(() => props2.swipeThreshold && children.length > +props2.swipeThreshold);
    const barStyle = Vue.computed(() => {
      if (opened.value && basic.isDef(props2.zIndex)) {
        return {
          zIndex: +props2.zIndex + 1
        };
      }
    });
    const close = () => {
      children.forEach((item) => {
        item.toggle(false);
      });
    };
    const onClickAway = () => {
      if (props2.closeOnClickOutside) {
        close();
      }
    };
    const updateOffset = () => {
      if (barRef.value) {
        const rect = index_esm.useRect(barRef);
        if (props2.direction === "down") {
          offset.value = rect.bottom;
        } else {
          offset.value = dom.windowHeight.value - rect.top;
        }
      }
    };
    const onScroll = () => {
      if (opened.value) {
        updateOffset();
      }
    };
    const toggleItem = (active) => {
      children.forEach((item, index) => {
        if (index === active) {
          item.toggle();
        } else if (item.state.showPopup) {
          item.toggle(false, {
            immediate: true
          });
        }
      });
    };
    const renderTitle = (item, index) => {
      const {
        showPopup
      } = item.state;
      const {
        disabled,
        titleClass
      } = item;
      return Vue.createVNode("div", {
        "id": `${id}-${index}`,
        "role": "button",
        "tabindex": disabled ? void 0 : 0,
        "data-allow-mismatch": "attribute",
        "class": [bem("item", {
          disabled,
          grow: scrollable.value
        }), {
          [constant.HAPTICS_FEEDBACK]: !disabled
        }],
        "onClick": () => {
          if (!disabled) {
            toggleItem(index);
          }
        }
      }, [Vue.createVNode("span", {
        "class": [bem("title", {
          down: showPopup === (props2.direction === "down"),
          active: showPopup
        }), titleClass],
        "style": {
          color: showPopup ? props2.activeColor : ""
        }
      }, [Vue.createVNode("div", {
        "class": "van-ellipsis"
      }, [item.renderTitle()])])]);
    };
    useExpose.useExpose({
      close,
      opened
    });
    linkChildren({
      id,
      props: props2,
      offset,
      opened,
      updateOffset
    });
    index_esm.useClickAway(root, onClickAway);
    index_esm.useEventListener("scroll", onScroll, {
      target: scrollParent,
      passive: true
    });
    return () => {
      var _a;
      return Vue.createVNode("div", {
        "ref": root,
        "class": bem()
      }, [Vue.createVNode("div", {
        "ref": barRef,
        "style": barStyle.value,
        "class": bem("bar", {
          opened: opened.value,
          scrollable: scrollable.value
        })
      }, [children.map(renderTitle)]), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
    };
  }
});
exports.DROPDOWN_KEY = DROPDOWN_KEY;
exports.default = stdin_default;
exports.dropdownMenuProps = dropdownMenuProps;
