/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, inject, computed, onMounted } from '@vue/composition-api';
import { prefix } from '../config.js';
import props from './menu-item-props.js';
import Ripple from '../utils/ripple.js';
import { renderTNodeJSX, renderContent } from '../utils/render-tnode.js';
import '../utils/set-style.js';
import '@babel/runtime/helpers/typeof';
import 'vue';

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 _MenuItem = defineComponent({
  name: "TMenuItem",
  props: _objectSpread({}, props),
  directives: {
    ripple: Ripple
  },
  setup: function setup(props2, ctx) {
    var menu = inject("TdMenu");
    var submenu = inject("TdSubmenu", null);
    var active = computed(function () {
      return menu.activeValue.value === props2.value;
    });
    var classes = computed(function () {
      var _ref;

      return ["".concat(prefix, "-menu__item"), (_ref = {}, _defineProperty(_ref, "".concat(prefix, "-is-active"), active.value), _defineProperty(_ref, "".concat(prefix, "-is-disabled"), props2.disabled), _defineProperty(_ref, "".concat(prefix, "-menu__item--plain"), !ctx.slots.icon), _defineProperty(_ref, "".concat(prefix, "-submenu__item"), !!submenu && !menu.isHead), _ref)];
    });

    var handleClick = function handleClick() {
      if (props2.disabled) return;
      menu.select(props2.value);
      ctx.emit("click");

      if (props2.href) {
        window.open(props2.href, props2.target);
      } else if (props2.to) {
        var router = props2.router || ctx.root.$router;
        var methods = props2.replace ? "replace" : "push";
        router[methods](props2.to)["catch"](function (err) {
          if (err.name !== "NavigationDuplicated" && !err.message.includes("Avoided redundant navigation to current location")) {
            throw err;
          }
        });
      }
    };

    onMounted(function () {
      var _menu$vMenu;

      menu === null || menu === void 0 ? void 0 : (_menu$vMenu = menu.vMenu) === null || _menu$vMenu === void 0 ? void 0 : _menu$vMenu.add({
        value: props2.value,
        parent: submenu === null || submenu === void 0 ? void 0 : submenu.value,
        vnode: ctx.slots["default"] && ctx.slots["default"]()
      });
    });
    return {
      menu: menu,
      active: active,
      classes: classes,
      handleClick: handleClick
    };
  },
  render: function render() {
    var h = arguments[0];
    return h("li", {
      "directives": [{
        name: "ripple",
        value: true
      }],
      "class": this.classes,
      "on": {
        "click": this.handleClick
      }
    }, [renderTNodeJSX(this, "icon"), h("span", {
      "class": ["".concat(prefix, "-menu__content")]
    }, [renderContent(this, "default", "content")])]);
  }
});

export { _MenuItem as default };
//# sourceMappingURL=menu-item.js.map
