"use client";
import { jsx, jsxs } from 'react/jsx-runtime';
import { Suspense } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import LayerRenderer from '../../../../../../ui/src/components/ui-builder/layer-renderer.mjs';
import { useSuspenseUIBuilderPageBySlug } from '../hooks/ui-builder-hooks.mjs';
import { defaultComponentRegistry } from '../registry.mjs';
import { uiBuilderLocalization } from '../localization/index.mjs';

function DefaultLoadingComponent() {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-[200px]", children: /* @__PURE__ */ jsx("div", { className: "animate-pulse text-muted-foreground", children: uiBuilderLocalization.pageRenderer.loading }) });
}
function DefaultErrorComponent({ error }) {
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center justify-center min-h-[200px] p-4", children: [
    /* @__PURE__ */ jsx("div", { className: "text-destructive font-medium", children: uiBuilderLocalization.pageRenderer.error }),
    /* @__PURE__ */ jsx("div", { className: "text-sm text-muted-foreground mt-2", children: error.message })
  ] });
}
function DefaultNotFoundComponent() {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-[200px]", children: /* @__PURE__ */ jsx("div", { className: "text-muted-foreground", children: uiBuilderLocalization.pageRenderer.notFound }) });
}
function PageRenderer({
  slug,
  componentRegistry = defaultComponentRegistry,
  variableValues,
  functionRegistry,
  LoadingComponent = DefaultLoadingComponent,
  ErrorComponent = DefaultErrorComponent,
  NotFoundComponent = DefaultNotFoundComponent,
  className
}) {
  return /* @__PURE__ */ jsx(
    ErrorBoundary,
    {
      FallbackComponent: ({ error }) => /* @__PURE__ */ jsx(ErrorComponent, { error }),
      children: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(LoadingComponent, {}), children: /* @__PURE__ */ jsx(
        SuspensePageRendererContent,
        {
          slug,
          componentRegistry,
          variableValues,
          functionRegistry,
          NotFoundComponent,
          className
        }
      ) })
    }
  );
}
function SuspensePageRendererContent({
  slug,
  componentRegistry = defaultComponentRegistry,
  variableValues,
  functionRegistry,
  NotFoundComponent = DefaultNotFoundComponent,
  className
}) {
  const { page, layers, variables } = useSuspenseUIBuilderPageBySlug(slug);
  if (!page || layers.length === 0) {
    return /* @__PURE__ */ jsx(NotFoundComponent, {});
  }
  const rootLayer = layers[0];
  if (!rootLayer) {
    return /* @__PURE__ */ jsx(NotFoundComponent, {});
  }
  return /* @__PURE__ */ jsx(
    LayerRenderer,
    {
      className,
      page: rootLayer,
      componentRegistry,
      variables,
      variableValues,
      functionRegistry
    }
  );
}
function SuspensePageRenderer({
  slug,
  componentRegistry = defaultComponentRegistry,
  variableValues,
  functionRegistry,
  NotFoundComponent = DefaultNotFoundComponent,
  className
}) {
  return /* @__PURE__ */ jsx(
    SuspensePageRendererContent,
    {
      slug,
      componentRegistry,
      variableValues,
      functionRegistry,
      NotFoundComponent,
      className
    }
  );
}

export { PageRenderer, SuspensePageRenderer };
