1import{a as m,c as h,r as d,k as a}from"./index-IxvR3ybE.js";const A=()=>{const{currentUser:i}=m(),{toast:r}=h(),[c,s]=d.useState([]),[g,l]=d.useState(!0);d.useEffect(()=>{i?(f(),p()):l(!1)},[i]);const f=async()=>{if(!(i!=null&&i.id)){l(!1);return}try{console.log("Fetching attendee notifications for user:",i.id);const{data:t,error:e}=await a.from("notifications").select("*").eq("user_id",i.id).order("created_at",{ascending:!1}).limit(50);if(e)throw console.error("Error fetching notifications:",e),e;console.log("Attendee notifications fetched:",(t==null?void 0:t.length)||0),s(t||[])}catch(t){console.error("Error in fetchNotifications:",t),r({title:"Error",description:"Failed to load notifications",variant:"destructive"})}finally{l(!1)}},p=()=>{if(!(i!=null&&i.id))return;console.log("Setting up realtime subscription for attendee notifications...");const t=a.channel("attendee-notifications").on("postgres_changes",{event:"INSERT",schema:"public",table:"notifications",filter:`user_id=eq.${i.id}`},e=>{console.log("New notification received:",e);const o=e.new;s(n=>[o,...n]),r({title:o.title,description:o.message}),"Notification"in window&&Notification.permission==="granted"&&new Notification(o.title,{body:o.message,icon:"/logo.png"})}).on("postgres_changes",{event:"UPDATE",schema:"public",table:"notifications",filter:`user_id=eq.${i.id}`},e=>{console.log("Notification updated:",e);const o=e.new;s(n=>n.map(u=>u.id===o.id?o:u))}).subscribe(e=>{console.log("Attendee notifications realtime subscription status:",e)});return()=>{console.log("Cleaning up attendee notifications realtime subscription"),a.removeChannel(t)}};return{notifications:c,loading:g,markAsRead:async t=>{try{const{error:e}=await a.from("notifications").update({is_read:!0}).eq("id",t);if(e)throw e;s(o=>o.map(n=>n.id===t?{...n,is_read:!0}:n))}catch(e){console.error("Error marking notification as read:",e)}},markAllAsRead:async()=>{if(i!=null&&i.id)try{const{error:t}=await a.from("notifications").update({is_read:!0}).eq("user_id",i.id).eq("is_read",!1);if(t)throw t;s(e=>e.map(o=>({...o,is_read:!0}))),r({title:"Success",description:"All notifications marked as read"})}catch(t){console.error("Error marking all notifications as read:",t),r({title:"Error",description:"Failed to mark all notifications as read",variant:"destructive"})}},deleteNotification:async t=>{try{const{error:e}=await a.from("notifications").delete().eq("id",t).eq("user_id",i==null?void 0:i.id);if(e)throw e;s(o=>o.filter(n=>n.id!==t)),r({title:"Success",description:"Notification deleted"})}catch(e){console.error("Error deleting notification:",e),r({title:"Error",description:"Failed to delete notification",variant:"destructive"})}},deleteSelectedNotifications:async t=>{if(!(!(i!=null&&i.id)||t.length===0))try{const{error:e}=await a.from("notifications").delete().in("id",t).eq("user_id",i.id);if(e)throw e;s(o=>o.filter(n=>!t.includes(n.id))),r({title:"Success",description:`${t.length} notification${t.length>1?"s":""} deleted`})}catch(e){console.error("Error deleting notifications:",e),r({title:"Error",description:"Failed to delete selected notifications",variant:"destructive"})}},getUnreadCount:()=>c.filter(t=>!t.is_read).length,getNotificationsByType:t=>c.filter(e=>e.type===t),requestNotificationPermission:async()=>{if("Notification"in window&&Notification.permission==="default"){const t=await Notification.requestPermission();console.log("Notification permission:",t)}},refetch:f}};export{A 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.