"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _store = require("../_util/store");

var _SubPopupMenu = _interopRequireWildcard(require("./SubPopupMenu"));

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _propsUtil = _interopRequireWildcard(require("../_util/props-util"));

var _commonPropsType = _interopRequireDefault(require("./commonPropsType"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

var Menu = {
  name: 'Menu',
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, _commonPropsType.default), {}, {
    selectable: _vueTypes.default.bool.def(true)
  }),
  mixins: [_BaseMixin.default],
  data: function data() {
    var props = (0, _propsUtil.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 = (0, _store.create)({
      selectedKeys: selectedKeys,
      openKeys: openKeys,
      activeKey: {
        '0-menu-': (0, _SubPopupMenu.getActiveKey)(_objectSpread(_objectSpread({}, props), {}, {
          children: props.children || []
        }), props.activeKey)
      }
    }); // this.isRootMenu = true // 声明在props上

    return {};
  },
  created: function created() {
    (0, _vue.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 (!(0, _propsUtil.default)(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 (!(0, _propsUtil.default)(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 (!(0, _propsUtil.default)(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 = (0, _propsUtil.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({}, (0, _propsUtil.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: (0, _propsUtil.getComponent)(this, 'itemIcon', props),
      expandIcon: (0, _propsUtil.getComponent)(this, 'expandIcon', props),
      overflowedIndicator: (0, _propsUtil.getComponent)(this, 'overflowedIndicator', props) || _vue.createVNode("span", null, [_vue.createTextVNode("\xB7\xB7\xB7")]),
      openTransitionName: this.getOpenTransitionName(),
      children: (0, _propsUtil.filterEmpty)(props.children),
      onClick: this.onClick,
      onOpenChange: this.onOpenChange,
      onDeselect: this.onDeselect,
      onSelect: this.onSelect,
      ref: this.saveInnerMenu
    });

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

    return _vue.createVNode(_store.Provider, {
      "store": this.store
    }, {
      default: function _default() {
        return [subPopupMenu];
      }
    });
  }
};
var _default2 = Menu;
exports.default = _default2;