var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  BaseMenu: () => BaseMenu,
  baseMenuProps: () => baseMenuProps
});
module.exports = __toCommonJS(stdin_exports);
var import_vue = require("vue");
var import_icons_vue = require("@ant-design/icons-vue");
var import_pro_provider = require("@antdv/pro-provider");
var import_pro_utils = require("@antdv/pro-utils");
var import_ant_design_vue = require("ant-design-vue");
var import_Menu = require("ant-design-vue/es/menu/src/Menu");
var import_vue2 = require("vue");
var import_defaultSettings = require("../../defaultSettings");
var import_utils = require("../../utils/utils");
var import_props = require("../GlobalHeader/props");
var import_SiderMenu = require("./SiderMenu");
var import_menu = require("./style/menu");
const MenuItemTooltip = (0, import_vue2.defineComponent)({
  name: "MenuItemTooltip",
  props: {
    collapsed: (0, import_pro_utils.boolType)(),
    title: (0, import_pro_utils.vNodeType)(),
    disable: (0, import_pro_utils.boolType)().def(void 0)
  },
  slots: Object,
  setup(props, {
    slots
  }) {
    const [collapsed, setCollapsed] = (0, import_pro_utils.useState)(props.collapsed);
    const [open, setOpen] = (0, import_pro_utils.useState)(false);
    (0, import_vue2.watch)(() => props.collapsed, () => {
      setOpen(false);
      setTimeout(() => {
        setCollapsed(props.collapsed);
      }, 400);
    }, {
      immediate: true
    });
    return () => {
      var _a;
      const children = (_a = slots.default) == null ? void 0 : _a.call(slots);
      if (props.disable) {
        return children;
      }
      return (0, import_vue.createVNode)(import_ant_design_vue.Tooltip, {
        "title": props.title,
        "open": collapsed.value && props.collapsed ? open.value : false,
        "placement": "right",
        "onOpenChange": setOpen
      }, {
        default: () => [children]
      });
    };
  }
});
const baseMenuProps = __spreadValues(__spreadValues({
  className: String,
  /** 默认的是否展开，会受到 breakpoint 的影响 */
  defaultCollapsed: (0, import_pro_utils.boolType)(),
  collapsed: (0, import_pro_utils.boolType)().def(void 0),
  isMobile: (0, import_pro_utils.boolType)().def(void 0),
  menuData: (0, import_pro_utils.arrayType)(),
  mode: (0, import_pro_utils.oneOf)(["vertical", "horizontal", "inline"]),
  onCollapse: (0, import_pro_utils.funcType)(),
  openKeys: (0, import_pro_utils.oneOfType)([Array, Boolean]).def(void 0),
  handleOpenChange: (0, import_pro_utils.funcType)(),
  iconPrefixes: String,
  /** 要给菜单的props */
  menuProps: (0, import_pro_utils.objectType)(),
  style: (0, import_pro_utils.objectType)(),
  formatMessage: (0, import_pro_utils.funcType)(),
  /**
   * @name 处理父级菜单的 props，可以覆写菜单的点击功能，一般用于埋点
   * @see 子级的菜单要使用 menuItemRender 来处理
   *
   * @example 使用 a 标签跳转到特殊的地址 subMenuItemRender={(item, defaultDom) => { return <a onClick={()=> history.push(item.path) }>{defaultDom}</a> }}
   * @example 增加埋点 subMenuItemRender={(item, defaultDom) => { return <a onClick={()=> log.click(item.name) }>{defaultDom}</a> }}
   */
  subMenuItemRender: (0, import_pro_utils.oneOfType)([Function, Boolean]).def(void 0),
  /**
   * @name 处理菜单的 props，可以覆写菜单的点击功能，一般结合 Router 框架使用
   * @see 非子级的菜单要使用 subMenuItemRender 来处理
   *
   * @example 使用 a 标签 menuItemRender={(item, defaultDom) => { return <a onClick={()=> history.push(item.path) }>{defaultDom}</a> }}
   * @example 使用 Link 标签 menuItemRender={(item, defaultDom) => { return <Link to={item.path}>{defaultDom}</Link> }}
   */
  menuItemRender: (0, import_pro_utils.oneOfType)([Function, Boolean]).def(void 0),
  /**
   * @name 处理 menuData 的方法，与 menuDataRender 不同，postMenuData处理完成后会直接渲染，不再进行国际化和拼接处理
   *
   * @example 增加菜单图标 postMenuData={(menuData) => { return menuData.map(item => { return { ...item, icon: <Icon type={item.icon} /> } }) }}
   */
  postMenuData: (0, import_pro_utils.funcType)(),
  computedMatch: (0, import_pro_utils.objectType)(),
  route: (0, import_pro_utils.objectType)(),
  location: (0, import_pro_utils.objectType)()
}, (0, import_pro_utils.omit)((0, import_Menu.menuProps)(), ["openKeys", "onOpenChange", "mode"])), (0, import_props.pureSettingProps)());
let IconFont = (0, import_icons_vue.createFromIconfontCN)({
  scriptUrl: import_defaultSettings.defaultSettings.iconfontUrl
});
function getIcon(icon, className, iconPrefixes = "icon-") {
  if (typeof icon === "string" && icon !== "") {
    if ((0, import_pro_utils.isUrl)(icon) || (0, import_pro_utils.isImg)(icon)) {
      return (0, import_vue.createVNode)("img", {
        "width": 16,
        "key": icon,
        "src": icon,
        "alt": "icon",
        "class": className
      }, null);
    }
    if (icon.startsWith(iconPrefixes)) {
      return (0, import_vue.createVNode)(IconFont, {
        "type": icon
      }, null);
    }
  }
  return icon;
}
function getMenuTitleSymbol(_title) {
  if (_title && typeof _title === "string") {
    const symbol = _title.substring(0, 1).toUpperCase();
    return symbol;
  }
  return null;
}
function useMenu(props) {
  const location = (0, import_vue2.computed)(() => props.location || {
    pathname: "/"
  });
  const conversionPath = (path) => {
    if (path && path.indexOf("http") === 0) {
      return path;
    }
    return `/${path || ""}`.replace(/\/+/g, "/");
  };
  const getIntlName = (item) => {
    var _a, _b;
    const {
      name,
      locale
    } = item;
    let finalName = name;
    if (locale && ((_a = props.menu) == null ? void 0 : _a.locale) !== false) {
      finalName = (_b = props.formatMessage) == null ? void 0 : _b.call(props, {
        id: locale,
        defaultMessage: name
      });
    }
    if (props.menuTextRender) {
      return props.menuTextRender(item, (0, import_vue.createVNode)(import_vue.Fragment, null, [finalName]), props);
    }
    return finalName;
  };
  const getMenuItemPath = (item, level, noGroupLevel) => {
    var _a, _b, _c;
    const itemPath = conversionPath(item.path || "/");
    const menuItemTitle = getIntlName(item);
    const isGroup = ((_a = props.menu) == null ? void 0 : _a.type) === "group";
    const hasIcon = level === 0 || isGroup && level === 1;
    const icon = !hasIcon ? null : getIcon(item.icon, `${props.baseClassName}-icon ${props == null ? void 0 : props.hashId}`, props.iconPrefixes);
    const defaultIcon = props.collapsed && hasIcon ? getMenuTitleSymbol(menuItemTitle) : null;
    let defaultItem = (0, import_vue.createVNode)("div", {
      "key": itemPath,
      "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-title`, props == null ? void 0 : props.hashId, {
        [`${props.baseClassName}-item-title-collapsed`]: props.collapsed,
        [`${props.baseClassName}-item-title-collapsed-level-${noGroupLevel}`]: props.collapsed,
        [`${props.baseClassName}-item-collapsed-show-title`]: ((_b = props.menu) == null ? void 0 : _b.collapsedShowTitle) && props.collapsed
      })
    }, [(0, import_vue.createVNode)("span", {
      "class": `${props.baseClassName}-item-icon ${props == null ? void 0 : props.hashId}`.trim(),
      "style": {
        display: defaultIcon === null && !icon ? "none" : ""
      }
    }, [icon || (0, import_vue.createVNode)("span", {
      "class": "anticon"
    }, [defaultIcon])]), (0, import_vue.createVNode)("span", {
      "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-text`, props == null ? void 0 : props.hashId, {
        [`${props.baseClassName}-item-text-has-icon`]: hasIcon && (icon || defaultIcon)
      })
    }, [menuItemTitle])]);
    const isHttpUrl = (0, import_pro_utils.isUrl)(itemPath);
    if (isHttpUrl) {
      defaultItem = (0, import_vue.createVNode)("span", {
        "key": itemPath,
        "onClick": () => {
          var _a2;
          (_a2 = window == null ? void 0 : window.open) == null ? void 0 : _a2.call(window, itemPath, "_blank");
        },
        "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-title`, props == null ? void 0 : props.hashId, {
          [`${props.baseClassName}-item-title-collapsed`]: props.collapsed,
          [`${props.baseClassName}-item-title-collapsed-level-${noGroupLevel}`]: props.collapsed,
          [`${props.baseClassName}-item-link`]: true,
          [`${props.baseClassName}-item-collapsed-show-title`]: ((_c = props.menu) == null ? void 0 : _c.collapsedShowTitle) && props.collapsed
        })
      }, [(0, import_vue.createVNode)("span", {
        "class": `${props.baseClassName}-item-icon ${props == null ? void 0 : props.hashId}`.trim(),
        "style": {
          display: defaultIcon === null && !icon ? "none" : ""
        }
      }, [icon || (0, import_vue.createVNode)("span", {
        "class": "anticon"
      }, [defaultIcon])]), (0, import_vue.createVNode)("span", {
        "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-text`, props == null ? void 0 : props.hashId, {
          [`${props.baseClassName}-item-text-has-icon`]: hasIcon && (icon || defaultIcon)
        })
      }, [menuItemTitle])]);
    }
    if (props.menuItemRender) {
      const renderItemProps = __spreadProps(__spreadValues({}, item), {
        isUrl: isHttpUrl,
        itemPath,
        isMobile: props.isMobile,
        replace: itemPath === location.value.pathname,
        onClick: () => props.onCollapse && props.onCollapse(true)
      });
      return level === 0 ? (0, import_vue.createVNode)(MenuItemTooltip, {
        "collapsed": props.collapsed,
        "title": menuItemTitle,
        "disable": item.disabledTooltip
      }, {
        default: () => [props.menuItemRender(renderItemProps, defaultItem, props)]
      }) : props.menuItemRender(renderItemProps, defaultItem, props);
    }
    return level === 0 ? (0, import_vue.createVNode)(MenuItemTooltip, {
      "collapsed": props.collapsed,
      "title": menuItemTitle,
      "disable": item.disabledTooltip
    }, {
      default: () => [defaultItem]
    }) : defaultItem;
  };
  const getNavMenuItems = (menusData = [], level, noGroupLevel) => menusData.map((item) => getSubMenuOrItem(item, level, noGroupLevel)).filter((item) => item).flat(1);
  const getSubMenuOrItem = (item, level, noGroupLevel) => {
    var _a, _b, _c, _d, _e;
    const isGroup = ((_a = props.menu) == null ? void 0 : _a.type) === "group" && props.layout !== "top";
    const designToken = props.token;
    const name = getIntlName(item);
    const children = (item == null ? void 0 : item.children) || (item == null ? void 0 : item.routes);
    const menuType = isGroup && level === 0 ? "group" : void 0;
    if (Array.isArray(children) && children.length > 0) {
      const shouldHasIcon = level === 0 || isGroup && level === 1;
      const iconDom = getIcon(item.icon, `${props.baseClassName}-icon ${props == null ? void 0 : props.hashId}`, props.iconPrefixes);
      const defaultIcon = props.collapsed && shouldHasIcon ? getMenuTitleSymbol(name) : null;
      const defaultTitle = (0, import_vue.createVNode)("div", {
        "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-title`, props == null ? void 0 : props.hashId, {
          [`${props.baseClassName}-item-title-collapsed`]: props.collapsed,
          [`${props.baseClassName}-item-title-collapsed-level-${noGroupLevel}`]: props.collapsed,
          [`${props.baseClassName}-group-item-title`]: menuType === "group",
          [`${props.baseClassName}-item-collapsed-show-title`]: ((_b = props.menu) == null ? void 0 : _b.collapsedShowTitle) && props.collapsed
        })
      }, [menuType === "group" && props.collapsed ? null : shouldHasIcon && iconDom ? (0, import_vue.createVNode)("span", {
        "class": `${props.baseClassName}-item-icon ${props == null ? void 0 : props.hashId}`.trim()
      }, [iconDom]) : defaultIcon, (0, import_vue.createVNode)("span", {
        "class": (0, import_pro_utils.classNames)(`${props.baseClassName}-item-text`, props == null ? void 0 : props.hashId, {
          [`${props.baseClassName}-item-text-has-icon`]: menuType !== "group" && shouldHasIcon && (iconDom || defaultIcon)
        })
      }, [name])]);
      const _title = props.subMenuItemRender ? props.subMenuItemRender(__spreadProps(__spreadValues({}, item), {
        isUrl: false
      }), defaultTitle, props) : defaultTitle;
      if (isGroup && level === 0 && props.collapsed && !((_c = props.menu) == null ? void 0 : _c.collapsedShowGroupTitle)) {
        return getNavMenuItems(children, level + 1, level);
      }
      const childrenList = getNavMenuItems(children, level + 1, isGroup && level === 0 && props.collapsed ? level : level + 1);
      return [{
        type: menuType,
        key: item.key || item.path,
        label: _title,
        onClick: isGroup ? void 0 : item.onTitleClick,
        children: childrenList,
        class: (0, import_pro_utils.classNames)({
          [`${props.baseClassName}-group`]: menuType === "group",
          [`${props.baseClassName}-submenu`]: menuType !== "group",
          [`${props.baseClassName}-submenu-has-icon`]: menuType !== "group" && shouldHasIcon && iconDom
        })
      }, isGroup && level === 0 ? {
        type: "divider",
        prefixCls: props.prefixCls,
        className: `${props.baseClassName}-divider`,
        key: `${item.key || item.path}-group-divider`,
        style: {
          padding: 0,
          borderBlockEnd: 0,
          margin: props.collapsed ? "4px" : "6px 16px",
          marginBlockStart: `${props.collapsed ? 4 : 8}px`,
          borderColor: (_e = (_d = designToken == null ? void 0 : designToken.layout) == null ? void 0 : _d.sider) == null ? void 0 : _e.colorMenuItemDivider
        }
      } : void 0].filter(Boolean);
    }
    return {
      class: `${props.baseClassName}-menu-item`,
      disabled: item.disabled,
      key: item.key || item.path,
      onClick: item.onTitleClick,
      label: getMenuItemPath(item, level, noGroupLevel)
    };
  };
  return {
    getNavMenuItems
  };
}
const BaseMenu = (0, import_vue2.defineComponent)({
  name: "BaseMenu",
  inheritAttrs: false,
  props: __spreadValues(__spreadValues({}, baseMenuProps), (0, import_SiderMenu.privateSiderMenuProps)()),
  emits: {
    "update:openKeys": (val) => true
  },
  setup(props, {
    emit
  }) {
    var _a, _b;
    const {
      token,
      dark
    } = import_pro_provider.ProProvider.useContext();
    const baseClassName = (0, import_vue2.computed)(() => `${props.prefixCls}-base-menu-${props.mode}`);
    const {
      wrapSSR,
      hashId
    } = (0, import_menu.useStyle)((0, import_vue2.computed)(() => `${props.prefixCls}-base-menu`));
    const defaultOpenAll = (0, import_vue2.ref)((_a = props.menu) == null ? void 0 : _a.defaultOpenAll);
    const defaultOpenKeys = (0, import_vue2.computed)(() => {
      var _a2;
      if ((_a2 = props.menu) == null ? void 0 : _a2.defaultOpenAll) {
        return (0, import_utils.getOpenKeysFromMenuData)(props.menuData) || [];
      }
      return [];
    });
    const openKeys = (0, import_vue2.shallowRef)(props.openKeys === false ? [] : (_b = props.openKeys) != null ? _b : defaultOpenKeys.value);
    (0, import_vue2.watchEffect)(() => {
      if (props.openKeys !== false) openKeys.value = props.openKeys;
    });
    (0, import_vue2.watch)(() => openKeys.value, (val) => {
      var _a2;
      emit("update:openKeys", val);
      (_a2 = props.handleOpenChange) == null ? void 0 : _a2.call(props, val);
    });
    const [selectedKeys, setSelectedKeys] = (0, import_pro_utils.useMountMergeState)([], {
      value: (0, import_vue2.ref)(props.selectedKeys),
      onChange: props.onSelect ? (keys) => {
        if (props.onSelect && keys) {
          props.onSelect(keys);
        }
      } : void 0
    });
    (0, import_pro_utils.useEffect)(() => {
      var _a2;
      if (((_a2 = props.menu) == null ? void 0 : _a2.defaultOpenAll) || props.openKeys === false) {
        return;
      }
      if (props.matchMenuKeys) {
        openKeys.value = props.matchMenuKeys;
        setSelectedKeys(props.matchMenuKeys);
      }
    }, (0, import_vue2.computed)(() => [props.matchMenuKeys.join("-")]));
    (0, import_vue2.watch)(() => props.matchMenuKeys.join("-"), () => {
      var _a2, _b2;
      if (props.matchMenuKeys.join("-") !== (selectedKeys.value || []).join("-")) {
        setSelectedKeys(props.matchMenuKeys);
      }
      if (!defaultOpenAll.value && props.openKeys !== false && props.matchMenuKeys.join("-") !== (openKeys.value || []).join("-")) {
        let newKeys = props.matchMenuKeys;
        if (((_a2 = props.menu) == null ? void 0 : _a2.autoClose) === false) {
          newKeys = Array.from(/* @__PURE__ */ new Set([...props.matchMenuKeys, ...openKeys.value || []]));
        }
        openKeys.value = newKeys;
      } else if (((_b2 = props.menu) == null ? void 0 : _b2.ignoreFlatMenu) && defaultOpenAll.value) {
        openKeys.value = (0, import_utils.getOpenKeysFromMenuData)(props.menuData);
      } else {
        defaultOpenAll.value = false;
      }
    });
    (0, import_vue2.watchEffect)(() => {
      if (props.iconfontUrl) {
        IconFont = (0, import_icons_vue.createFromIconfontCN)({
          scriptUrl: props.iconfontUrl
        });
      }
    });
    const openKeysProps = (0, import_vue2.computed)(() => getOpenKeysProps(openKeys.value, props));
    return () => {
      const {
        mode,
        className,
        style,
        menuData,
        menu
      } = props;
      if (menu == null ? void 0 : menu.loading) {
        return (0, import_vue.createVNode)("div", {
          "style": (mode == null ? void 0 : mode.includes("inline")) ? {
            padding: `24px`
          } : {
            marginBlockStart: `16px`
          }
        }, [(0, import_vue.createVNode)(import_ant_design_vue.Skeleton, {
          "active": true,
          "title": false,
          "paragraph": {
            rows: (mode == null ? void 0 : mode.includes("inline")) ? 6 : 1
          }
        }, null)]);
      }
      const {
        getNavMenuItems
      } = useMenu(__spreadProps(__spreadValues({}, props), {
        token,
        menuRenderType: props.menuRenderType,
        baseClassName: baseClassName.value,
        hashId: hashId.value
      }));
      const finallyData = props.postMenuData ? props.postMenuData(menuData) : menuData;
      if (finallyData && (finallyData == null ? void 0 : finallyData.length) < 1) {
        return null;
      }
      return wrapSSR((0, import_vue.createVNode)(import_ant_design_vue.Menu, (0, import_vue.mergeProps)(openKeysProps.value, {
        "key": "Menu",
        "mode": mode,
        "inlineIndent": 16,
        "openKeys": openKeys.value,
        "theme": dark ? "dark" : "light",
        "selectedKeys": selectedKeys.value,
        "style": __spreadValues({
          backgroundColor: "transparent",
          border: "none"
        }, style),
        "class": (0, import_pro_utils.classNames)(className, hashId.value, baseClassName.value, {
          [`${baseClassName.value}-horizontal`]: mode === "horizontal",
          [`${baseClassName.value}-collapsed`]: props.collapsed
        }),
        "items": getNavMenuItems(finallyData, 0, 0),
        "onOpenChange": (_openKeys) => {
          if (!props.collapsed) {
            openKeys.value = _openKeys;
          }
        }
      }, props.menuProps), null));
    };
  }
});
function getOpenKeysProps(openKeys, {
  layout,
  collapsed
}) {
  let openKeysProps = {};
  if (openKeys && !collapsed && ["side", "mix"].includes(layout || "mix")) {
    openKeysProps = {
      openKeys
    };
  }
  return openKeysProps;
}
