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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var isFunction = require('lodash/isFunction');
var isArray = require('lodash/isArray');
var _common_js_log_log = require('../_common/js/log/log.js');
var tabs_index = require('../tabs/index.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var menu_headMenuProps = require('./head-menu-props.js');
var menu_submenu = require('./submenu.js');
var menu_vMenu = require('./v-menu.js');
require('../utils/withInstall.js');
require('../tabs/tabs.js');
require('../hooks/tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('../tabs/tab-panel.js');
require('../hooks/useDestroyOnClose.js');
require('../tabs/tab-panel-props.js');
require('../tabs/tab-nav.js');
require('lodash/debounce');
require('../_common/js/tabs/base.js');
require('../hooks/useListener.js');
require('../hooks/useGlobalIcon.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
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-7f0559c3.js');
require('dayjs');
require('../hooks/useDragSort.js');
require('../tabs/props.js');
require('../tabs/tab-nav-item.js');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../tabs/tab-nav-bar.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isUndefined');
require('lodash/isNull');
require('lodash/isNumber');
require('../common-components/fake-arrow.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');
require('../popup/props.js');
require('./submenu-props.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 _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s);
}
var _HeadMenu = vue.defineComponent({
  name: "THeadMenu",
  props: _objectSpread({}, menu_headMenuProps["default"]),
  setup: function setup(props2, ctx) {
    var _ctx$slots$default, _ctx$slots, _ctx$slots$content, _ctx$slots2;
    var classPrefix = hooks_useConfig.usePrefixClass();
    vue.watchEffect(function () {
      if (ctx.slots.options) _common_js_log_log["default"].warnOnce("TMenu", "`options` slot is going to be deprecated, please use `operations` for slot instead.");
    });
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue,
      expanded = _toRefs.expanded;
    var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      activeValue = _useVModel2[0],
      setActiveValue = _useVModel2[1];
    var _useDefaultValue = hooks_useDefaultValue["default"](expanded, props2.defaultExpanded, props2.onExpand, "expanded"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      expandValues = _useDefaultValue2[0],
      setExpanded = _useDefaultValue2[1];
    var activeValues = vue.ref([]);
    var theme = vue.computed(function () {
      return props2.theme;
    });
    var menuClass = vue.computed(function () {
      return ["".concat(classPrefix.value, "-menu"), "".concat(classPrefix.value, "-head-menu"), "".concat(classPrefix.value, "-menu--").concat(props2.theme)];
    });
    var mode = vue.ref(props2.expandType);
    var submenu = vue.reactive([]);
    var vMenu = new menu_vMenu["default"]({
      isMutex: true,
      expandValues: expandValues.value
    });
    vue.provide("TdMenu", {
      mode: mode,
      theme: theme,
      vMenu: vMenu,
      isHead: true,
      expandValues: expandValues,
      activeValue: activeValue,
      activeValues: activeValues,
      select: function select(value2) {
        setActiveValue(value2);
      },
      open: function open(value2, type) {
        var expanded2 = _toConsumableArray__default["default"](expandValues.value);
        var index = expanded2.indexOf(value2);
        if (mode.value === "popup") {
          if (type === "add") {
            if (index === -1) {
              expanded2.push(value2);
            }
          } else if (type === "remove") {
            expanded2.splice(index, 1);
          }
        } else if (mode.value === "normal" && value2 !== void 0) {
          expanded2.splice(0, 1);
          if (index === -1) expanded2.push(value2);
        }
        setExpanded(expanded2);
      }
    });
    var handleTabChange = function handleTabChange(value2) {
      setActiveValue(value2);
    };
    var handleSubmenuExpand = function handleSubmenuExpand(value2) {
      var ans = vMenu.getChild(value2);
      submenu.length = 0;
      submenu.push.apply(submenu, _toConsumableArray__default["default"](ans));
    };
    vue.watch(expandValues, function (value2) {
      if (mode.value === "normal") handleSubmenuExpand(value2[0]);
    });
    var updateActiveValues = function updateActiveValues(value2) {
      activeValues.value = vMenu.select(value2);
    };
    vue.watch(activeValue, updateActiveValues);
    vue.watch(function () {
      return props2.expandType;
    }, function (value2) {
      mode.value = value2;
    });
    vue.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]);
    });
    var renderNormalSubmenu = function renderNormalSubmenu() {
      var _slot;
      if (submenu.length === 0) return null;
      return vue.createVNode("ul", {
        "class": ["".concat(classPrefix.value, "-head-menu__submenu"), "".concat(classPrefix.value, "-submenu")]
      }, [vue.createVNode(tabs_index.Tabs, {
        "value": activeValue.value,
        "onChange": handleTabChange
      }, _isSlot(_slot = submenu.map(function (item) {
        var _item$vnode$;
        return vue.createVNode(tabs_index.TabPanel, {
          "value": item.value,
          "label": (_item$vnode$ = item.vnode()[0]) === null || _item$vnode$ === void 0 ? void 0 : _item$vnode$.children
        }, null);
      })) ? _slot : {
        "default": function _default() {
          return [_slot];
        }
      })]);
    };
    var menuRef = vue.ref();
    var innerRef = vue.ref();
    var logoRef = vue.ref();
    var operationRef = vue.ref();
    var getComputedCss = function getComputedCss(el, cssProperty) {
      var _getComputedStyle$css;
      return (_getComputedStyle$css = getComputedStyle(el)[cssProperty]) !== null && _getComputedStyle$css !== void 0 ? _getComputedStyle$css : "";
    };
    var getComputedCssValue = function getComputedCssValue(el, cssProperty) {
      return Number.parseInt(String(getComputedCss(el, cssProperty)), 10);
    };
    var calcMenuWidth = function calcMenuWidth() {
      var menuPaddingLeft = getComputedCssValue(menuRef.value, "paddingLeft");
      var menuPaddingRight = getComputedCssValue(menuRef.value, "paddingRight");
      var totalWidth = innerRef.value.clientWidth;
      if (logoRef.value) {
        var logoMarginLeft = getComputedCssValue(logoRef.value, "marginLeft");
        var logoMarginRight = getComputedCssValue(logoRef.value, "marginRight");
        totalWidth = totalWidth - logoRef.value.offsetWidth - logoMarginLeft - logoMarginRight;
      }
      if (operationRef.value) {
        var operationMarginLeft = getComputedCssValue(operationRef.value, "marginLeft");
        var operationMarginRight = getComputedCssValue(operationRef.value, "marginRight");
        totalWidth = totalWidth - operationRef.value.offsetWidth - operationMarginLeft - operationMarginRight;
      }
      return totalWidth - menuPaddingLeft - menuPaddingRight;
    };
    var initVMenu = function initVMenu(slots, parentValue) {
      slots.forEach(function (node) {
        var _node$props, _node$type, _node$type2, _node$children;
        var nodeValue = (_node$props = node.props) === null || _node$props === void 0 ? void 0 : _node$props.value;
        if (((_node$type = node.type) === null || _node$type === void 0 ? void 0 : _node$type.name) === "TSubmenu" || ((_node$type2 = node.type) === null || _node$type2 === void 0 ? void 0 : _node$type2.name) === "TMenuItem") vMenu.add({
          value: nodeValue,
          parent: parentValue,
          vnode: node.children["default"]
        });
        if (isFunction__default["default"]((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children["default"])) {
          initVMenu(node.children["default"](), nodeValue);
          return;
        }
        if (isArray__default["default"](node.children)) initVMenu(node.children, nodeValue);
      });
    };
    initVMenu(((_ctx$slots$default = (_ctx$slots = ctx.slots)["default"]) === null || _ctx$slots$default === void 0 ? void 0 : _ctx$slots$default.call(_ctx$slots)) || ((_ctx$slots$content = (_ctx$slots2 = ctx.slots).content) === null || _ctx$slots$content === void 0 ? void 0 : _ctx$slots$content.call(_ctx$slots2)) || []);
    return function () {
      var _props2$logo, _ctx$slots$logo, _ctx$slots3, _props2$operations, _ctx$slots$operations, _ctx$slots4, _ctx$slots$options, _ctx$slots5;
      var logo = ((_props2$logo = props2.logo) === null || _props2$logo === void 0 ? void 0 : _props2$logo.call(props2, vue.h)) || ((_ctx$slots$logo = (_ctx$slots3 = ctx.slots).logo) === null || _ctx$slots$logo === void 0 ? void 0 : _ctx$slots$logo.call(_ctx$slots3));
      var operations = ((_props2$operations = props2.operations) === null || _props2$operations === void 0 ? void 0 : _props2$operations.call(props2, vue.h)) || ((_ctx$slots$operations = (_ctx$slots4 = ctx.slots).operations) === null || _ctx$slots$operations === void 0 ? void 0 : _ctx$slots$operations.call(_ctx$slots4)) || ((_ctx$slots$options = (_ctx$slots5 = ctx.slots).options) === null || _ctx$slots$options === void 0 ? void 0 : _ctx$slots$options.call(_ctx$slots5));
      var formatContent = function formatContent() {
        var _ctx$slots$default2, _ctx$slots6, _ctx$slots$content2, _ctx$slots7;
        var slot = ((_ctx$slots$default2 = (_ctx$slots6 = ctx.slots)["default"]) === null || _ctx$slots$default2 === void 0 ? void 0 : _ctx$slots$default2.call(_ctx$slots6)) || ((_ctx$slots$content2 = (_ctx$slots7 = ctx.slots).content) === null || _ctx$slots$content2 === void 0 ? void 0 : _ctx$slots$content2.call(_ctx$slots7)) || [];
        if (menuRef.value && innerRef.value) {
          var _menuRef$value$childN;
          var validNodes = Array.from((_menuRef$value$childN = menuRef.value.childNodes) !== null && _menuRef$value$childN !== void 0 ? _menuRef$value$childN : []).filter(function (item) {
            return item.nodeName !== "#text" || item.nodeValue;
          });
          var menuWidth = calcMenuWidth();
          var menuItemMinWidth = 104;
          var remainWidth = menuWidth;
          var sliceIndex = validNodes.length;
          for (var index = 0; index < validNodes.length; index++) {
            var element = validNodes[index];
            remainWidth -= element.offsetWidth || 0;
            if (remainWidth < menuItemMinWidth) {
              sliceIndex = index;
              break;
            }
          }
          var defaultSlot = slot.slice(0, sliceIndex);
          var subMore = slot.slice(sliceIndex);
          if (subMore.length) {
            slot = defaultSlot.concat(vue.createVNode(menu_submenu["default"], {
              "expandType": "popup",
              "title": function title() {
                return vue.createVNode(iconsVueNext.EllipsisIcon, null, null);
              }
            }, _isSlot(subMore) ? subMore : {
              "default": function _default() {
                return [subMore];
              }
            }));
          }
        }
        return slot;
      };
      var content = formatContent();
      return vue.createVNode("div", {
        "class": menuClass.value
      }, [vue.createVNode("div", {
        "class": "".concat(classPrefix.value, "-head-menu__inner"),
        "ref": innerRef
      }, [logo && vue.createVNode("div", {
        "class": "".concat(classPrefix.value, "-menu__logo"),
        "ref": logoRef
      }, [logo]), vue.createVNode("ul", {
        "class": "".concat(classPrefix.value, "-menu"),
        "ref": menuRef
      }, [content]), operations && vue.createVNode("div", {
        "class": "".concat(classPrefix.value, "-menu__operations"),
        "ref": operationRef
      }, [operations])]), mode.value === "normal" && renderNormalSubmenu()]);
    };
  }
});

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