import{SitemapStream as l,streamToPromise as p}from"sitemap";import{Readable as g}from"stream";import{writeFileSync as y}from"fs";import b from"path";import{SITEMAP_FILE_NAME as h}from"../../constants/common.js";import{ServerRoutes as w}from"../../../constants/index.js";import{canAccessResource as P,logger as m}from"../../utils/index.js";async function S(t,o){if(!t)return;const e=t.endsWith(".tsx"),r=t.endsWith(".md");if(!(!e&&!r))try{const i=e?"react-frontmatter":"markdown-frontmatter",a=await o.load(t,i);return a.data.frontmatter?.seo?.priority??a.data.seo?.priority}catch{return}}async function R(t,o){const e=t.fsPath?await S(t.fsPath,o):void 0;if(typeof e=="number"){if(e>=0&&e<=1)return e;m.warn(`Invalid SEO priority value "${e}" found for route "${t.slug}". Priority must be between 0 and 1. Using default priority instead.`)}const r=t.versions?.find(i=>i.active);return r?.default||!r?.5:.3}async function L(){return{id:"sitemap",async afterRoutesCreated(t,o){const{seo:e}=t.getConfig();if(!e?.siteUrl)return;const r=t.getGlobalConfig("rbac"),i=t.getGlobalConfig("requiresLogin"),a=t.getAllRoutes().filter(n=>P(n,{teams:["anonymous"]},r,i)&&!Object.keys(w).includes(n.slug));if(a.length)try{const n=b.join(t.outdir,h),c=new l({hostname:e.siteUrl}),u=await Promise.all(a.map(async s=>{const d=await R(s,o.cache);return{url:s.slug,priority:d}})),f=await p(g.from(u).pipe(c)).then(s=>s.toString());y(n,f),t.hasSitemap=!0}catch(n){m.error(`Error creating sitemap: ${n.message}`)}}}}export{L as sitemapPlugin};
