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));
import { createVNode as _createVNode } from "vue";
import { match } from "path-to-regexp";
import { urlToList } from "./pathTools.mjs";
const defaultItemRender = ({
  route,
  routes
}) => {
  const {
    breadcrumbName,
    title,
    path
  } = route;
  const last = routes.findIndex((i) => i.path === route.path) === routes.length - 1;
  return last ? _createVNode("span", null, [title || breadcrumbName]) : _createVNode("span", {
    "onClick": path ? () => {
      location.href = path;
    } : void 0
  }, [title || breadcrumbName]);
};
function renderItemLocal(item, props) {
  const {
    formatMessage,
    menu
  } = props;
  if (item.locale && formatMessage && (menu == null ? void 0 : menu.locale) !== false) {
    return formatMessage({
      id: item.locale,
      defaultMessage: item.name
    });
  }
  return item.name;
}
function getBreadcrumb(breadcrumbMap, url) {
  let breadcrumbItem = breadcrumbMap.get(url);
  if (!breadcrumbItem) {
    const keys = Array.from(breadcrumbMap.keys()) || [];
    const targetPath = keys.find((path) => {
      try {
        if (path == null ? void 0 : path.startsWith("http")) return false;
        return match(path.replace("?", ""))(url);
      } catch (error) {
        console.log("path", path, error);
        return false;
      }
    });
    if (targetPath) breadcrumbItem = breadcrumbMap.get(targetPath);
  }
  return breadcrumbItem || {
    path: ""
  };
}
function getBreadcrumbFromProps(props) {
  const {
    location: location2,
    breadcrumbMap
  } = props;
  return {
    location: location2,
    breadcrumbMap
  };
}
function conversionFromLocation(routerLocation, breadcrumbMap, props) {
  const pathSnippets = urlToList(routerLocation == null ? void 0 : routerLocation.pathname);
  const extraBreadcrumbItems = pathSnippets.map((url) => {
    const currentBreadcrumb = getBreadcrumb(breadcrumbMap, url);
    const name = renderItemLocal(currentBreadcrumb, props);
    const {
      hideInBreadcrumb
    } = currentBreadcrumb;
    return name && !hideInBreadcrumb ? {
      linkPath: url,
      breadcrumbName: name,
      title: name,
      component: currentBreadcrumb.component
    } : {
      linkPath: "",
      breadcrumbName: "",
      title: ""
    };
  }).filter((item) => item && item.linkPath);
  return extraBreadcrumbItems;
}
function genBreadcrumbProps(props) {
  const {
    location: location2,
    breadcrumbMap
  } = getBreadcrumbFromProps(props);
  if (location2 && location2.pathname && breadcrumbMap) {
    return conversionFromLocation(location2, breadcrumbMap, props);
  }
  return [];
}
function getBreadcrumbProps(props, layoutPros) {
  const {
    breadcrumbRender,
    itemRender: propsItemRender
  } = props;
  const {
    minLength = 2
  } = layoutPros.breadcrumbProps || {};
  const routesArray = genBreadcrumbProps(props);
  const itemRender = (item) => {
    const renderFunction = propsItemRender || defaultItemRender;
    const {
      route
    } = item;
    route.path = route.linkPath || route.path;
    return renderFunction == null ? void 0 : renderFunction(__spreadProps(__spreadValues({}, item), {
      route
    }));
  };
  let items = routesArray;
  if (breadcrumbRender) {
    items = breadcrumbRender(items || []) || void 0;
  }
  if (items && items.length < minLength || breadcrumbRender === false) {
    items = void 0;
  }
  return {
    routes: items,
    itemRender
  };
}
export {
  genBreadcrumbProps,
  getBreadcrumb,
  getBreadcrumbFromProps,
  getBreadcrumbProps
};
