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

import { defineComponent, ref, computed, reactive, provide, watch, onMounted } from '@vue/composition-api';
import '../../chunks/index.js';
import { Tabs, TabPanel } from '../tabs/index.js';
import { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { HeadMenuProps } from './props.js';
import VMenu from './v-menu.js';
import { l as log } from '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/map-props.js';
import 'vue';
import '../../utils/with-install.js';
import 'lodash';
import '../../config.js';
import '../tabs/tabs.js';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/kebabCase';
import '../../utils/event.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/mixins.js';
import '../tabs/props.js';
import '../tabs/tab-panel.js';
import '../../hooks/use-destroy-onclose.js';
import '../tabs/tab-nav.js';
import 'lodash/debounce';
import '@ldesign/icons-vue';
import '../tabs/tab-nav-item.js';
import '../../utils/ripple.js';
import '../../chunks/set-style.js';
import '../tabs/style/css.js';
import '../tabs/type.js';
import '../config-provider/useConfig.js';
import '@babel/runtime/helpers/defineProperty';

var _HeadMenu = defineComponent({
  name: "LHeadMenu",
  components: {
    Tabs,
    TabPanel
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: HeadMenuProps,
  setup(props2, ctx) {
    const activeValue = ref(props2.defaultValue || props2.value);
    const activeValues = ref([]);
    const expandValues = ref(props2.defaultExpanded || props2.expanded || []);
    const classPrefix = usePrefixClass();
    const theme = computed(() => props2.theme);
    const menuClass = computed(() => [`${classPrefix.value}-menu`, `${classPrefix.value}-head-menu`, `${classPrefix.value}-menu--${props2.theme}`]);
    const mode = ref(props2.expandType);
    const submenu = reactive([]);
    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");
    const vMenu = new VMenu({
      isMutex: true,
      expandValues: expandValues.value
    });
    provide("LdMenu", {
      mode,
      theme,
      vMenu,
      isHead: true,
      expandValues,
      activeValue,
      activeValues,
      select: value => {
        emitChange(value);
      },
      open: (value, type) => {
        const expanded = [...expandValues.value];
        const index = expanded.indexOf(value);
        if (mode.value === "popup") {
          if (type === "add") {
            if (index === -1) {
              expanded.push(value);
            }
          } else if (type === "remove") {
            expanded.splice(index, 1);
          }
        } else {
          expanded.splice(0, 1);
          if (index === -1) expanded.push(value);
        }
        emitExpand(expanded);
      }
    });
    const handleTabChange = value => {
      emitChange(value);
    };
    const handleSubmenuExpand = value => {
      const ans = vMenu.getChild(value);
      submenu.length = 0;
      submenu.push(...ans);
    };
    watch(() => props2.expanded, value => {
      expandValues.value = value;
      if (mode.value === "normal") handleSubmenuExpand(value[0]);
    });
    const updateActiveValues = value => {
      activeValue.value = value;
      activeValues.value = vMenu.select(value);
    };
    watch(() => props2.value, updateActiveValues);
    watch(() => props2.defaultValue, updateActiveValues);
    watch(() => props2.expandType, value => {
      mode.value = value;
    });
    onMounted(() => {
      var _expandValues$value;
      activeValues.value = vMenu.select(activeValue.value);
      if (((_expandValues$value = expandValues.value) === null || _expandValues$value === void 0 ? void 0 : _expandValues$value.length) > 0) handleSubmenuExpand(expandValues.value[0]);
    });
    return {
      mode,
      menuClass,
      expandValues,
      activeValue,
      activeValues,
      submenu,
      handleTabChange,
      classPrefix
    };
  },
  methods: {
    renderNormalSubmenu() {
      const h = this.$createElement;
      if (this.submenu.length === 0) return null;
      return h("ul", {
        "class": [`${this.classPrefix}-head-menu__submenu`, `${this.classPrefix}-submenu`]
      }, [h("l-tabs", {
        "attrs": {
          "value": this.activeValue
        },
        "on": {
          "change": this.handleTabChange
        }
      }, [this.submenu.map(item => h("l-tab-panel", {
        "attrs": {
          "value": item.value,
          "label": item.vnode[0].text
        }
      }))])]);
    }
  },
  render() {
    const h = arguments[0];
    if (this.$slots.options) log.warn("HeadMenu", "`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
    }, [h("div", {
      "class": `${this.classPrefix}-head-menu__inner`
    }, [logo && h("div", {
      "class": `${this.classPrefix}-menu__logo`
    }, [logo]), h("ul", {
      "class": `${this.classPrefix}-menu`
    }, [renderContent(this, "default", "content")]), operations && h("div", {
      "class": `${this.classPrefix}-menu__operations`
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

export { _HeadMenu as default };
//# sourceMappingURL=head-menu.js.map
