/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, computed, watchEffect, provide, watch, onMounted } from '@vue/composition-api';
import { prefix } from '../config.js';
import props from './props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import VMenu from './v-menu.js';
import '@babel/runtime/helpers/typeof';
import 'vue';
import 'lodash/isObject';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

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(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 = defineComponent({
  name: "TMenu",
  props: _objectSpread({}, props),
  model: {
    prop: "value",
    event: "change"
  },
  setup: function setup(props2, ctx) {
    var mode = ref(props2.expandType);
    var theme = computed(function () {
      return props2.theme;
    });
    var isMutex = computed(function () {
      return props2.expandMutex;
    });
    var menuClass = computed(function () {
      return ["".concat(prefix, "-default-menu"), "".concat(prefix, "-menu--").concat(props2.theme), _defineProperty({}, "".concat(prefix, "-is-collapsed"), props2.collapsed)];
    });
    var innerClasses = computed(function () {
      return ["".concat(prefix, "-menu"), _defineProperty({}, "".concat(prefix, "-menu--scroll"), mode.value !== "popup"), "narrow-scrollbar"];
    });
    var styles = computed(function () {
      var width = props2.width;
      var expandWidth = typeof width === "number" ? "".concat(width, "px") : width;
      return {
        height: "100%",
        width: props2.collapsed ? "64px" : expandWidth
      };
    });
    var activeValue = ref(props2.defaultValue || props2.value);
    var activeValues = ref([]);
    var expandValues = 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");
    watchEffect(function () {
      mode.value = props2.collapsed ? "popup" : props2.expandType;
    });
    var vMenu = new VMenu({
      isMutex: isMutex.value,
      expandValues: expandValues.value
    });
    provide("TdMenu", {
      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(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);
      }
    });
    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);
    };

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

export { _Menu as default };
//# sourceMappingURL=menu.js.map
