/**
 * 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 components_tabs_index = require('../tabs/index.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('../../utils/map-props.js');
require('vue');
require('../../utils/with-install.js');
require('lodash');
require('../../config.js');
require('../tabs/tabs.js');
require('../../chunks/helper.js');
require('lodash/kebabCase');
require('../../utils/event.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/mixins.js');
require('../tabs/props.js');
require('../tabs/tab-panel.js');
require('../../hooks/use-destroy-onclose.js');
require('../tabs/tab-nav.js');
require('lodash/debounce');
require('@ldesign/icons-vue');
require('../tabs/tab-nav-item.js');
require('../../utils/ripple.js');
require('../../chunks/set-style.js');
require('../config-provider/useConfig.js');
require('@babel/runtime/helpers/defineProperty');

var _HeadMenu = VueCompositionAPI.defineComponent({
  name: "LHeadMenu",
  components: {
    Tabs: components_tabs_index.Tabs,
    TabPanel: components_tabs_index.TabPanel
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: components_menu_props.HeadMenuProps,
  setup(props2, ctx) {
    const activeValue = VueCompositionAPI.ref(props2.defaultValue || props2.value);
    const activeValues = VueCompositionAPI.ref([]);
    const expandValues = VueCompositionAPI.ref(props2.defaultExpanded || props2.expanded || []);
    const classPrefix = hooks_useConfig.usePrefixClass();
    const theme = VueCompositionAPI.computed(() => props2.theme);
    const menuClass = VueCompositionAPI.computed(() => [`${classPrefix.value}-menu`, `${classPrefix.value}-head-menu`, `${classPrefix.value}-menu--${props2.theme}`]);
    const mode = VueCompositionAPI.ref(props2.expandType);
    const submenu = VueCompositionAPI.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 components_menu_vMenu["default"]({
      isMutex: true,
      expandValues: expandValues.value
    });
    VueCompositionAPI.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);
    };
    VueCompositionAPI.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);
    };
    VueCompositionAPI.watch(() => props2.value, updateActiveValues);
    VueCompositionAPI.watch(() => props2.defaultValue, updateActiveValues);
    VueCompositionAPI.watch(() => props2.expandType, value => {
      mode.value = value;
    });
    VueCompositionAPI.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.log.warn("HeadMenu", "`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
    }, [h("div", {
      "class": `${this.classPrefix}-head-menu__inner`
    }, [logo && h("div", {
      "class": `${this.classPrefix}-menu__logo`
    }, [logo]), h("ul", {
      "class": `${this.classPrefix}-menu`
    }, [utils_renderNode.renderContent(this, "default", "content")]), operations && h("div", {
      "class": `${this.classPrefix}-menu__operations`
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

exports["default"] = _HeadMenu;
//# sourceMappingURL=head-menu.js.map
