import{u as f}from"./vendor-query-CN1EccJz.js";import{s as o}from"./index-CJqEbhJN.js";function m(t,e,n="popular"){return f({queryKey:["shows",t,e,n],queryFn:async()=>{let a=o.from("shows").select("*");n==="newest"?a=a.order("created_at",{ascending:!1}):n==="rating"?a=a.order("rating",{ascending:!1,nullsFirst:!1}):a=a.order("rating",{ascending:!1,nullsFirst:!1}),t&&(a=a.or(`title_english.ilike.%${t}%,title_romaji.ilike.%${t}%`)),e&&e.length>0&&(a=a.overlaps("genres",e));const{data:r,error:l}=await a.limit(50);if(l)throw l;if(t&&(!r||r.length<5))try{const i=await fetch(`https://api.jikan.moe/v4/anime?q=${encodeURIComponent(t)}&limit=10&order_by=popularity`);if(i.ok){const g=(await i.json()).data.map(s=>({id:`jikan-${s.mal_id}`,mal_id:s.mal_id,title_english:s.title_english||s.title,title_romaji:s.title,title_native:s.title_japanese,poster_url:s.images?.jpg?.large_image_url,banner_url:s.images?.jpg?.large_image_url,synopsis:s.synopsis,genres:s.genres?.map(d=>d.name)||[],status:s.status==="Currently Airing"?"Ongoing":s.status==="Finished Airing"?"Finished":s.status,episodes_count:s.episodes,rating:s.score,slug:null,in_database:!1})),u=[...(r||[]).map(s=>({...s,in_database:!0}))],c=new Set(r?.map(s=>s.mal_id));for(const s of g)c.has(s.mal_id)||u.push(s);return u}}catch(i){console.error("Error fetching from Jikan:",i)}return(r||[]).map(i=>({...i,in_database:!0}))}})}function w(t){return f({queryKey:["show",t],queryFn:async()=>{const{data:e,error:n}=await o.from("shows").select("*").eq("slug",t).maybeSingle();if(e)return e;const{data:a,error:r}=await o.from("shows").select("*").eq("id",t).maybeSingle();return a||null},enabled:!!t})}async function y(){const t=await o.functions.invoke("sync-anime-shows",{method:"POST"});if(t.error)throw t.error;return t.data}export{w as a,y as s,m as u};