import {
  renderTypography
} from "./chunk-KAMBSKAE.js";
import {
  isString
} from "./chunk-HAC5YAGQ.js";
import {
  colors
} from "./chunk-MIIIUXV6.js";

// src/router.tsx
import styled from "@emotion/styled";
import { omit } from "lodash-es";
import { forwardRef, 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, hash) {
  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;
  }
  if (hash) {
    to = `${to}#${hash}`;
  }
  return to;
}
var LinkBase = forwardRef(
  ({
    to,
    id,
    externalLink,
    filename,
    params,
    children,
    text,
    css,
    onClick,
    className,
    hash = null,
    blue = false,
    newTab: newTabProp,
    typography
  }, ref) => {
    if (!isString(to) && !externalLink && !onClick) {
      throw new Error(
        "Link must have either `to` or `externalLink` or `onClick` defined"
      );
    }
    let toStr;
    let newTab = Boolean(newTabProp);
    if (isString(to)) {
      toStr = replaceParams(to, params, hash);
    } else if (externalLink) {
      const definedExternalLink = externalLink;
      if (!definedExternalLink.startsWith("http") && !definedExternalLink.startsWith("mailto:")) {
        throw new Error("Link's externalLink must start with http or mailto:");
      }
      if (newTabProp === void 0) {
        newTab = true;
      }
      toStr = definedExternalLink;
    } else {
      toStr = "#";
    }
    let content = children;
    if (text) {
      content = typography ? renderTypography(typography.type, {
        size: typography.size,
        color: typography.color,
        children: text,
        underline: typography.underline
      }) : text;
    }
    return /* @__PURE__ */ jsx(
      RLink,
      {
        to: toStr,
        id,
        css,
        onClick,
        className,
        download: filename,
        style: { color: blue ? colors.blue43 : "inherit" },
        target: newTab ? "_blank" : void 0,
        rel: newTab ? "noopener noreferrer" : void 0,
        ref,
        children: content
      }
    );
  }
);
LinkBase.displayName = "LinkBase";
var Link = styled(LinkBase)`
  ${({ disabled }) => disabled && `pointer-events: none;`}
`;
function Navigate({
  to,
  params,
  hash,
  state,
  replace = false
}) {
  to = replaceParams(to, params, hash);
  return /* @__PURE__ */ jsx(RNavigate, { to, state, replace });
}
function useNavigate() {
  const navigate = useRNavigate();
  return useCallback(
    (to, params, options, hash) => {
      if (typeof to === "string") {
        to = replaceParams(to, params, hash);
        return navigate(to, options);
      }
      return navigate(to);
    },
    [navigate]
  );
}
function useMatchRoutes(routes) {
  const location = useLocation();
  const doesMatch = matchRoutes(routes, location.pathname);
  return doesMatch;
}
function matchRoutes(routes, locationPathname) {
  return routes.some((route) => matchPath(route, locationPathname));
}
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,
  LinkBase,
  Link,
  Navigate,
  useNavigate,
  useMatchRoutes,
  matchRoutes,
  useFindMatchingRoute,
  useMatchRoute,
  useCurrentRoute,
  getRouteName
};
