"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "MenuDataItem", {
  enumerable: true,
  get: function get() {
    return _typings.MenuDataItem;
  }
});
Object.defineProperty(exports, "IMenu", {
  enumerable: true,
  get: function get() {
    return _typings.IMenu;
  }
});
exports.default = exports.DefaultAppMenuProps = exports.EVENT_UPDATE_SELECTED_KEYS = exports.EVENT_UPDATE_OPEN_KEYS = void 0;

var _vue = require("vue");

var _settings = require("../settings");

var _proLayout = require("@antd-vue/pro-layout");

var _antDesignVue = require("ant-design-vue");

var _utils = require("../utils");

var _typings = require("./typings");

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 EVENT_UPDATE_OPEN_KEYS = 'update:openKeys';
exports.EVENT_UPDATE_OPEN_KEYS = EVENT_UPDATE_OPEN_KEYS;
var EVENT_UPDATE_SELECTED_KEYS = 'update:selectedKeys';
exports.EVENT_UPDATE_SELECTED_KEYS = EVENT_UPDATE_SELECTED_KEYS;

var DefaultAppMenuProps = _objectSpread(_objectSpread({}, _settings.DefaultPureSettings), {}, {
  menus: _utils.PropTypes.array.def([]),
  indent: _utils.PropTypes.number.def(16),
  mode: _utils.PropTypes.string.def('inline'),
  theme: _utils.PropTypes.string.def('dark'),
  collapsed: _utils.PropTypes.bool.def(false),
  // collapse: PropTypes.func,
  openKeys: _utils.PropTypes.array.def([]),
  selectedKeys: _utils.PropTypes.array.def([]),
  i18n: {
    type: Function
  },
  menuTitleRender: {
    type: Function
  },
  menuItemRender: {
    type: [Function, Boolean]
  },
  subMenuItemRender: {
    type: [Function, Boolean]
  },
  menuRender: {
    type: [Function, Boolean]
  }
});

