import React from "react";
import { Link as RouterLink } from "react-router-dom";

import { RouteInfo, useRouter } from "../contexts/RouterContext";

export type LinkProps = { route: RouteInfo } | { route: string };

export const Link: React.FC<LinkProps> = ({ route }) => {
  const { getRoute } = useRouter();

  if (typeof route === "string") {
    const routeInfo = getRoute(route);

    if (routeInfo === undefined) {
      throw new Error(`Could not find route with reverse: ${route}`);
    }

    route = routeInfo;
  }

  return <RouterLink to={route.path}></RouterLink>;
};

export default Link;
