import { jsx, jsxs } from 'react/jsx-runtime';
import { lazy } from 'react';
import { defineClientPlugin } from '@btst/stack/plugins/client';
import { createRoute } from '@btst/yar';
import { fetchAllSitemapEntries, generateRouteDocsSchema } from '../generator.mjs';

const DocsPageComponent = lazy(
  () => import('./components/pages/docs-page.mjs').then((m) => ({
    default: m.DocsPageComponent
  }))
);
const DocsPageSkeleton = lazy(
  () => import('./components/loading/docs-skeleton.mjs').then((m) => ({
    default: m.DocsPageSkeleton
  }))
);
const ROUTE_DOCS_QUERY_KEY = ["route-docs", "schema"];
let moduleStoredContext = null;
function getStoredContext() {
  return moduleStoredContext;
}
async function generateSchema() {
  if (!moduleStoredContext) {
    return {
      plugins: [],
      generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
      allSitemapEntries: []
    };
  }
  try {
    const sitemapEntries = await fetchAllSitemapEntries(moduleStoredContext);
    return generateRouteDocsSchema(moduleStoredContext, sitemapEntries);
  } catch (error) {
    console.warn("Failed to generate route docs schema:", error);
    return generateRouteDocsSchema(moduleStoredContext, []);
  }
}
function createDocsMeta(config) {
  return () => {
    const title = config.title || "Route Documentation";
    return [
      { title },
      { name: "title", content: title },
      { name: "robots", content: "noindex" }
    ];
  };
}
function DocsErrorComponent() {
  return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen bg-background", children: /* @__PURE__ */ jsxs("div", { className: "text-center", children: [
    /* @__PURE__ */ jsx("h1", { className: "text-2xl font-semibold text-destructive mb-2", children: "Error Loading Documentation" }),
    /* @__PURE__ */ jsx("p", { className: "text-muted-foreground", children: "An error occurred while loading the documentation." })
  ] }) });
}
function createRouteDocsLoader(config) {
  return async () => {
    if (typeof window === "undefined" && moduleStoredContext) {
      const { queryClient } = config;
      try {
        const sitemapEntries = await fetchAllSitemapEntries(moduleStoredContext);
        const schema = generateRouteDocsSchema(
          moduleStoredContext,
          sitemapEntries
        );
        queryClient.setQueryData(ROUTE_DOCS_QUERY_KEY, schema);
      } catch (error) {
        console.warn("Failed to load route docs schema:", error);
        queryClient.setQueryData(ROUTE_DOCS_QUERY_KEY, {
          plugins: [],
          generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
          allSitemapEntries: []
        });
      }
    }
  };
}
const routeDocsClientPlugin = (config) => {
  return defineClientPlugin({
    name: "route-docs",
    routes: (context) => {
      moduleStoredContext = context || null;
      return {
        docs: createRoute("/route-docs", () => {
          return {
            PageComponent: () => /* @__PURE__ */ jsx(
              DocsPageComponent,
              {
                title: config.title,
                description: config.description,
                siteBasePath: config.siteBasePath || "/pages"
              }
            ),
            LoadingComponent: () => /* @__PURE__ */ jsx(DocsPageSkeleton, {}),
            ErrorComponent: () => /* @__PURE__ */ jsx(DocsErrorComponent, {}),
            loader: createRouteDocsLoader(config),
            meta: createDocsMeta(config)
          };
        })
      };
    },
    sitemap: async () => {
      return [];
    }
  });
};

export { ROUTE_DOCS_QUERY_KEY, generateSchema, getStoredContext, routeDocsClientPlugin };
