1import{c as F,o as N,a as Q,p as K,k as a,V as p}from"./index-IxvR3ybE.js";const C=s=>{const{toast:c}=F(),u=N(),{currentUser:e}=Q(),{data:q=[],isLoading:b,error:A}=K({queryKey:["announcements",e==null?void 0:e.id,s],queryFn:async()=>{if(!(e!=null&&e.id))throw new Error("User not authenticated");let n=s;if(!n){console.log("Fetching announcements for admin:",e.id);const{data:o,error:i}=await a.from("profiles").select("current_event_id").eq("id",e.id).single();if(i)throw console.error("Error fetching admin profile:",i),i;if(!(o!=null&&o.current_event_id))return console.log("No current event found for admin"),[];n=o.current_event_id}const{data:t,error:r}=await a.from("announcements").select("*").eq("event_id",n).eq("created_by",e.id).order("created_at",{ascending:!1});if(r)throw console.error("Error fetching announcements:",r),r;return console.log("Announcements fetched:",(t==null?void 0:t.length)||0),t},enabled:!!(e!=null&&e.id)&&(!!s||!0)}),g=async n=>{const t=n.name.split(".").pop(),o=`announcements/${`${Math.random()}.${t}`}`,{error:i}=await a.storage.from("media").upload(o,n);if(i)throw i;const{data:{publicUrl:d}}=a.storage.from("media").getPublicUrl(o);return d},_=async n=>{const t=`voice_notes/${Math.random()}.webm`,{error:r}=await a.storage.from("media").upload(t,n,{contentType:"audio/webm"});if(r)throw r;const{data:{publicUrl:o}}=a.storage.from("media").getPublicUrl(t);return o},v=p({mutationFn:async n=>{if(!(e!=null&&e.id))throw new Error("User not authenticated");let t=s;if(!t){const{data:f,error:P}=await a.from("profiles").select("current_event_id").eq("id",e.id).single();if(P||!(f!=null&&f.current_event_id))throw new Error("No current event found. Please select an event first.");t=f.current_event_id}let r;n.image&&(r=await g(n.image));let o;n.voice_note&&(o=await _(n.voice_note));const{image:i,voice_note:d,...y}=n,l={...y,image_url:r||n.image_url,voice_note_url:o||n.voice_note_url,created_by:e.id,event_id:t};console.log("Creating announcement with data:",l);const{data:m,error:E}=await a.from("announcements").insert([l]).select().single();if(E)throw E;return m},onSuccess:()=>{u.invalidateQueries({queryKey:["announcements",e==null?void 0:e.id,s]}),u.invalidateQueries({queryKey:["attendee-announcements"]}),c({title:"Announcement Created",description:"The announcement has been published successfully."})},onError:n=>{c({title:"Error",description:n.message||"Failed to create announcement. Please try again.",variant:"destructive"}),console.error("Error creating announcement:",n)}}),h=p({mutationFn:async({id:n,image:t,voice_note:r,...o})=>{let i=o.image_url;t&&(i=await g(t));let d=o.voice_note_url;r&&(d=await _(r));const y={...o,image_url:i,voice_note_url:d},{data:l,error:m}=await a.from("announcements").update(y).eq("id",n).eq("created_by",e==null?void 0:e.id).select().single();if(m)throw m;return l},onSuccess:()=>{u.invalidateQueries({queryKey:["announcements",e==null?void 0:e.id,s]}),u.invalidateQueries({queryKey:["attendee-announcements"]}),c({title:"Announcement Updated",description:"The announcement has been updated successfully."})},onError:n=>{c({title:"Error",description:"Failed to update announcement. Please try again.",variant:"destructive"}),console.error("Error updating announcement:",n)}}),w=p({mutationFn:async n=>{const{error:t}=await a.from("announcements").delete().eq("id",n).eq("created_by",e==null?void 0:e.id);if(t)throw t},onSuccess:()=>{u.invalidateQueries({queryKey:["announcements",e==null?void 0:e.id,s]}),u.invalidateQueries({queryKey:["attendee-announcements"]}),c({title:"Announcement Deleted",description:"The announcement has been removed successfully.",variant:"destructive"})},onError:n=>{c({title:"Error",description:"Failed to delete announcement. Please try again.",variant:"destructive"}),console.error("Error deleting announcement:",n)}});return{announcements:q,isLoading:b,error:A,createAnnouncement:v.mutate,updateAnnouncement:h.mutate,deleteAnnouncement:w.mutate,isCreating:v.isPending,isUpdating:h.isPending,isDeleting:w.isPending,uploadImage:g}};export{C as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.