import { match } from "path-to-regexp";
function matchParamsPath(pathname, breadcrumb, breadcrumbMap) {
  if (breadcrumbMap) {
    const pathKey = [...breadcrumbMap.keys()].find((key) => {
      try {
        if (key.startsWith("http")) {
          return false;
        }
        return match(key)(pathname);
      } catch (error) {
        console.log("key", key, error);
        return false;
      }
    });
    if (pathKey) {
      return breadcrumbMap.get(pathKey);
    }
  }
  if (breadcrumb) {
    const pathKey = Object.keys(breadcrumb).find((key) => {
      try {
        if (key == null ? void 0 : key.startsWith("http")) {
          return false;
        }
        return match(key)(pathname);
      } catch (error) {
        console.log("key", key, error);
        return false;
      }
    });
    if (pathKey) {
      return breadcrumb[pathKey];
    }
  }
  return {
    path: ""
  };
}
function getPageTitleInfo(props, ignoreTitle) {
  const {
    pathname = "/",
    breadcrumb,
    breadcrumbMap,
    formatMessage,
    title,
    menu = {
      locale: false
    }
  } = props;
  const pageTitle = ignoreTitle ? "" : title || "";
  const currRouterData = matchParamsPath(pathname, breadcrumb, breadcrumbMap);
  if (!currRouterData) {
    return {
      title: pageTitle,
      id: "",
      pageName: pageTitle
    };
  }
  let pageName = currRouterData.name;
  if (menu.locale !== false && currRouterData.locale && formatMessage) {
    pageName = formatMessage({
      id: currRouterData.locale || "",
      defaultMessage: currRouterData.name
    });
  }
  if (!pageName) {
    return {
      title: pageTitle,
      id: currRouterData.locale || "",
      pageName: pageTitle
    };
  }
  if (ignoreTitle || !title) {
    return {
      title: pageName,
      id: currRouterData.locale || "",
      pageName
    };
  }
  return {
    title: `${pageName} - ${title}`,
    id: currRouterData.locale || "",
    pageName
  };
}
function getPageTitle(props, ignoreTitle) {
  return getPageTitleInfo(props, ignoreTitle).title;
}
export {
  getPageTitle,
  getPageTitleInfo,
  matchParamsPath
};
