'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const client = require('@btst/stack/plugins/client');
const yar = require('@btst/yar');
const generator = require('../generator.cjs');

const DocsPageComponent = React.lazy(
  () => import('./components/pages/docs-page.cjs').then((m) => ({
    default: m.DocsPageComponent
  }))
);
const DocsPageSkeleton = React.lazy(
  () => import('./components/loading/docs-skeleton.cjs').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 generator.fetchAllSitemapEntries(moduleStoredContext);
    return generator.generateRouteDocsSchema(moduleStoredContext, sitemapEntries);
  } catch (error) {
    console.warn("Failed to generate route docs schema:", error);
    return generator.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__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center min-h-screen bg-background", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-center", children: [
    /* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-2xl font-semibold text-destructive mb-2", children: "Error Loading Documentation" }),
    /* @__PURE__ */ jsxRuntime.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 generator.fetchAllSitemapEntries(moduleStoredContext);
        const schema = generator.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 client.defineClientPlugin({
    name: "route-docs",
    routes: (context) => {
      moduleStoredContext = context || null;
      return {
        docs: yar.createRoute("/route-docs", () => {
          return {
            PageComponent: () => /* @__PURE__ */ jsxRuntime.jsx(
              DocsPageComponent,
              {
                title: config.title,
                description: config.description,
                siteBasePath: config.siteBasePath || "/pages"
              }
            ),
            LoadingComponent: () => /* @__PURE__ */ jsxRuntime.jsx(DocsPageSkeleton, {}),
            ErrorComponent: () => /* @__PURE__ */ jsxRuntime.jsx(DocsErrorComponent, {}),
            loader: createRouteDocsLoader(config),
            meta: createDocsMeta(config)
          };
        })
      };
    },
    sitemap: async () => {
      return [];
    }
  });
};

exports.ROUTE_DOCS_QUERY_KEY = ROUTE_DOCS_QUERY_KEY;
exports.generateSchema = generateSchema;
exports.getStoredContext = getStoredContext;
exports.routeDocsClientPlugin = routeDocsClientPlugin;
