"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const errorBoundary = require('./error-boundary.cjs');

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

exports.ComposedRoute = ComposedRoute;
exports.RouteRenderer = RouteRenderer;
