/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { defineComponent, ref, computed, reactive, provide, watch, onMounted } from '@vue/composition-api';
import { HeadMenuProps } from './props.js';
import { Tabs, TabPanel } from '../tabs/index.js';
import { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import VMenu from './v-menu.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import '../tabs/tabs.js';
import '../../_chunks/dep-b13ef886.js';
import '@babel/runtime/helpers/defineProperty';
import 'vue';
import 'lodash/kebabCase';
import '../tabs/props.js';
import '../tabs/tab-panel.js';
import '../../hooks/use-destroy-onclose.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';
import '../../utils/mixins.js';
import '../tabs/tab-nav.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/debounce';
import '@ldesign/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 '../../_chunks/dep-85e8b23b.js';
import '../../config.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '@babel/runtime/helpers/typeof';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../config-provider/useConfig.js';

var _HeadMenu = defineComponent({
  name: "LHeadMenu",
  props: HeadMenuProps,
  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 classPrefix = usePrefixClass();
    var theme = computed(function () {
      return props2.theme;
    });
    var menuClass = computed(function () {
      return ["".concat(classPrefix.value, "-menu"), "".concat(classPrefix.value, "-head-menu"), "".concat(classPrefix.value, "-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("LdMenu", {
      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,
      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("LDesign Warn: `options` slot is going to be deprecated, please use `operations` for slot instead.");
    }

    var operations = renderContent(this, "operations", "options");
    var logo = renderLNodeJSX(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")
    }, [renderContent(this, "default", "content")]), operations && h("div", {
      "class": "".concat(this.classPrefix, "-menu__operations")
    }, [operations])]), this.mode === "normal" && this.renderNormalSubmenu()]);
  }
});

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