import { RouteObject } from "react-router-dom";
import { RoutePathDefinition } from "./types";

export function mapDefinitionToRoute(props: RoutePathDefinition): RouteObject {
  const { Component, element } = props;
  if(typeof element === "undefined" && typeof Component === "undefined") {
    throw Error("Either `element` or `Component` ")
  }
  return props.element
    ? props
    : Component! && {
        ...props,
        children:
          props.children &&
          props.children.map((props, index) => mapDefinitionToRoute(props)),
        element: <Component name={props.name} path={props.path} />,
      };
}
