import "./Item.css";
/* script */
//
//
//
//
//
//
//
//
import { createComponentName, componentMixin } from '../_utils/component';
const MenuName = createComponentName('menu');
const MenuItemName = createComponentName('menu-item');
const SubMenuName = createComponentName('sub-menu');
const PopoverName = createComponentName('popover');
const __vue_script__ = {
  name: createComponentName('menu-item'),
  mixins: [componentMixin],
  inject: ['menuBus', 'rootMenu'],
  props: {
    index: {
      type: [String, Number],
      required: true
    },
    route: {
      type: String,
      default: ''
    }
  },
  computed: {
    link() {
      return this.route || this.index;
    },

    indexPath() {
      const path = [this.link];
      let parent = this.$parent;

      while (parent.$options.name !== MenuName) {
        if (parent.$options.name === SubMenuName) {
          if (parent.link !== this.link) path.unshift(parent.link);
        }

        parent = parent.$parent;
      }

      return path;
    },

    parentMenuItem() {
      let parent = this.$parent;
      let end = false;

      while (!end) {
        if (parent.$options.name === MenuName) {
          end = true;
          return;
        }

        parent = parent.$parent;

        if (parent.$options.name === MenuItemName) {
          end = true;
        }
      }

      return parent;
    },

    active() {
      // 如果父层item和自身link一致,则不高亮
      if (this.parentMenuItem) {
        return this.parentMenuItem.link !== this.link && this.rootMenu.activeIndexPath.includes(this.link);
      }

      return this.rootMenu.activeIndexPath.includes(this.link);
    },

    subMenu() {
      let parent = this.$parent;

      while (parent.$options.name !== SubMenuName) {
        if (parent.$options.name === MenuName) return;
        if (parent === this.$root) throw new Error('MenuItem 必须包含在 Menu组件中');
        parent = parent.$parent;
      }

      return parent;
    }

  },
  methods: {
    isVueRouter() {
      if (this.rootMenu.router && this.$router) {
        if (/^http/.test(this.link)) return false;
        return true;
      }

      return false;
    },

    selectItem() {
      let parent = this.$parent;
      this.menuBus.emit('menu-item:select', this.index, this.indexPath);

      if (parent.$options.name === PopoverName) {
        return;
      }

      if (this.rootMenu.trigger === 'click') {
        while (parent.$options.name !== SubMenuName) {
          if (parent.$options.name === MenuName) return;
          if (parent === this.$root) throw new Error('MenuItem 必须包含在 Menu组件中');
          parent = parent.$parent;
        }

        parent.close();
      }
    }

  }
};
/* template */

var __vue_render__ = function () {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("li", {
    class: _vm.bem({
      active: _vm.active
    })
  }, [_vm.rootMenu.router && !_vm.isVueRouter() ? _c("a", {
    attrs: {
      href: _vm.link
    },
    on: {
      click: _vm.selectItem
    }
  }, [_vm._t("default")], 2) : _vm.rootMenu.router && _vm.isVueRouter() ? _c("router-link", {
    attrs: {
      to: _vm.link
    },
    on: {
      click: _vm.selectItem
    }
  }, [_vm._t("default")], 2) : _c("span", {
    on: {
      click: _vm.selectItem
    }
  }, [_vm._t("default")], 2)], 1);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../_utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);