/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { defineComponent, ref, computed, watchEffect, provide, watch, onMounted } from '@vue/composition-api';
import '../../chunks/index.js';
import { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { MenuProps } from './props.js';
import VMenu from './v-menu.js';
import { l as log } from '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import 'vue';
import 'lodash';
import '../config-provider/useConfig.js';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '@babel/runtime/helpers/defineProperty';

var _Menu = defineComponent({
  name: "LMenu",
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    ...MenuProps
  },
  setup(props2, ctx) {
    const mode = ref(props2.expandType);
    const theme = computed(() => props2.theme);
    const isMutex = computed(() => props2.expandMutex);
    const classPrefix = usePrefixClass();
    const menuClass = computed(() => [`${classPrefix.value}-default-menu`, `${classPrefix.value}-menu--${props2.theme}`, {
      [`${classPrefix.value}-is-collapsed`]: props2.collapsed
    }]);
    const innerClasses = computed(() => [`${classPrefix.value}-menu`, {
      [`${classPrefix.value}-menu--scroll`]: mode.value !== "popup"
    }, "narrow-scrollbar"]);
    const expandWidth = computed(() => {
      const {
        width
      } = props2;
      const format = val => typeof val === "number" ? `${val}px` : val;
      if (Array.isArray(width)) return width.map(item => format(item));
      return [format(width), "64px"];
    });
    const styles = computed(() => ({
      height: "100%",
      width: props2.collapsed ? expandWidth.value[1] : expandWidth.value[0]
    }));
    const activeValue = ref(props2.defaultValue || props2.value);
    const activeValues = ref([]);
    const expandValues = ref(props2.expanded || []);
    const deliver = evt => {
      const func = `on${evt[0].toUpperCase() + evt.slice(1)}`;
      return val => {
        if (typeof props2[func] === "function") props2[func](val);
        ctx.emit(evt, val);
      };
    };
    const emitChange = deliver("change");
    const emitExpand = deliver("expand");
    watchEffect(() => {
      mode.value = props2.collapsed ? "popup" : props2.expandType;
    });
    const vMenu = new VMenu({
      isMutex: isMutex.value,
      expandValues: expandValues.value
    });
    provide("LdMenu", {
      activeValue,
      activeValues,
      expandValues,
      mode,
      theme,
      isHead: false,
      vMenu,
      select: value => {
        emitChange(value);
      },
      open: (value, type) => {
        let expanded = [...expandValues.value];
        if (mode.value === "normal") {
          expanded = vMenu.expand(value);
        } else {
          const index = expanded.indexOf(value);
          if (type === "add") {
            if (index === -1) {
              expanded.push(value);
            }
          } else if (type === "remove") {
            expanded.splice(index, 1);
          }
        }
        emitExpand(expanded);
      }
    });
    watch(() => props2.expanded, value => {
      expandValues.value = value;
      vMenu.expandValues = new Set(value);
    });
    const updateActiveValues = value => {
      activeValue.value = value;
      activeValues.value = vMenu.select(value);
    };
    watch(() => props2.value, updateActiveValues);
    watch(() => props2.defaultValue, updateActiveValues);
    onMounted(() => {
      activeValues.value = vMenu.select(activeValue.value);
    });
    return {
      styles,
      menuClass,
      innerClasses,
      activeValue,
      activeValues,
      expandValues,
      classPrefix
    };
  },
  render() {
    const h = arguments[0];
    if (this.$slots.options) log.warn("Menu", "`options` slot is going to be deprecated, please use `operations` for slot instead.");
    const operations = renderContent(this, "operations", "options");
    const logo = renderLNodeJSX(this, "logo");
    return h("div", {
      "class": this.menuClass,
      "style": this.styles
    }, [h("div", {
      "class": `${this.classPrefix}-default-menu__inner`
    }, [logo && h("div", {
      "class": `${this.classPrefix}-menu__logo`
    }, [logo]), h("ul", {
      "class": this.innerClasses
    }, [renderContent(this, "default", "content")]), operations && h("div", {
      "class": `${this.classPrefix}-menu__operations`
    }, [operations])])]);
  }
});

export { _Menu as default };
//# sourceMappingURL=menu.js.map
