import {renderNewsBody} from './news-format.mjs';
import {forumLinks,NEWS_CATEGORIES} from './news.mjs';
import {SHARED_HEADER,SHARED_HEAD} from '../scripts/portal-navigation.mjs';
const base='https://www.dunerealms.com';
const esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
const pages={
'/':['DuneRealms — Desert Minecraft Server | Java & Bedrock','Explore DuneRealms, a desert science-fiction Minecraft community. Read server news, follow The Quest for Tavren II Aedrecht and join on Java or Bedrock.'],
'/news-archive.html':['DuneRealms News & Changelog Archive','Browse DuneRealms server news, maintenance notices, community updates and published changelogs.'],
'/manual.html':['DuneRealms Field Manual & Wiki','Learn about DuneRealms worlds, houses, travel, spice, gameplay and community systems in the illustrated field manual.'],
'/wiki.html':['Our World & Disclaimer | DuneRealms','Discover the original DuneRealms setting, Dutch-inspired names, fan-project disclaimer and contact information.'],
'/donate.html':['Community Support & Goals | DuneRealms','Follow DuneRealms community funding goals and shared rewards. Check current availability before contributing.'],
'/discord.html':['DuneRealms Discord Community','Find the DuneRealms community Discord and connect with fellow desert adventurers.'],
'/staff.html':['DuneRealms Staff & Contributors','Meet the DuneRealms community staff and project contributors.']
};
export const newsPath=p=>'/news/'+p.title.toLowerCase().normalize('NFKD').replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'').slice(0,75)+'-'+p.id;
const published="SELECT p.id,p.title,p.body,p.category,p.published_at,p.updated_at,COALESCE(a.display_name,a.username,'DuneRealms') author_name FROM website_posts p LEFT JOIN site_accounts a ON a.id=p.author_id WHERE p.status='published' AND p.published_at<=?";
const ld=data=>'';
function metadata(html,{title,description,canonical,type='website',image=base+'/assets/dunerealms-season-logo-v2.png',structured}){
html=html.replace(/
[\s\S]*?<\/title>/i,'').replace(/]*(?:name=["'](?:description|robots|twitter:[^"']+)["']|property=["']og:[^"']+["']))[^>]*>/gi,'').replace(/]*rel=["']canonical["'])[^>]*>/gi,'');
const head=''+esc(title)+''+(structured?ld(structured):'');
return html.replace(/<\/head>/i,head+'');
}
export function createSearchVisibility(db){
const getPosts=async(limit=50000)=> (await db.prepare(published+' ORDER BY p.published_at DESC,p.id DESC LIMIT ?').bind(Date.now(),limit).all()).results;
return{
async route(request){
const u=new URL(request.url);if(!['GET','HEAD'].includes(request.method))return null;
if(u.pathname==='/robots.txt')return new Response('User-agent: *\nAllow: /\nDisallow: /api/\nDisallow: /internal/\nDisallow: /git/\nSitemap: '+base+'/sitemap.xml\n',{headers:{'Content-Type':'text/plain; charset=utf-8','Cache-Control':'public,max-age=300'}});
if(u.pathname==='/sitemap.xml'){
const posts=await getPosts();const urls=[...Object.keys(pages).filter(p=>p!=='/manual.html').map(p=>''+base+p+''),...posts.map(p=>''+esc(base+newsPath(p))+''+new Date(Number(p.updated_at||p.published_at)).toISOString()+'')];
return new Response(''+urls.join('')+'',{headers:{'Content-Type':'application/xml; charset=utf-8','Cache-Control':'public,max-age=300'}});
}
if(u.pathname==='/index.html')return Response.redirect(base+'/',301);
const legacy=u.pathname==='/news-archive.html'&&u.searchParams.has('post'),match=u.pathname.match(/^\/news\/(?:[a-z0-9-]*-)?([a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12})$/i);
if(!legacy&&!match)return null;
const id=legacy?u.searchParams.get('post'):match[1];if(!/^[a-f0-9-]{36}$/i.test(id||''))return new Response('News post not found',{status:404});
const p=await db.prepare(published+' AND p.id=?').bind(Date.now(),id).first();if(!p)return new Response('News post not found',{status:404,headers:{'X-Robots-Tag':'noindex'}});
const path=newsPath(p);if(legacy||u.pathname!==path)return Response.redirect(base+path,301);
const description=p.body.replace(/\s+/g,' ').slice(0,160),date=new Date(Number(p.published_at)).toISOString();
const art='/assets/news-'+(['world','news','maintenance','changelogs','events','community'].includes(p.category)?p.category:'news')+'.webp';
const linked=(await forumLinks(db,[p]))[0],byline='