/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var VueCompositionAPI = require('@vue/composition-api');
var tabs_index = require('../tabs/index.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var menu_headMenuProps = require('./head-menu-props.js');
var menu_vMenu = require('./v-menu.js');
require('../utils/map-props.js');
require('@babel/runtime/helpers/defineProperty');
require('@babel/runtime/helpers/objectWithoutProperties');
require('vue');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../tabs/tabs.js');
require('../_chunks/dep-c3e1ffac.js');
require('lodash/kebabCase');
require('../utils/event.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('../utils/mixins.js');
require('../tabs/props.js');
require('../tabs/tab-panel.js');
require('../hooks/useDestroyOnClose.js');
require('../tabs/tab-panel-props.js');
require('../tabs/tab-nav.js');
require('lodash/debounce');
require('@ldesign/icons-vue');
require('../tabs/tab-nav-item.js');
require('../utils/ripple.js');
require('../_common/js/utils/set-style.js');
require('../config.js');
require('../config-provider/useConfig.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);

var _HeadMenu = VueCompositionAPI.defineComponent({
  name: "THeadMenu",
  components: {
    Tabs: tabs_index.Tabs,
    TabPanel: tabs_index.TabPanel
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: menu_headMenuProps["default"],
  setup: function setup(props2, ctx) {
    var activeValue = VueCompositionAPI.ref(props2.defaultValue || props2.value);
    var activeValues = VueCompositionAPI.ref([]);
    var expandValues = VueCompositionAPI.ref(props2.defaultExpanded || props2.expanded || []);
    var classPrefix = hooks_useConfig.usePrefixClass();
    var theme = VueCompositionAPI.computed(function () {
      return props2.theme;
    });
    var menuClass = VueCompositionAPI.computed(function () {
      return ["".concat(classPrefix.value, "-menu"), "".concat(classPrefix.value, "-head-menu"), "".concat(classPrefix.value, "-menu--").concat(props2.theme)];
    });
    var mode = VueCompositionAPI.ref(props2.expandType);
    var submenu = VueCompositionAPI.reactive([]);
    var deliver = function deliver(evt) {
      var func = "on".concat(evt[0].toUpperCase() + evt.slice(1));
      return function (val) {
        if (typeof props2[func] === "function") props2[func](val);
        ctx.emit(evt, val);
      };
    };
    var emitChange = deliver("change");
    var emitExpand = deliver("expand");
    var vMenu = new menu_vMenu["default"]({
      isMutex: true,
      expandValues: expandValues.value
    });
    VueCompositionAPI.provide("TdMenu", {
      mode: mode,
      theme: theme,
      vMenu: vMenu,
      isHead: true,
      expandValues: expandValues,
      activeValue: activeValue,
      activeValues: activeValues,
      select: function select(value) {
        emitChange(value);
      },
      open: function open(value, type) {
        var expanded = _toConsumableArray__default["default"](expandValues.value);
        var 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);
      }
    });
    var handleTabChange = function handleTabChange(value) {
      emitChange(value);
    };
    var handleSubmenuExpand = function handleSubmenuExpand(value) {
      var ans = vMenu.getChild(value);
      submenu.length = 0;
      submenu.push.apply(submenu, _toConsumableArray__default["default"](ans));
    };
    VueCompositionAPI.watch(function () {
      return props2.expanded;
    }, function (value) {
      expandValues.value = value;
      if (mode.value === "normal") handleSubmenuExpand(value[0]);
    });
    var updateActiveValues = function updateActiveValues(value) {
      activeValue.value = value;
      activeValues.value = vMenu.select(value);
    };
    VueCompositionAPI.watch(function () {
      return props2.value;
    }, updateActiveValues);
    VueCompositionAPI.watch(function () {
      return props2.defaultValue;
    }, updateActiveValues);
    VueCompositionAPI.watch(function () {
      return props2.expandType;
    }, function (value) {
      mode.value = value;
    });
    VueCompositionAPI.onMounted(function () {
      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: mode,
      menuClass: menuClass,
      expandValues: expandValues,
      activeValue: activeValue,
      activeValues: activeValues,
      submenu: submenu,
      handleTabChange: handleTabChange,
      classPrefix: classPrefix
    };
  },
  methods: {
    renderNormalSubmenu: function renderNormalSubmenu() {
      var h = this.$createElement;
      if (this.submenu.length === 0) return null;
      return h("ul", {
        "class": ["".concat(this.classPrefix, "-head-menu__submenu"), "".concat(this.classPrefix, "-submenu")]
      }, [h("l-tabs", {
        "attrs": {
          "value": this.activeValue
        },
        "on": {
          "change": this.handleTabChange
        }
      }, [this.submenu.map(function (item) {
        return h("l-tab-panel", {
          "attrs": {
            "value": item.value,
            "label": item.vnode[0].text
          }
        });
      })])]);
    }
  },
  render: function render() {
    var h = arguments[0];
    if (this.$slots.options) console.warn("TDesign Warn: `options` slot is going to be deprecated, please use `operations` for slot instead.");
    var operations = utils_renderTnode.renderContent(this, "operations", "options");
    var logo = utils_renderTnode.renderTNodeJSX(this, "logo");
    return h("div", {
      "class": this.menuClass
    }, [h("div", {
      "class": "".concat(this.classPrefix, "-head-menu__inner")
    }, [logo && h("div", {
      "class": "".concat(this.classPrefix, "-menu__logo")
    }, [logo]), h("ul", {
      "class": "".concat(this.classPrefix, "-menu")
    }, [utils_renderTnode.renderContent(this, "default", "content")]), operations && h("div", {
      "class": "".concat(this.classPrefix, "-menu__operations")
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

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