/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var VueCompositionAPI = require('@vue/composition-api');
var config = require('../config.js');
var menu_headMenuProps = require('./head-menu-props.js');
var tabs_index = require('../tabs/index.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var menu_vMenu = require('./v-menu.js');
require('../tabs/tabs.js');
require('../_chunks/dep-44e6bdf6.js');
require('@babel/runtime/helpers/defineProperty');
require('vue');
require('lodash/kebabCase');
require('../tabs/props.js');
require('../tabs/tab-panel.js');
require('../tabs/tab-panel-props.js');
require('../tabs/tab-nav.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/debounce');
require('tdesign-icons-vue');
require('../tabs/tab-nav-item.js');
require('../utils/event.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');
require('../utils/ripple.js');
require('../utils/set-style.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('@babel/runtime/helpers/typeof');
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",
  props: menu_headMenuProps["default"],
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    Tabs: tabs_index.Tabs,
    TabPanel: tabs_index.TabPanel
  },
  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 theme = VueCompositionAPI.computed(function () {
      return props2.theme;
    });
    var menuClass = VueCompositionAPI.computed(function () {
      return ["".concat(config.prefix, "-menu"), "".concat(config.prefix, "-head-menu"), "".concat(config.prefix, "-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
    };
  },
  methods: {
    renderNormalSubmenu: function renderNormalSubmenu() {
      var h = this.$createElement;
      if (this.submenu.length === 0) return null;
      return h("ul", {
        "class": ["".concat(config.prefix, "-head-menu__submenu"), "".concat(config.prefix, "-submenu")]
      }, [h("t-tabs", {
        "attrs": {
          "value": this.activeValue
        },
        "on": {
          "change": this.handleTabChange
        }
      }, [this.submenu.map(function (item) {
        return h("t-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(config.prefix, "-head-menu__inner")
    }, [logo && h("div", {
      "class": "".concat(config.prefix, "-menu__logo")
    }, [logo]), h("ul", {
      "class": "".concat(config.prefix, "-menu")
    }, [utils_renderTnode.renderContent(this, "default", "content")]), operations && h("div", {
      "class": "".concat(config.prefix, "-menu__operations")
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

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