import {
  parseSitemapEntries,
  parseSitemapIndex,
  sortSitemapEntries,
  extractSitemapUrlFromRobots
} from "./sitemap.js";
const PROBE_PATHS = ["/sitemap.xml", "/sitemap_index.xml", "/sitemap.xml.gz"];
const SITEMAP_MIN_URLS = 5;
const MAX_INDEX_CHILDREN = 5;
async function probeSitemap(origin, rawFetch) {
  try {
    const robots = await rawFetch(`${origin}/robots.txt`);
    if (robots.html) {
      const sitemapUrls = extractSitemapUrlFromRobots(robots.html);
      for (const smUrl of sitemapUrls) {
        const urls = await fetchAndParseSitemap(smUrl, rawFetch);
        if (urls && urls.length >= SITEMAP_MIN_URLS) return urls;
      }
    }
  } catch {
  }
  for (const path of PROBE_PATHS) {
    if (path.endsWith(".gz")) continue;
    const urls = await fetchAndParseSitemap(`${origin}${path}`, rawFetch);
    if (urls && urls.length >= SITEMAP_MIN_URLS) return urls;
  }
  return null;
}
async function fetchAndParseSitemap(url, rawFetch) {
  try {
    const result = await rawFetch(url);
    if (!result.html) return null;
    if (result.statusCode && result.statusCode >= 400) return null;
    if (result.html.includes("<sitemapindex")) {
      const children = parseSitemapIndex(result.html);
      const all = [];
      for (const child of children.slice(0, MAX_INDEX_CHILDREN)) {
        const grand = await fetchAndParseSitemap(child, rawFetch);
        if (grand) all.push(...grand);
      }
      return all.length > 0 ? all : null;
    }
    const entries = parseSitemapEntries(result.html);
    if (entries.length === 0) return null;
    return sortSitemapEntries(entries).map((e) => e.url);
  } catch {
    return null;
  }
}
export {
  probeSitemap
};
//# sourceMappingURL=sitemap-first.js.map