import {
  isString
} from "./chunk-LAMQKQU3.js";
import {
  colors
} from "./chunk-2VBDEO6M.js";

// src/router.tsx
import { omit } from "lodash-es";
import { useCallback } from "react";
import {
  Link as RLink,
  Navigate as RNavigate,
  matchPath,
  useLocation,
  useNavigate as useRNavigate
} from "react-router-dom";
import { jsx } from "@emotion/react/jsx-runtime";
function replaceParams(to, params) {
  if (params) {
    let toWithParams = to;
    Object.entries(omit(params, "query")).forEach(([key, value]) => {
      if (typeof value !== "string") {
        throw new Error(
          `Only 'query' may be an object. Route params must be a string, but '${key}' is not.`
        );
      }
      toWithParams = toWithParams.replace(`:${key}`, value);
    });
    if (params.query) {
      let query = params.query;
      if (typeof query !== "string") {
        query = Object.entries(params.query).map(([key, value]) => `${key}${value ? `=${value}` : ""}`).join("&");
      }
      toWithParams = `${toWithParams}?${query}`;
    }
    to = toWithParams;
  }
  return to;
}
function Link({
  to,
  externalLink,
  params,
  children,
  css,
  onClick,
  className,
  hash = null,
  blue = false,
  newTab = false
}) {
  if (!isString(to) && !externalLink && !onClick) {
    throw new Error(
      "Link must have either `to` or `externalLink` or `onClick` defined"
    );
  }
  let toStr;
  if (isString(to)) {
    toStr = replaceParams(to, params);
    toStr += hash ? `#${hash}` : "";
  } else if (externalLink) {
    const definedExternalLink = externalLink;
    if (!definedExternalLink.startsWith("http")) {
      throw new Error("Link's externalLink must start with http");
    }
    toStr = definedExternalLink;
  } else {
    toStr = "#";
  }
  return /* @__PURE__ */ jsx(
    RLink,
    {
      to: toStr,
      css,
      onClick,
      className,
      style: { color: blue ? colors.blue43 : "inherit" },
      target: newTab ? "_blank" : void 0,
      rel: newTab ? "noopener noreferrer" : void 0,
      children
    }
  );
}
function Navigate({
  to,
  params,
  state,
  replace = false
}) {
  to = replaceParams(to, params);
  return /* @__PURE__ */ jsx(RNavigate, { to, state, replace });
}
function useNavigate() {
  const navigate = useRNavigate();
  return useCallback(
    (to, params, options) => {
      if (typeof to === "string") {
        to = replaceParams(to, params);
        return navigate(to, options);
      }
      return navigate(to);
    },
    [navigate]
  );
}
function useMatchRoutes(routes) {
  const location = useLocation();
  const doesMatch = routes.some((route) => matchPath(route, location.pathname));
  return doesMatch;
}
function useFindMatchingRoute(routes) {
  const location = useLocation();
  const matchingRoute = routes.find(
    (route) => matchPath(route, location.pathname)
  );
  return matchingRoute;
}
function useMatchRoute(route) {
  const location = useLocation();
  return matchPath(route, location.pathname);
}
function useCurrentRoute() {
  const location = useLocation();
  return location.pathname;
}
function getRouteName(path, routes) {
  for (const routeName of Object.keys(routes)) {
    if (matchPath(routes[routeName], path)) {
      return routeName;
    }
  }
  return null;
}

export {
  replaceParams,
  Link,
  Navigate,
  useNavigate,
  useMatchRoutes,
  useFindMatchingRoute,
  useMatchRoute,
  useCurrentRoute,
  getRouteName
};
