import { defineComponent, toRefs, ref, watch, onMounted, provide, toRef, createElementBlock, openBlock, normalizeClass, unref, renderSlot } from "vue";
import { menuInjectKey } from "./provide.mjs";
import { menuProps } from "./types.mjs";
import MenuTree from "./menu.mjs";
import { isUndefined, isArray } from "../_utils/is.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "OMenu",
  props: menuProps,
  emits: ["update:modelValue", "change", "update:expanded", "expanded-change"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const menuTree = new MenuTree(NaN, null);
    const { size, accordion, modelValue, defaultValue, expanded, defaultExpanded } = toRefs(props);
    const realValue = ref((modelValue == null ? void 0 : modelValue.value) ?? defaultValue.value);
    watch(
      () => modelValue == null ? void 0 : modelValue.value,
      (val) => {
        if (!isUndefined(val)) {
          realValue.value = val;
        }
      }
    );
    const updateModelValue = (val) => {
      realValue.value = val;
      emits("update:modelValue", val);
      emits("change", val);
    };
    const activeNodes = ref([]);
    watch(
      () => realValue.value,
      (val) => {
        activeNodes.value = menuTree.selectNode(val);
      }
    );
    onMounted(() => {
      activeNodes.value = menuTree.selectNode(realValue.value);
    });
    const realExpanded = ref(isArray(expanded == null ? void 0 : expanded.value) ? expanded == null ? void 0 : expanded.value : defaultExpanded.value);
    watch(
      () => expanded == null ? void 0 : expanded.value,
      (val) => {
        if (isArray(val)) {
          realExpanded.value = val;
        }
      }
    );
    const updateExpanded = (val) => {
      realExpanded.value = val;
      emits("update:expanded", val);
      emits("expanded-change", val);
    };
    provide(menuInjectKey, {
      size,
      accordion,
      realValue,
      activeNodes,
      realExpanded,
      menuTree,
      updateModelValue,
      updateExpanded,
      arrowPosition: toRef(props, "arrowPosition")
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock(
        "ul",
        {
          class: normalizeClass([
            "o-menu",
            `o-menu-${unref(size)}`,
            _ctx.arrowPosition && `o-menu-arrow-${_ctx.arrowPosition}`
          ])
        },
        [
          renderSlot(_ctx.$slots, "default")
        ],
        2
        /* CLASS */
      );
    };
  }
});
export {
  _sfc_main as default
};
