import { defineComponent, inject, computed, provide, ref, onMounted, createElementBlock, openBlock, unref, normalizeStyle, normalizeClass, createElementVNode, createBlock, createCommentVNode, createVNode, renderSlot, resolveDynamicComponent, withCtx, createTextVNode, toDisplayString } from "vue";
import { subMenuProps } from "./types.mjs";
import { IconChevronDownBold } from "../_utils/icons.mjs";
import { menuInjectKey, subMenuInjectKey } from "./provide.mjs";
import { isUndefined } from "../_utils/is.mjs";
import { isOverflown } from "../_utils/dom.mjs";
import { OPopover } from "../popover/index.mjs";
const _hoisted_1 = ["data-level"];
const _hoisted_2 = {
  key: 0,
  class: "o-sub-menu-arrow"
};
const _hoisted_3 = {
  key: 1,
  class: "o-sub-menu-title-icon"
};
const _hoisted_4 = {
  key: 2,
  class: "o-sub-menu-arrow"
};
const _hoisted_5 = { class: "o-sub-menu-children-wrap" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "OSubMenu",
  props: subMenuProps,
  setup(__props) {
    const props = __props;
    const menuInjection = inject(menuInjectKey, null);
    const subMenuInjection = inject(subMenuInjectKey, null);
    const { arrowPosition } = menuInjection || {};
    const isExpanded = computed(() => {
      if (isUndefined(props.value)) {
        return false;
      }
      if (menuInjection) {
        return menuInjection.realExpanded.value.includes(props.value);
      }
      return false;
    });
    const isAssociatedSelected = computed(() => {
      if (menuInjection) {
        return menuInjection.activeNodes.value.includes(props.value);
      }
      return false;
    });
    const isSelected = computed(() => {
      if (menuInjection) {
        return menuInjection.realValue.value === props.value;
      }
      return false;
    });
    const onSubItemClick = (ev) => {
      ev.stopPropagation();
      if (isUndefined(props.value)) {
        return;
      }
      let set = menuInjection ? /* @__PURE__ */ new Set([...menuInjection.realExpanded.value]) : /* @__PURE__ */ new Set([]);
      if (isExpanded.value && set.has(props.value)) {
        set.delete(props.value);
      }
      if (!isExpanded.value && !set.has(props.value)) {
        if (menuInjection == null ? void 0 : menuInjection.accordion.value) {
          const siblings = (menuInjection == null ? void 0 : menuInjection.menuTree.getSiblings(props.value)) || [];
          siblings.forEach((val) => {
            set.delete(val);
          });
        }
        set.add(props.value);
      }
      const expandedVal = Array.from(set);
      menuInjection == null ? void 0 : menuInjection.updateExpanded(expandedVal);
      if (props.selectable) {
        menuInjection == null ? void 0 : menuInjection.updateModelValue(props.value);
      }
    };
    const currentDepth = subMenuInjection ? subMenuInjection.parentDepth + 1 : 0;
    provide(subMenuInjectKey, {
      value: props.value,
      parentDepth: currentDepth
    });
    menuInjection == null ? void 0 : menuInjection.menuTree.addChild({
      value: props.value,
      parentVal: subMenuInjection == null ? void 0 : subMenuInjection.value
    });
    const subMenuTitleRef = ref();
    const itemContentRef = ref();
    const isContentOverflow = ref(false);
    const content = ref("");
    onMounted(() => {
      var _a;
      if (!itemContentRef.value) {
        return;
      }
      isContentOverflow.value = isOverflown(itemContentRef.value);
      content.value = ((_a = itemContentRef.value) == null ? void 0 : _a.innerText) || "";
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("li", {
        class: normalizeClass({
          "o-sub-menu": true,
          "o-sub-menu-selected": isSelected.value,
          "o-sub-menu-associated-selected": isAssociatedSelected.value,
          "o-sub-menu-expanded": isExpanded.value
        }),
        style: normalizeStyle({ "--menu-level": unref(currentDepth) }),
        "data-level": unref(currentDepth),
        onClick: onSubItemClick
      }, [
        createElementVNode(
          "div",
          {
            class: "o-sub-menu-title",
            ref_key: "subMenuTitleRef",
            ref: subMenuTitleRef
          },
          [
            unref(arrowPosition) === "left" ? (openBlock(), createElementBlock("div", _hoisted_2, [
              createVNode(unref(IconChevronDownBold))
            ])) : createCommentVNode("v-if", true),
            _ctx.$slots.icon || props.icon ? (openBlock(), createElementBlock("div", _hoisted_3, [
              renderSlot(_ctx.$slots, "icon", {}, () => [
                (openBlock(), createBlock(resolveDynamicComponent(props.icon)))
              ])
            ])) : createCommentVNode("v-if", true),
            createElementVNode(
              "div",
              {
                ref_key: "itemContentRef",
                ref: itemContentRef,
                class: "o-sub-menu-title-content"
              },
              [
                renderSlot(_ctx.$slots, "title")
              ],
              512
              /* NEED_PATCH */
            ),
            !unref(arrowPosition) || unref(arrowPosition) === "right" ? (openBlock(), createElementBlock("div", _hoisted_4, [
              createVNode(unref(IconChevronDownBold))
            ])) : createCommentVNode("v-if", true)
          ],
          512
          /* NEED_PATCH */
        ),
        createElementVNode(
          "ul",
          {
            class: normalizeClass(["o-sub-menu-children", { "expanded": isExpanded.value }])
          },
          [
            createElementVNode("div", _hoisted_5, [
              renderSlot(_ctx.$slots, "default")
            ])
          ],
          2
          /* CLASS */
        ),
        isContentOverflow.value ? (openBlock(), createBlock(unref(OPopover), {
          key: 0,
          offset: 12,
          target: subMenuTitleRef.value,
          position: "bottom",
          wrapClass: "o-menu-popover"
        }, {
          default: withCtx(() => [
            createTextVNode(
              toDisplayString(content.value),
              1
              /* TEXT */
            )
          ]),
          _: 1
          /* STABLE */
        }, 8, ["target"])) : createCommentVNode("v-if", true)
      ], 14, _hoisted_1);
    };
  }
});
export {
  _sfc_main as default
};
