var e=(e,n,t)=>new Promise((s,r)=>{var i=e=>{try{a(t.next(e))}catch(n){r(n)}},o=e=>{try{a(t.throw(e))}catch(n){r(n)}},a=e=>e.done?s(e.value):Promise.resolve(e.value).then(i,o);a((t=t.apply(e,n)).next())});import{j as n,Z as t}from"./ui-vendor-BewJQzBv.js";import{B as s}from"./case-study-components-DWRUehs-.js";import{i as r,g as i}from"./breakingNewsUtils-Dp-Tjbac.js";import{p as o,i as a,u as l,j as c,s as u}from"./home-components-zRcEiFkI.js";import{r as d}from"./react-vendor-D6Nbkvjy.js";const w=({createdAt:e,isBreaking:o=!1,variant:a="default",showTimeRemaining:l=!1,className:c=""})=>{if(!r(e,o))return null;const u=l?i(e):"";return n.jsxs(s,{className:`bg-red-600 hover:bg-red-700 text-white font-bold transition-colors animate-pulse ${{default:"px-2 py-1 rounded-full text-xs",compact:"px-1.5 py-0.5 rounded text-[10px]",corner:"px-2 py-1 rounded-full text-xs shadow-lg"}[a]} ${c}`,children:[n.jsx(t,{className:"h-2 w-2 mr-1"}),"BREAKING",u&&n.jsxs("span",{className:"ml-1 text-[10px] opacity-80",children:["(",u,")"]})]})},m=n=>{const{user:t}=o(),s=a(),{data:r,isLoading:i}=l({queryKey:["news-views",n],queryFn:()=>e(void 0,null,function*(){if(!n)return null;const{count:e,error:t}=yield u.from("news_views").select("*",{count:"exact",head:!0}).eq("news_id",n);if(t)throw t;const{count:s,error:r}=yield u.from("news_views").select("*",{count:"exact",head:!0}).eq("news_id",n).not("user_id","is",null);if(r)throw r;if(null!==e){const{error:t}=yield u.from("news").update({view_count:e}).eq("id",n)}return{totalViews:e||0,uniqueMembers:s||0,anonymousViews:(e||0)-(s||0)}}),enabled:!!n}),w=c({mutationFn:n=>e(void 0,[n],function*({newsId:e,deviceFingerprint:n}){const s={news_id:e,user_id:(null==t?void 0:t.id)||null,device_fingerprint:n,ip_address:null},{error:r}=yield u.from("news_views").insert([s]);if(r&&!r.message.includes("duplicate key value violates unique constraint"))throw r;if(!r){const{error:n}=yield u.rpc("increment_view_count",{table_name:"news",record_id:e})}}),onSuccess:()=>{s.invalidateQueries({queryKey:["news-views"]}),s.invalidateQueries({queryKey:["news"]})}});return d.useEffect(()=>{if(!n)return;const e=u.channel(`news-views-${n}`).on("postgres_changes",{event:"INSERT",schema:"public",table:"news_views",filter:`news_id=eq.${n}`},()=>{s.invalidateQueries({queryKey:["news-views",n]}),s.invalidateQueries({queryKey:["news"]})}).subscribe();return()=>{u.removeChannel(e)}},[n,s]),{viewCounts:r,isLoading:i,trackView:e=>{const n=(()=>{const e=document.createElement("canvas"),n=e.getContext("2d");n.textBaseline="top",n.font="14px Arial",n.fillText("Device fingerprint",2,2);const t=[navigator.userAgent,navigator.language,screen.width+"x"+screen.height,Intl.DateTimeFormat().resolvedOptions().timeZone,e.toDataURL()].join("|");let s=0;for(let r=0;r<t.length;r++)s=(s<<5)-s+t.charCodeAt(r),s&=s;return Math.abs(s).toString(36)})(),t=localStorage.getItem(`news_viewed_${e}_${n}`),s=Date.now();(!t||s-parseInt(t)>1e4)&&(localStorage.setItem(`news_viewed_${e}_${n}`,s.toString()),w.mutate({newsId:e,deviceFingerprint:n}))},isTracking:w.isPending}};export{w as B,m as u};
