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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var components_menu_props = require('./props.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_menu_vMenu = require('./v-menu.js');
var hooks_useConfig = require('../../hooks/use-config.js');
require('@babel/runtime/helpers/typeof');
require('vue');
require('lodash/camelCase');
require('lodash/kebabCase');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('../config-provider/useConfig.js');
require('../config-provider/context.js');
require('lodash');
require('../../chunks/zh_CN.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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 = VueCompositionAPI.defineComponent({
  name: "LMenu",
  props: _objectSpread({}, components_menu_props.MenuProps),
  model: {
    prop: "value",
    event: "change"
  },
  setup: function setup(props2, ctx) {
    var mode = VueCompositionAPI.ref(props2.expandType);
    var theme = VueCompositionAPI.computed(function () {
      return props2.theme;
    });
    var isMutex = VueCompositionAPI.computed(function () {
      return props2.expandMutex;
    });
    var classPrefix = hooks_useConfig.usePrefixClass();
    var menuClass = VueCompositionAPI.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 = VueCompositionAPI.computed(function () {
      return ["".concat(classPrefix.value, "-menu"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-menu--scroll"), mode.value !== "popup"), "narrow-scrollbar"];
    });
    var expandWidth = VueCompositionAPI.computed(function () {
      var width = props2.width;

      var format = function format(val) {
        return typeof val === "number" ? "".concat(val, "px") : val;
      };

      if (Array.isArray(width)) return width.map(function (item) {
        return format(item);
      });
      return [format(width), "64px"];
    });
    var styles = VueCompositionAPI.computed(function () {
      return {
        height: "100%",
        width: props2.collapsed ? expandWidth.value[1] : expandWidth.value[0]
      };
    });
    var activeValue = VueCompositionAPI.ref(props2.defaultValue || props2.value);
    var activeValues = VueCompositionAPI.ref([]);
    var expandValues = VueCompositionAPI.ref(props2.expanded || []);

    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");
    VueCompositionAPI.watchEffect(function () {
      mode.value = props2.collapsed ? "popup" : props2.expandType;
    });
    var vMenu = new components_menu_vMenu["default"]({
      isMutex: isMutex.value,
      expandValues: expandValues.value
    });
    VueCompositionAPI.provide("LdMenu", {
      activeValue: activeValue,
      activeValues: activeValues,
      expandValues: expandValues,
      mode: mode,
      theme: theme,
      isHead: false,
      vMenu: vMenu,
      select: function select(value) {
        emitChange(value);
      },
      open: function open(value, type) {
        var expanded = _toConsumableArray__default["default"](expandValues.value);

        if (mode.value === "normal") {
          expanded = vMenu.expand(value);
        } else {
          var index = expanded.indexOf(value);

          if (type === "add") {
            if (index === -1) {
              expanded.push(value);
            }
          } else if (type === "remove") {
            expanded.splice(index, 1);
          }
        }

        emitExpand(expanded);
      }
    });
    VueCompositionAPI.watch(function () {
      return props2.expanded;
    }, function (value) {
      expandValues.value = value;
      vMenu.expandValues = new Set(value);
    });

    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.onMounted(function () {
      activeValues.value = vMenu.select(activeValue.value);
    });
    return {
      styles: styles,
      menuClass: menuClass,
      innerClasses: innerClasses,
      activeValue: activeValue,
      activeValues: activeValues,
      expandValues: expandValues,
      classPrefix: classPrefix
    };
  },
  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_renderNode.renderContent(this, "operations", "options");
    var logo = utils_renderNode.renderLNodeJSX(this, "logo");
    return h("div", {
      "class": this.menuClass,
      "style": this.styles
    }, [h("div", {
      "class": "".concat(this.classPrefix, "-default-menu__inner")
    }, [logo && h("div", {
      "class": "".concat(this.classPrefix, "-menu__logo")
    }, [logo]), h("ul", {
      "class": this.innerClasses
    }, [utils_renderNode.renderContent(this, "default", "content")]), operations && h("div", {
      "class": "".concat(this.classPrefix, "-menu__operations")
    }, [operations])])]);
  }
});

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