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

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { defineComponent, ref, computed, reactive, provide, watch, onMounted } from '@vue/composition-api';
import { prefix } from '../config.js';
import props from './head-menu-props.js';
import { Tabs, TabPanel } from '../tabs/index.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import VMenu from './v-menu.js';
import '../tabs/tabs.js';
import '../_chunks/dep-42d5c87e.js';
import '@babel/runtime/helpers/defineProperty';
import 'vue';
import 'lodash/kebabCase';
import '../tabs/props.js';
import '../tabs/tab-panel.js';
import '../tabs/tab-panel-props.js';
import '../tabs/tab-nav.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/debounce';
import 'tdesign-icons-vue';
import '../tabs/tab-nav-item.js';
import '../utils/event.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import '../utils/ripple.js';
import '../utils/set-style.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../tabs/type.js';
import '@babel/runtime/helpers/typeof';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

var _HeadMenu = defineComponent({
  name: "THeadMenu",
  props: props,
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    Tabs: Tabs,
    TabPanel: TabPanel
  },
  setup: function setup(props2, ctx) {
    var activeValue = ref(props2.defaultValue || props2.value);
    var activeValues = ref([]);
    var expandValues = ref(props2.defaultExpanded || props2.expanded || []);
    var theme = computed(function () {
      return props2.theme;
    });
    var menuClass = computed(function () {
      return ["".concat(prefix, "-menu"), "".concat(prefix, "-head-menu"), "".concat(prefix, "-menu--").concat(props2.theme)];
    });
    var mode = ref(props2.expandType);
    var submenu = 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 VMenu({
      isMutex: true,
      expandValues: expandValues.value
    });
    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(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(ans));
    };

    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);
    };

    watch(function () {
      return props2.value;
    }, updateActiveValues);
    watch(function () {
      return props2.defaultValue;
    }, updateActiveValues);
    watch(function () {
      return props2.expandType;
    }, function (value) {
      mode.value = value;
    });
    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(prefix, "-head-menu__submenu"), "".concat(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 = renderContent(this, "operations", "options");
    var logo = renderTNodeJSX(this, "logo");
    return h("div", {
      "class": this.menuClass
    }, [h("div", {
      "class": "".concat(prefix, "-head-menu__inner")
    }, [logo && h("div", {
      "class": "".concat(prefix, "-menu__logo")
    }, [logo]), h("ul", {
      "class": "".concat(prefix, "-menu")
    }, [renderContent(this, "default", "content")]), operations && h("div", {
      "class": "".concat(prefix, "-menu__operations")
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

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