exports.DefaultAppMenuProps = DefaultAppMenuProps;
var AppMenu = (0, _vue.defineComponent)({
  name: 'AppMenu',
  emits: ['click', EVENT_UPDATE_OPEN_KEYS, EVENT_UPDATE_SELECTED_KEYS],
  props: DefaultAppMenuProps,
  setup: function setup(props, _ref) {
    var slots = _ref.slots,
        attrs = _ref.attrs,
        emit = _ref.emit;
    var isInline = (0, _vue.computed)(function () {
      return props.mode === 'inline';
    });
    /* 组件内部状态 */

    var state = (0, _vue.reactive)({
      openKeys: props.openKeys,
      selectedKeys: props.selectedKeys
    });
    (0, _vue.watch)(props.openKeys, function (val, old) {
      if (val && val != old) state.openKeys = val;
    });
    (0, _vue.watch)(props.selectedKeys, function (val, old) {
      if (val && val != old) state.selectedKeys = val;
    });
    /**
     * SubMenu 展开/关闭的回调
     * @param keys
     */

    var handleOpenChange = function handleOpenChange(keys) {
      state.openKeys = keys;
      emit(EVENT_UPDATE_OPEN_KEYS, keys);
    };
    /**
     * 被选中时调用
     * @param info
     */


    var handleSelect = function handleSelect(info) {
      state.selectedKeys = info.selectedKeys;
      emit(EVENT_UPDATE_SELECTED_KEYS, info.selectedKeys);
    };
    /**
     * 菜单默认点击事件处理
     * @param id
     */


    var handleMenuClick = function handleMenuClick(menu) {
      emit('click', menu);
    };

    return function () {
      var indent = props.indent,
          collapsed = props.collapsed,
          mode = props.mode,
          theme = props.theme,
          menus = props.menus,
          menuProps = props.menuProps;
      /**
       * 名称渲染
       * @param menu
       */

      var titleRender = function titleRender(menu) {
        var i18n = props.i18n;

        if ((0, _vue.isVNode)(menu.name)) {
          return menu.name;
        } else if (typeof menu.name === 'string') {
          var dom = (0, _vue.createVNode)("span", null, [i18n && menu.name && i18n(menu.name) || menu.name]);
          return (0, _utils.getRenderComponent)('menuTitleRender', props, slots, dom, {
            propsArgs: [menu, dom, props],
            slotArg: {
              menu: menu,
              props: props
            }
          });
        }
      };
      /**
       * 菜单分隔符
       * @param divider
       * @param element
       */


      var menuDivider = function menuDivider(divider, element) {
        var jsx = [];
        jsx.push(element);

        switch (divider) {
          case 'before':
            jsx.unshift((0, _vue.createVNode)(_antDesignVue.Menu.Divider, null, null));
            break;

          case 'after':
            jsx.push((0, _vue.createVNode)(_antDesignVue.Menu.Divider, null, null));
            break;

          case 'all':
            jsx.unshift((0, _vue.createVNode)(_antDesignVue.Menu.Divider, null, null));
            jsx.push((0, _vue.createVNode)(_antDesignVue.Menu.Divider, null, null));
            break;

          case false:
        }

        return jsx.map(function (jsxElement) {
          return jsxElement;
        });
      };
      /**
       * 无子菜单的菜单项渲染
       * @param menu
       */


      var menuItemRender = function menuItemRender(menu) {
        var id = menu.id,
            name = menu.name,
            icon = menu.icon,
            href = menu.href,
            external = menu.external,
            target = menu.target,
            divider = menu.divider,
            registeredRoute = menu.registeredRoute,
            onClick = menu.onClick;
        var menuItemRenderFunc = props['menuItemRender'];
        var menuItemRenderSlot = slots['menuItemRender'];
        var dom = (0, _vue.createVNode)(_proLayout.AppLink, {
          "to": href,
          "customClickEvent": !!onClick,
          "external": external,
          "target": target,
          "registered": registeredRoute
        }, {
          default: function _default() {
            return [(0, _vue.createVNode)(_proLayout.AppIcon, {
              "icon": icon
            }, null), titleRender(menu)];
          }
        });
        var render = menuItemRenderFunc && menuItemRenderFunc(menu, dom, props) || menuItemRenderSlot && menuItemRenderSlot(menu, props);
        return menuDivider(divider, (0, _vue.createVNode)(_antDesignVue.Menu.Item, {
          "key": id,
          "title": name,
          "onClick": function onClick() {
            handleMenuClick(menu);
          }
        }, {
          default: function _default() {
            return [render ? render : dom];
          }
        }));
      };
      /**
       * 拥有子菜单的菜单项渲染
       * @param menu
       */


      var subMenuItemRender = function subMenuItemRender(menu) {
        var id = menu.id,
            name = menu.name,
            icon = menu.icon,
            hidden = menu.hidden,
            children = menu.children,
            divider = menu.divider,
            onClick = menu.onClick;
        var hiddenChildren = hidden && hidden === 'children';
        var subMenuItemRenderFunc = props['subMenuItemRender'];
        var subMenuItemRenderSlot = slots['subMenuItemRender'];
        var dom = (0, _vue.createVNode)("span", null, [(0, _vue.createVNode)(_proLayout.AppIcon, {
          "icon": icon
        }, null), (0, _vue.createVNode)("span", null, [titleRender(menu)])]);
        var render = subMenuItemRenderFunc && subMenuItemRenderFunc(menu, dom, props) || subMenuItemRenderSlot && subMenuItemRenderSlot(menu, props);
        var renderMenuContent = render ? render : dom;
        return menuDivider(divider, (0, _vue.createVNode)(_antDesignVue.Menu.SubMenu, {
          "key": id,
          "title": renderMenuContent,
          "onTitleClick": function onTitleClick() {
            handleMenuClick(menu);
          }
        }, {
          default: function _default() {
            return [!hiddenChildren && (children === null || children === void 0 ? void 0 : children.map(function (menu) {
              return menuRender(menu);
            }))];
          }
        }));
      };
      /**
       * 菜单渲染
       * @param menu
       */


      var menuRender = function menuRender(menu) {
        if (menu) {
          var menuRenderFunc = props['menuRender'];
          var menuRenderSlot = slots['menuRender'];
          var render = menuRenderFunc && menuRenderFunc(menu, props) || menuRenderSlot && menuRenderSlot(menu, props);
          var hidden = menu.hidden,
              children = menu.children;

          if (render) {
            return render;
          } else if (!hidden) {
            return children ? subMenuItemRender(menu) : menuItemRender(menu);
          } else if (hidden === 'children') {
            return menuItemRender(menu);
          } else if (hidden === 'self') {
            var _menu$children;

            return (_menu$children = menu.children) === null || _menu$children === void 0 ? void 0 : _menu$children.map(function (childMenu) {
              return menuRender(childMenu);
            });
          }
        }

        return null;
      };

      return (0, _vue.createVNode)(_antDesignVue.Menu, _objectSpread({
        "key": "Menu",
        "inlineCollapsed": isInline.value ? collapsed : undefined,
        "inlineIndent": indent,
        "mode": mode,
        "theme": theme,
        "openKeys": state.openKeys,
        "selectedKeys": state.selectedKeys,
        "onOpenChange": handleOpenChange,
        "onSelect": handleSelect
      }, menuProps), {
        default: function _default() {
          return [menus && menus.map(function (m) {
            return menuRender(m);
          })];
        }
      });
    };
  }
});

var _default2 = (0, _utils.withInstall)(AppMenu);

exports.default = _default2;