"use client";
import { jsx } from 'react/jsx-runtime';
import { Suspense } from 'react';
import { ErrorBoundary } from './error-boundary.mjs';

function RouteRenderer({
  router,
  path,
  NotFoundComponent,
  onNotFound,
  onError,
  props
}) {
  const route = router.getRoute(path);
  return /* @__PURE__ */ jsx(
    ComposedRoute,
    {
      path,
      PageComponent: route?.PageComponent,
      ErrorComponent: route?.ErrorComponent,
      LoadingComponent: route?.LoadingComponent,
      onNotFound,
      NotFoundComponent,
      onError,
      props
    }
  );
}
function ComposedRoute({
  path,
  PageComponent,
  ErrorComponent,
  LoadingComponent,
  onNotFound,
  NotFoundComponent,
  props,
  onError
}) {
  if (PageComponent) {
    const content = /* @__PURE__ */ jsx(PageComponent, { ...props });
    const isBrowser = typeof window !== "undefined";
    const suspenseFallback = isBrowser && LoadingComponent ? /* @__PURE__ */ jsx(LoadingComponent, {}) : null;
    if (ErrorComponent) {
      return /* @__PURE__ */ jsx(Suspense, { fallback: suspenseFallback, children: /* @__PURE__ */ jsx(
        ErrorBoundary,
        {
          FallbackComponent: ErrorComponent,
          resetKeys: [path],
          onError,
          children: /* @__PURE__ */ jsx(Suspense, { fallback: suspenseFallback, children: content }, `inner-${path}`)
        }
      ) }, `outer-${path}`);
    }
    return /* @__PURE__ */ jsx(Suspense, { fallback: suspenseFallback, children: content }, path);
  } else {
    if (onNotFound) {
      onNotFound();
    }
    if (NotFoundComponent) {
      return /* @__PURE__ */ jsx(NotFoundComponent, { message: `Unknown route: ${path}` });
    }
  }
}

export { ComposedRoute, RouteRenderer };
