(function(){const o=document.createElement("link").relList;if(o&&o.supports&&o.supports("modulepreload"))return;for(const r of document.querySelectorAll('link[rel="modulepreload"]'))s(r);new MutationObserver(r=>{for(const n of r)if(n.type==="childList")for(const d of n.addedNodes)d.tagName==="LINK"&&d.rel==="modulepreload"&&s(d)}).observe(document,{childList:!0,subtree:!0});function l(r){const n={};return r.integrity&&(n.integrity=r.integrity),r.referrerPolicy&&(n.referrerPolicy=r.referrerPolicy),r.crossOrigin==="use-credentials"?n.credentials="include":r.crossOrigin==="anonymous"?n.credentials="omit":n.credentials="same-origin",n}function s(r){if(r.ep)return;r.ep=!0;const n=l(r);fetch(r.href,n)}})();const x=document.querySelector("#app"),t={articles:[],groups:[],loading:!0,error:null,filter:"all",viewMode:"regular",carMode:!1,carModeIndex:0,carModeInterval:null};function M(e){const o=new Date(e),s=new Date-o,r=Math.floor(s/(1e3*60*60)),n=Math.floor(s/(1e3*60));return n<60?n+"m ago":r<24?r+"h ago":o.toLocaleDateString("en-US",{month:"short",day:"numeric"})}function b(e,o=150){return!e||e.length<=o?e||"":e.slice(0,o).trim()+"..."}const f={abc:{name:"ABC News",color:"bg-blue-100 text-blue-800"},npr:{name:"NPR",color:"bg-indigo-100 text-indigo-800"},cnn:{name:"CNN",color:"bg-orange-100 text-orange-800"},nbc:{name:"NBC News",color:"bg-purple-100 text-purple-800"},cbs:{name:"CBS News",color:"bg-emerald-100 text-emerald-800"},nytimes:{name:"NY Times",color:"bg-slate-100 text-slate-800"}};function u(e){return f[e]?.color||"bg-gray-100 text-gray-800"}function c(e){return f[e]?.name||e}const w={politics:"bg-red-100 text-red-800",business:"bg-green-100 text-green-800",technology:"bg-blue-100 text-blue-800",sports:"bg-orange-100 text-orange-800",entertainment:"bg-pink-100 text-pink-800",health:"bg-teal-100 text-teal-800",science:"bg-purple-100 text-purple-800",world:"bg-indigo-100 text-indigo-800",other:"bg-gray-100 text-gray-800"};function y(e){return w[e]||w.other}function k(e){const o=e.image?`
`:"",l=e.sources.slice(0,3).map(r=>`${c(r)}`).join(""),s=e.sources.length>3?`+${e.sources.length-3}`:"";return`
${o}
${e.category} ${e.articleCount} articles AI Summary

${e.title}

${e.summary}

${l}${s}
View source articles
`}function C(e){const o=e.image?`
`:"";return`
${o}
${c(e.source)} ${M(e.pubDate)}

${e.title}

${b(e.content,500)}

`}function $(){return`

${t.viewMode==="ai"?"AI is analyzing and grouping news...":"Loading latest news..."}

`}function j(e){return`

Failed to load news

${e}

`}function v(){return`

No articles found

Try selecting a different source filter.

`}function N(e,o,l){const s=e.sources.slice(0,4).map(n=>`${c(n)}`).join(""),r=e.sources.length>4?`+${e.sources.length-4} more`:"";return`
${e.image?`
`:""}
${e.category} ${e.articleCount} articles AI Summary

${e.title}

${e.summary}

${s}${r}
${Array.from({length:l},(n,d)=>`
`).join("")}
`}function I(){if(t.groups.length===0)return`

Loading AI grouped news...

`;const e=t.groups[t.carModeIndex];return`
${N(e,t.carModeIndex,t.groups.length)}
Car Mode
`}function a(){if(t.carMode){x.innerHTML=I();return}const e=t.filter==="all"?t.articles:t.articles.filter(i=>i.source===t.filter),o=t.loading?"animate-spin":"",l=[...new Set(t.articles.map(i=>i.source))];let s="";t.loading?s=$():t.error?s=j(t.error):t.viewMode==="ai"?t.groups.length===0?s=v():s=t.groups.map(k).join(""):e.length===0?s=v():s=e.map(C).join("");const r=t.viewMode==="regular"?"bg-gray-900 text-white":"bg-gray-100 text-gray-700 hover:bg-gray-200",n=t.viewMode==="ai"?"bg-blue-600 text-white":"bg-blue-100 text-blue-700 hover:bg-blue-200",d=t.viewMode==="regular"?` ${l.map(i=>``).join("")} `:"";x.innerHTML=`

News Feed

${t.viewMode==="ai"?"AI-grouped summaries from multiple sources":"Latest headlines from multiple sources"}

${d} ${t.viewMode==="ai"?'':""}
${s}
`}async function g(){t.loading=!0,t.error=null,a();try{const e=await fetch("/api/news");if(!e.ok)throw new Error("Failed to fetch news");const o=await e.json(),l=o.feeds.flatMap(s=>s.items).sort((s,r)=>new Date(r.pubDate)-new Date(s.pubDate));t.articles=l,t.loading=!1,o.errors&&o.errors.length>0&&console.warn("Some feeds failed:",o.errors)}catch(e){t.error=e.message,t.loading=!1}a()}async function p(){t.loading=!0,t.error=null,a();try{const e=await fetch("/api/grouped-news");if(!e.ok)throw new Error("Failed to fetch grouped news");const o=await e.json();t.groups=o.groups,t.loading=!1}catch(e){t.error=e.message,t.loading=!1}a()}window.setFilter=function(e){t.filter=e,a()};window.setViewMode=function(e){t.viewMode=e,e==="ai"&&t.groups.length===0?p():a()};window.refresh=function(){t.viewMode==="ai"?p():g()};window.clearCacheAndRefresh=async function(){t.loading=!0,t.error=null,a();try{const e=await fetch("/api/grouped-news?refresh=true");if(!e.ok)throw new Error("Failed to fetch grouped news");const o=await e.json();t.groups=o.groups,t.loading=!1}catch(e){t.error=e.message,t.loading=!1}a()};window.fetchNews=g;window.fetchGroupedNews=p;window.enterCarMode=async function(){if(t.carMode=!0,t.carModeIndex=0,a(),t.groups.length===0)try{const e=await fetch("/api/grouped-news");if(!e.ok)throw new Error("Failed to fetch grouped news");const o=await e.json();t.groups=o.groups,a()}catch(e){console.error("Failed to load grouped news for car mode:",e)}h()};window.exitCarMode=function(){t.carMode=!1,m(),a()};window.carModeNext=function(){t.groups.length!==0&&(t.carModeIndex=(t.carModeIndex+1)%t.groups.length,m(),a(),h())};window.carModePrev=function(){t.groups.length!==0&&(t.carModeIndex=(t.carModeIndex-1+t.groups.length)%t.groups.length,m(),a(),h())};function h(){m(),t.carModeInterval=setInterval(()=>{t.groups.length>0&&(t.carModeIndex=(t.carModeIndex+1)%t.groups.length,a())},5e3)}function m(){t.carModeInterval&&(clearInterval(t.carModeInterval),t.carModeInterval=null)}document.addEventListener("keydown",e=>{if(t.carMode)switch(e.key){case"Escape":window.exitCarMode();break;case"ArrowRight":case" ":window.carModeNext();break;case"ArrowLeft":window.carModePrev();break}});g();setInterval(()=>{t.viewMode==="ai"?p():g()},300*1e3);