/**
 * 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 isNumber = require('lodash/isNumber');
var isArray = require('lodash/isArray');
var utils_renderTnode = require('../utils/render-tnode.js');
var _common_js_log_log = require('../_common/js/log/log.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var menu_props = require('./props.js');
var menu_vMenu = require('./v-menu.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isObject');
require('lodash/camelCase');
require('lodash/kebabCase');
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('@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 isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);
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; }
var _Menu = vue.defineComponent({
  name: "TMenu",
  props: _objectSpread(_objectSpread({}, menu_props["default"]), {}, {
    onCollapsed: Function
  }),
  setup: function setup(props2, ctx) {
    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 mode = vue.ref(props2.expandType);
    var theme = vue.computed(function () {
      return props2.theme;
    });
    var isMutex = vue.computed(function () {
      return props2.expandMutex;
    });
    var menuClass = vue.computed(function () {
      return ["".concat(classPrefix.value, "-default-menu"), "".concat(classPrefix.value, "-menu--").concat(props2.theme), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-collapsed"), props2.collapsed)];
    });
    var innerClasses = vue.computed(function () {
      return ["".concat(classPrefix.value, "-menu"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-menu--scroll"), mode.value !== "popup"), "narrow-scrollbar"];
    });
    var expandWidth = vue.computed(function () {
      var width = props2.width;
      var format = function format(val) {
        return isNumber__default["default"](val) ? "".concat(val, "px") : val;
      };
      if (isArray__default["default"](width)) return width.map(function (item) {
        return format(item);
      });
      return [format(width), "64px"];
    });
    var styles = vue.computed(function () {
      return {
        height: "100%",
        width: props2.collapsed ? expandWidth.value[1] : expandWidth.value[0]
      };
    });
    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],
      setExpand = _useDefaultValue2[1];
    var activeValues = vue.ref([]);
    vue.watchEffect(function () {
      var _props2$onCollapsed;
      mode.value = props2.collapsed ? "popup" : props2.expandType;
      (_props2$onCollapsed = props2.onCollapsed) === null || _props2$onCollapsed === void 0 ? void 0 : _props2$onCollapsed.call(props2, {
        collapsed: props2.collapsed
      });
    });
    var vMenu = new menu_vMenu["default"]({
      isMutex: isMutex,
      expandValues: expandValues.value ? _toConsumableArray__default["default"](expandValues.value) : []
    });
    vue.provide("TdMenu", {
      activeValue: activeValue,
      activeValues: activeValues,
      expandValues: expandValues,
      mode: mode,
      theme: theme,
      isHead: false,
      vMenu: vMenu,
      select: function select(value2) {
        setActiveValue(value2);
      },
      open: function open(value2, type) {
        if (mode.value === "normal") {
          setExpand(vMenu.expand(value2));
        } else if (type === "add") {
          if (!expandValues.value.includes(value2)) {
            setExpand([].concat(_toConsumableArray__default["default"](expandValues.value), [value2]));
          }
        } else if (type === "remove") {
          var index = expandValues.value.indexOf(value2);
          var tmp = _toConsumableArray__default["default"](expandValues.value);
          tmp.splice(index, 1);
          setExpand(tmp);
        }
      }
    });
    vue.watch(function () {
      return props2.expanded;
    }, function (value2) {
      vMenu.expandValues = new Set(value2);
    });
    vue.watch(function () {
      return props2.collapsed;
    }, function (newValue, oldValue) {
      if (!newValue && oldValue) {
        setExpand(_toConsumableArray__default["default"](vMenu.expandValues));
      }
    });
    var updateActiveValues = function updateActiveValues(value2) {
      activeValues.value = vMenu.select(value2);
    };
    vue.watch(activeValue, updateActiveValues);
    vue.onMounted(function () {
      activeValues.value = vMenu.select(activeValue.value);
    });
    return {
      styles: styles,
      classPrefix: classPrefix,
      menuClass: menuClass,
      innerClasses: innerClasses,
      activeValue: activeValue,
      activeValues: activeValues,
      expandValues: expandValues
    };
  },
  render: function render() {
    var operations = utils_renderTnode.renderContent(this, "operations", "options");
    var logo = utils_renderTnode.renderTNodeJSX(this, "logo");
    return vue.createVNode("div", {
      "class": this.menuClass,
      "style": this.styles
    }, [vue.createVNode("div", {
      "class": "".concat(this.classPrefix, "-default-menu__inner")
    }, [logo && vue.createVNode("div", {
      "class": "".concat(this.classPrefix, "-menu__logo")
    }, [logo]), vue.createVNode("ul", {
      "class": this.innerClasses
    }, [utils_renderTnode.renderContent(this, "default", "content")]), operations && vue.createVNode("div", {
      "class": "".concat(this.classPrefix, "-menu__operations")
    }, [operations])])]);
  }
});

exports["default"] = _Menu;
//# sourceMappingURL=menu.js.map
