var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
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 __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import pathToRegexp from "path-to-regexp";
import sha265 from "./sha265.mjs";
const childrenPropsName = "routes";
function stripQueryStringAndHashFromPath(url) {
  return url.split("?")[0].split("#")[0];
}
function isUrl(path) {
  if (!path.startsWith("http")) {
    return false;
  }
  try {
    const url = new URL(path);
    return !!url;
  } catch (error) {
    return false;
  }
}
function getKeyByPath(item) {
  const { path } = item;
  if (!path || path === "/") {
    try {
      return `/${sha265(JSON.stringify(item))}`;
    } catch (error) {
    }
  }
  return path ? stripQueryStringAndHashFromPath(path) : path;
}
function getItemLocaleName(item, parentName) {
  const { name, locale } = item;
  if ("locale" in item && locale === false || !name) {
    return false;
  }
  return item.locale || `${parentName}.${name}`;
}
function mergePath(path = "", parentPath = "/") {
  if (path.endsWith("/*")) {
    return path.replace("/*", "/");
  }
  if ((path || parentPath).startsWith("/")) {
    return path;
  }
  if (isUrl(path)) {
    return path;
  }
  return `/${parentPath}/${path}`.replace(/\/\//g, "/").replace(/\/\//g, "/");
}
function bigfishCompatibleConversions(route, props) {
  const { menu = {}, indexRoute, path = "" } = route;
  const routerChildren = route.children || [];
  const {
    name = route.name,
    icon = route.icon,
    hideChildren = route.hideChildren,
    flatMenu = route.flatMenu
  } = menu;
  const childrenList = indexRoute && Object.keys(indexRoute).join(",") !== "redirect" ? [
    __spreadValues({
      path,
      menu
    }, indexRoute)
  ].concat(routerChildren || []) : routerChildren;
  const result = __spreadValues({}, route);
  if (name) {
    result.name = name;
  }
  if (icon) {
    result.icon = icon;
  }
  if (childrenList && childrenList.length) {
    if (hideChildren) {
      delete result.children;
      return result;
    }
    const finalChildren = formatter(
      __spreadProps(__spreadValues({}, props), {
        data: childrenList
      }),
      route
    );
    if (flatMenu) {
      return finalChildren;
    }
    delete result[childrenPropsName];
  }
  return result;
}
const notNullArray = (value) => Array.isArray(value) && value.length > 0;
function formatter(props, parent = { path: "/" }) {
  const { data, formatMessage, parentName, locale: menuLocale } = props;
  if (!data || !Array.isArray(data)) {
    return [];
  }
  return data.filter((item) => {
    if (!item) return false;
    if (notNullArray(item.children)) return true;
    if (item.path) return true;
    if (item.originPath) return true;
    if (item.layout) return true;
    if (item.redirect) return false;
    if (item.unaccessible) return false;
    return false;
  }).filter((item) => {
    var _a, _b;
    if (((_a = item == null ? void 0 : item.menu) == null ? void 0 : _a.name) || (item == null ? void 0 : item.flatMenu) || ((_b = item == null ? void 0 : item.menu) == null ? void 0 : _b.flatMenu)) {
      return true;
    }
    if (item.menu === false) {
      return false;
    }
    return true;
  }).map((finallyItem) => {
    const item = __spreadProps(__spreadValues({}, finallyItem), {
      path: finallyItem.path || finallyItem.originPath
    });
    if (!item.children && item[childrenPropsName]) {
      item.children = item[childrenPropsName];
      delete item[childrenPropsName];
    }
    if (item.unaccessible) {
      delete item.name;
    }
    if (item.path === "*") {
      item.path = ".";
    }
    if (item.path === "/*") {
      item.path = ".";
    }
    if (!item.path && item.originPath) {
      item.path = item.originPath;
    }
    return item;
  }).map((item = { path: "/" }) => {
    const routerChildren = item.children || item[childrenPropsName] || [];
    const path = mergePath(item.path, parent ? parent.path : "/");
    const { name } = item;
    const locale = getItemLocaleName(item, parentName || "menu");
    const localeName = locale !== false && menuLocale !== false && formatMessage && locale ? formatMessage({ id: locale, defaultMessage: name }) : name;
    const _a = parent, {
      pro_layout_parentKeys = [],
      children,
      icon,
      flatMenu,
      indexRoute,
      routes
    } = _a, restParent = __objRest(_a, [
      "pro_layout_parentKeys",
      "children",
      "icon",
      "flatMenu",
      "indexRoute",
      "routes"
    ]);
    const item_pro_layout_parentKeys = /* @__PURE__ */ new Set([
      ...pro_layout_parentKeys,
      ...item.parentKeys || []
    ]);
    if (parent.key) {
      item_pro_layout_parentKeys.add(parent.key);
    }
    const finallyItem = __spreadProps(__spreadValues(__spreadProps(__spreadValues({}, restParent), {
      menu: void 0
    }), item), {
      path,
      locale,
      key: item.key || getKeyByPath(__spreadProps(__spreadValues({}, item), { path })),
      pro_layout_parentKeys: Array.from(item_pro_layout_parentKeys).filter(
        (key) => key && key !== "/"
      )
    });
    if (localeName) {
      finallyItem.name = localeName;
    } else {
      delete finallyItem.name;
    }
    if (finallyItem.menu === void 0) {
      delete finallyItem.menu;
    }
    if (notNullArray(routerChildren)) {
      const formatterChildren = formatter(
        __spreadProps(__spreadValues({}, props), {
          data: routerChildren,
          parentName: locale || ""
        }),
        finallyItem
      );
      if (notNullArray(formatterChildren)) {
        finallyItem.children = formatterChildren;
      }
    }
    return bigfishCompatibleConversions(finallyItem, props);
  }).flat(1);
}
function defaultFilterMenuData(menuData = []) {
  return menuData.filter(
    (item) => item && (item.name || notNullArray(item.children)) && !item.hideInMenu && !item.redirect
  ).map((item) => {
    const newItem = __spreadValues({}, item);
    const routerChildren = newItem.children || item[childrenPropsName] || [];
    delete newItem[childrenPropsName];
    if (notNullArray(routerChildren) && !newItem.hideChildrenInMenu && routerChildren.some((child) => child && !!child.name)) {
      const newChildren = defaultFilterMenuData(routerChildren);
      if (newChildren.length) {
        return __spreadProps(__spreadValues({}, newItem), {
          children: newChildren
        });
      }
    }
    return __spreadValues({}, item);
  }).filter((item) => item);
}
class RouteListMap extends Map {
  get(pathname) {
    let routeValue;
    try {
      for (const [key, value] of this.entries()) {
        const path = stripQueryStringAndHashFromPath(key);
        if (!isUrl(key) && pathToRegexp(path, []).test(pathname)) {
          routeValue = value;
          break;
        }
      }
    } catch (error) {
      routeValue = void 0;
    }
    return routeValue;
  }
}
function getBreadcrumbNameMap(menuData) {
  const routerMap = new RouteListMap();
  const flattenMenuData = (data, parent) => {
    data.forEach((menuItem) => {
      const routerChildren = menuItem.children || menuItem[childrenPropsName] || [];
      if (notNullArray(routerChildren)) {
        flattenMenuData(routerChildren, menuItem);
      }
      const path = mergePath(menuItem.path, parent ? parent.path : "/");
      routerMap.set(stripQueryStringAndHashFromPath(path), menuItem);
    });
  };
  flattenMenuData(menuData);
  return routerMap;
}
function clearChildren(menuData = []) {
  return menuData.map((item) => {
    const routerChildren = item.children || item[childrenPropsName];
    if (notNullArray(routerChildren)) {
      const newChildren = clearChildren(routerChildren);
      if (newChildren.length) return __spreadValues({}, item);
    }
    const finallyItem = __spreadValues({}, item);
    delete finallyItem[childrenPropsName];
    delete finallyItem.children;
    return finallyItem;
  }).filter((item) => item);
}
function transformRoute(routeList, locale, formatMessage, ignoreFilter) {
  const originalMenuData = formatter({
    data: routeList,
    formatMessage,
    locale
  });
  const menuData = ignoreFilter ? clearChildren(originalMenuData) : defaultFilterMenuData(originalMenuData);
  const breadcrumb = getBreadcrumbNameMap(originalMenuData);
  return { breadcrumb, menuData };
}
var stdin_default = transformRoute;
export {
  childrenPropsName,
  stdin_default as default,
  getKeyByPath,
  isUrl,
  stripQueryStringAndHashFromPath
};
