import{g as f,r as k,s as i,t as n,j as e}from"./index-BH-cN4KT.js";import{a as y}from"./useQuery-C5OEng6p.js";import{u as d}from"./Combination-CBlSanWE.js";import{A as v}from"./AdminShell-CWlEEeGv.js";import{S as w,D as N}from"./DeleteButton-YOTxCXVv.js";import{B as o}from"./button-ChRro_zr.js";import{L as m,I as u}from"./index-JcJvmTtT.js";import{S as x}from"./switch-CC7b3Upq.js";import{D as b,a as C,b as q,c as D,d as S}from"./alert-dialog-xhdXOIPZ.js";import{P as E}from"./plus-DV449IX1.js";import"./use-auth-wvxHCDzg.js";import"./index-BqQI1ugW.js";const P={message:"",link:"",priority:0,active:!0};function $(){const l=f(),[r,t]=k.useState(null),c=y({queryKey:["admin","breaking"],queryFn:async()=>{const{data:s,error:a}=await i.from("breaking_news").select("*").order("priority",{ascending:!1}).order("created_at",{ascending:!1});if(a)throw a;return s}}),g=d({mutationFn:async()=>{if(!r?.message?.trim())throw new Error("Message is required");const s={message:r.message.trim(),link:r.link?.trim()||null,priority:Number(r.priority)||0,active:!!r.active};if(r.id){const{error:a}=await i.from("breaking_news").update(s).eq("id",r.id);if(a)throw a}else{const{error:a}=await i.from("breaking_news").insert(s);if(a)throw a}},onSuccess:()=>{n.success(r?.id?"Alert updated":"Alert added"),t(null),l.invalidateQueries({queryKey:["admin","breaking"]})},onError:s=>n.error(s.message)}),p=d({mutationFn:async({id:s,active:a})=>{const{error:h}=await i.from("breaking_news").update({active:a}).eq("id",s);if(h)throw h},onSuccess:()=>l.invalidateQueries({queryKey:["admin","breaking"]})}),j=d({mutationFn:async s=>{const{error:a}=await i.from("breaking_news").delete().eq("id",s);if(a)throw a},onSuccess:()=>{n.success("Deleted"),l.invalidateQueries({queryKey:["admin","breaking"]})},onError:s=>n.error(s.message)});return e.jsxs(v,{title:"Breaking News Ticker",children:[e.jsxs("div",{className:"flex justify-between items-center mb-4",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[c.data?.length??0," alerts"]}),e.jsxs(o,{onClick:()=>t({...P}),children:[e.jsx(E,{className:"w-4 h-4 mr-1"})," New Alert"]})]}),e.jsxs("ul",{className:"space-y-2",children:[c.data?.map(s=>e.jsxs("li",{className:"flex items-center gap-3 bg-surface border border-border p-3 rounded-sm",children:[e.jsx(x,{checked:s.active,onCheckedChange:a=>p.mutate({id:s.id,active:a})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("div",{className:"font-bold truncate",children:s.message}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Priority ",s.priority,s.link&&e.jsxs(e.Fragment,{children:[" · ",e.jsx("span",{className:"text-brand-accent truncate",children:s.link})]})]})]}),e.jsx(o,{variant:"ghost",size:"sm",onClick:()=>t(s),children:e.jsx(w,{className:"w-4 h-4"})}),e.jsx(N,{onDelete:()=>j.mutate(s.id),title:"Delete breaking alert?",description:`Remove "${s.message.slice(0,60)}${s.message.length>60?"…":""}" from the ticker.`})]},s.id)),!c.data?.length&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No breaking news yet."})]}),e.jsx(b,{open:!!r,onOpenChange:s=>!s&&t(null),children:e.jsxs(C,{children:[e.jsx(q,{children:e.jsx(D,{children:r?.id?"Edit alert":"New alert"})}),r&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(m,{children:"Message"}),e.jsx(u,{placeholder:"LILONGWE: ...",value:r.message??"",onChange:s=>t({...r,message:s.target.value})})]}),e.jsxs("div",{children:[e.jsx(m,{children:"Link (optional)"}),e.jsx(u,{placeholder:"https://...",value:r.link??"",onChange:s=>t({...r,link:s.target.value})})]}),e.jsxs("div",{children:[e.jsx(m,{children:"Priority (higher shows first)"}),e.jsx(u,{type:"number",value:r.priority??0,onChange:s=>t({...r,priority:Number(s.target.value)})})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm",children:[e.jsx(x,{checked:!!r.active,onCheckedChange:s=>t({...r,active:s})}),"Active on site"]})]}),e.jsxs(S,{children:[e.jsx(o,{variant:"outline",onClick:()=>t(null),children:"Cancel"}),e.jsx(o,{onClick:()=>g.mutate(),disabled:g.isPending||!r?.message?.trim(),children:"Save"})]})]})})]})}export{$ as component};
