import * as _vue from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import PropTypes from '../_util/vue-types';
import { Provider, create } from '../_util/store';
import { default as SubPopupMenu, getActiveKey } from './SubPopupMenu';
import BaseMixin from '../_util/BaseMixin';
import hasProp, { getOptionProps, getComponent, filterEmpty } from '../_util/props-util';
import commonPropsType from './commonPropsType';
import { provide } from 'vue';
var Menu = {
  name: 'Menu',
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, commonPropsType), {}, {
    selectable: PropTypes.bool.def(true)
  }),
  mixins: [BaseMixin],
  data: function data() {
    var props = getOptionProps(this);
    var selectedKeys = props.defaultSelectedKeys;
    var openKeys = props.defaultOpenKeys;

    if ('selectedKeys' in props) {
      selectedKeys = props.selectedKeys || [];
    }

    if ('openKeys' in props) {
      openKeys = props.openKeys || [];
    }

    this.store = create({
      selectedKeys: selectedKeys,
      openKeys: openKeys,
      activeKey: {
        '0-menu-': getActiveKey(_objectSpread(_objectSpread({}, props), {}, {
          children: props.children || []
        }), props.activeKey)
      }
    }); // this.isRootMenu = true // 声明在props上

    return {};
  },
  created: function created() {
    provide('parentMenu', this);
  },
  mounted: function mounted() {
    this.updateMiniStore();
  },
  updated: function updated() {
    this.updateMiniStore();
  },
  methods: {
    onSelect: function onSelect(selectInfo) {
      var props = this.$props;

      if (props.selectable) {
        // root menu
        var selectedKeys = this.store.getState().selectedKeys;
        var selectedKey = selectInfo.key;

        if (props.multiple) {
          selectedKeys = selectedKeys.concat([selectedKey]);
        } else {
          selectedKeys = [selectedKey];
        }

        if (!hasProp(this, 'selectedKeys')) {
          this.store.setState({
            selectedKeys: selectedKeys
          });
        }

        this.__emit('select', _objectSpread(_objectSpread({}, selectInfo), {}, {
          selectedKeys: selectedKeys
        }));
      }
    },
    onClick: function onClick(e) {
      this.__emit('click', e);
    },
    // onKeyDown needs to be exposed as a instance method
    // e.g., in rc-select, we need to navigate menu item while
    // current active item is rc-select input box rather than the menu itself
    onKeyDown: function onKeyDown(e, callback) {
      this.innerMenu.getWrappedInstance().onKeyDown(e, callback);
    },
    onOpenChange: function onOpenChange(event) {
      var openKeys = this.store.getState().openKeys.concat();
      var changed = false;

      var processSingle = function processSingle(e) {
        var oneChanged = false;

        if (e.open) {
          oneChanged = openKeys.indexOf(e.key) === -1;

          if (oneChanged) {
            openKeys.push(e.key);
          }
        } else {
          var index = openKeys.indexOf(e.key);
          oneChanged = index !== -1;

          if (oneChanged) {
            openKeys.splice(index, 1);
          }
        }

        changed = changed || oneChanged;
      };

      if (Array.isArray(event)) {
        // batch change call
        event.forEach(processSingle);
      } else {
        processSingle(event);
      }

      if (changed) {
        if (!hasProp(this, 'openKeys')) {
          this.store.setState({
            openKeys: openKeys
          });
        }

        this.__emit('openChange', openKeys);
      }
    },
    onDeselect: function onDeselect(selectInfo) {
      var props = this.$props;

      if (props.selectable) {
        var selectedKeys = this.store.getState().selectedKeys.concat();
        var selectedKey = selectInfo.key;
        var index = selectedKeys.indexOf(selectedKey);

        if (index !== -1) {
          selectedKeys.splice(index, 1);
        }

        if (!hasProp(this, 'selectedKeys')) {
          this.store.setState({
            selectedKeys: selectedKeys
          });
        }

        this.__emit('deselect', _objectSpread(_objectSpread({}, selectInfo), {}, {
          selectedKeys: selectedKeys
        }));
      }
    },
    getOpenTransitionName: function getOpenTransitionName() {
      var props = this.$props;
      var transitionName = props.openTransitionName;
      var animationName = props.openAnimation;

      if (!transitionName && typeof animationName === 'string') {
        transitionName = "".concat(props.prefixCls, "-open-").concat(animationName);
      }

      return transitionName;
    },
    updateMiniStore: function updateMiniStore() {
      var props = getOptionProps(this);

      if ('selectedKeys' in props) {
        this.store.setState({
          selectedKeys: props.selectedKeys || []
        });
      }

      if ('openKeys' in props) {
        this.store.setState({
          openKeys: props.openKeys || []
        });
      }
    },
    saveInnerMenu: function saveInnerMenu(ref) {
      this.innerMenu = ref;
    }
  },
  render: function render() {
    var props = _objectSpread(_objectSpread({}, getOptionProps(this)), this.$attrs);

    props.class = props.class ? "".concat(props.class, " ").concat(props.prefixCls, "-root") : "".concat(props.prefixCls, "-root");

    var subPopupMenuProps = _objectSpread(_objectSpread({}, props), {}, {
      itemIcon: getComponent(this, 'itemIcon', props),
      expandIcon: getComponent(this, 'expandIcon', props),
      overflowedIndicator: getComponent(this, 'overflowedIndicator', props) || _vue.createVNode("span", null, [_vue.createTextVNode("\xB7\xB7\xB7")]),
      openTransitionName: this.getOpenTransitionName(),
      children: filterEmpty(props.children),
      onClick: this.onClick,
      onOpenChange: this.onOpenChange,
      onDeselect: this.onDeselect,
      onSelect: this.onSelect,
      ref: this.saveInnerMenu
    });

    var subPopupMenu = _vue.createVNode(SubPopupMenu, subPopupMenuProps, null);

    return _vue.createVNode(Provider, {
      "store": this.store
    }, {
      default: function _default() {
        return [subPopupMenu];
      }
    });
  }
};
export default Menu;