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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var VueCompositionAPI = require('@vue/composition-api');
var log = require('../../chunks/log.js');
var utils_renderNode = require('../../utils/render-node.js');
var hooks_useConfig = require('../../hooks/use-config.js');
var components_menu_props = require('./props.js');
var components_menu_vMenu = require('./v-menu.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('vue');
require('lodash');
require('../config-provider/useConfig.js');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('@babel/runtime/helpers/defineProperty');

var _Menu = VueCompositionAPI.defineComponent({
  name: "LMenu",
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    ...components_menu_props.MenuProps
  },
  setup(props2, ctx) {
    const mode = VueCompositionAPI.ref(props2.expandType);
    const theme = VueCompositionAPI.computed(() => props2.theme);
    const isMutex = VueCompositionAPI.computed(() => props2.expandMutex);
    const classPrefix = hooks_useConfig.usePrefixClass();
    const menuClass = VueCompositionAPI.computed(() => [`${classPrefix.value}-default-menu`, `${classPrefix.value}-menu--${props2.theme}`, {
      [`${classPrefix.value}-is-collapsed`]: props2.collapsed
    }]);
    const innerClasses = VueCompositionAPI.computed(() => [`${classPrefix.value}-menu`, {
      [`${classPrefix.value}-menu--scroll`]: mode.value !== "popup"
    }, "narrow-scrollbar"]);
    const expandWidth = VueCompositionAPI.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 = VueCompositionAPI.computed(() => ({
      height: "100%",
      width: props2.collapsed ? expandWidth.value[1] : expandWidth.value[0]
    }));
    const activeValue = VueCompositionAPI.ref(props2.defaultValue || props2.value);
    const activeValues = VueCompositionAPI.ref([]);
    const expandValues = VueCompositionAPI.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");
    VueCompositionAPI.watchEffect(() => {
      mode.value = props2.collapsed ? "popup" : props2.expandType;
    });
    const vMenu = new components_menu_vMenu["default"]({
      isMutex: isMutex.value,
      expandValues: expandValues.value
    });
    VueCompositionAPI.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);
      }
    });
    VueCompositionAPI.watch(() => props2.expanded, value => {
      expandValues.value = value;
      vMenu.expandValues = new Set(value);
    });
    const updateActiveValues = value => {
      activeValue.value = value;
      activeValues.value = vMenu.select(value);
    };
    VueCompositionAPI.watch(() => props2.value, updateActiveValues);
    VueCompositionAPI.watch(() => props2.defaultValue, updateActiveValues);
    VueCompositionAPI.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.log.warn("Menu", "`options` slot is going to be deprecated, please use `operations` for slot instead.");
    const operations = utils_renderNode.renderContent(this, "operations", "options");
    const logo = utils_renderNode.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
    }, [utils_renderNode.renderContent(this, "default", "content")]), operations && h("div", {
      "class": `${this.classPrefix}-menu__operations`
    }, [operations])])]);
  }
});

exports["default"] = _Menu;
//# sourceMappingURL=menu.js.map
