import { fileURLToPath } from "url";
import * as colors from "kleur/colors";
import { debug } from "../logger.js";
import { preload as ssrPreload } from "../ssr/index.js";
import { validateGetStaticPathsModule, validateGetStaticPathsResult } from "../ssr/routing.js";
import { generateRssFunction } from "../ssr/rss.js";
import { assignStaticPaths } from "../ssr/route-cache.js";
async function collectPagesData(opts) {
  const { astroConfig, logging, manifest, origin, routeCache, viteServer } = opts;
  const assets = {};
  const allPages = {};
  await Promise.all(manifest.routes.map(async (route) => {
    var _a;
    if (route.pathname) {
      allPages[route.component] = {
        route,
        paths: [route.pathname],
        preload: await ssrPreload({
          astroConfig,
          filePath: new URL(`./${route.component}`, astroConfig.projectRoot),
          logging,
          mode: "production",
          origin,
          pathname: route.pathname,
          route,
          routeCache,
          viteServer
        }).then((routes) => {
          const html = `${route.pathname}`.replace(/\/?$/, "/index.html");
          debug(logging, "build", `\u251C\u2500\u2500 ${colors.bold(colors.green("\u2714"))} ${route.component} \u2192 ${colors.yellow(html)}`);
          return routes;
        }).catch((err) => {
          debug(logging, "build", `\u251C\u2500\u2500 ${colors.bold(colors.red("\u2718"))} ${route.component}`);
          throw err;
        })
      };
      return;
    }
    const result = await getStaticPathsForRoute(opts, route).then((routes) => {
      const label = routes.paths.length === 1 ? "page" : "pages";
      debug(logging, "build", `\u251C\u2500\u2500 ${colors.bold(colors.green("\u2714"))} ${route.component} \u2192 ${colors.magenta(`[${routes.paths.length} ${label}]`)}`);
      return routes;
    }).catch((err) => {
      debug(logging, "build", `\u251C\u2500\u2500 ${colors.bold(colors.red("\u2717"))} ${route.component}`);
      throw err;
    });
    if ((_a = result.rss) == null ? void 0 : _a.xml) {
      const rssFile = new URL(result.rss.url.replace(/^\/?/, "./"), astroConfig.dist);
      if (assets[fileURLToPath(rssFile)]) {
        throw new Error(`[getStaticPaths] RSS feed ${result.rss.url} already exists.
Use \`rss(data, {url: '...'})\` to choose a unique, custom URL. (${route.component})`);
      }
      assets[fileURLToPath(rssFile)] = result.rss.xml;
    }
    allPages[route.component] = {
      route,
      paths: result.paths,
      preload: await ssrPreload({
        astroConfig,
        filePath: new URL(`./${route.component}`, astroConfig.projectRoot),
        logging,
        mode: "production",
        origin,
        pathname: result.paths[0],
        route,
        routeCache,
        viteServer
      })
    };
  }));
  return { assets, allPages };
}
async function getStaticPathsForRoute(opts, route) {
  const { astroConfig, logging, routeCache, viteServer } = opts;
  if (!viteServer)
    throw new Error(`vite.createServer() not called!`);
  const filePath = new URL(`./${route.component}`, astroConfig.projectRoot);
  const mod = await viteServer.ssrLoadModule(fileURLToPath(filePath));
  validateGetStaticPathsModule(mod);
  const rss = generateRssFunction(astroConfig.buildOptions.site, route);
  await assignStaticPaths(routeCache, route, mod, rss.generator);
  const staticPaths = routeCache[route.component];
  validateGetStaticPathsResult(staticPaths, logging);
  return {
    paths: staticPaths.map((staticPath) => staticPath.params && route.generate(staticPath.params)).filter(Boolean),
    rss: rss.rss
  };
}
export {
  collectPagesData
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vc3JjL2NvcmUvYnVpbGQvcGFnZS1kYXRhLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBS0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUVBO0FBQ0E7QUFpQkEsZ0NBQXVDLE1BQWdFO0FBQ3RHLFFBQU0sRUFBRSxhQUFhLFNBQVMsVUFBVSxRQUFRLFlBQVksZUFBZTtBQUUzRSxRQUFNLFNBQWlDO0FBQ3ZDLFFBQU0sV0FBeUI7QUFNL0IsUUFBTSxRQUFRLElBQ2IsU0FBUyxPQUFPLElBQUksT0FBTyxVQUFVO0FBeEN2QztBQTBDRyxRQUFJLE1BQU0sVUFBVTtBQUNuQixlQUFTLE1BQU0sYUFBYTtBQUFBLFFBQzNCO0FBQUEsUUFDQSxPQUFPLENBQUMsTUFBTTtBQUFBLFFBQ2QsU0FBUyxNQUFNLFdBQVc7QUFBQSxVQUN6QjtBQUFBLFVB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