import{r}from"./react-vendor-0bvDbF03.js";import{s as _}from"./index-CwGgW-mc.js";const A=e=>{const[f,c]=r.useState([]),[b,i]=r.useState(!0),[y,l]=r.useState(null),[g,t]=r.useState(!0),d=async()=>{try{i(!0),l(null);let o=_.from("blog_posts").select(`
          *,
          author:profiles(username, avatar_url),
          categories:blog_post_categories(
            blog_category:blog_categories(*)
          )
        `).order("published_at",{ascending:!1});e?.status!==void 0?o=o.eq("status",e.status):e&&"status"in e||(o=o.eq("status","published")),e?.limit&&(o=o.limit(e.limit)),e?.offset&&(o=o.range(e.offset,e.offset+(e.limit||10)-1)),e?.search&&(o=o.or(`title.ilike.%${e.search}%,excerpt.ilike.%${e.search}%`));const{data:h,error:n}=await o;if(n)throw n;const m=(h||[]).map(s=>{let a={blocks:[]};if(s.content)try{if(typeof s.content=="string"){const u=JSON.parse(s.content);a=u&&typeof u=="object"&&Array.isArray(u.blocks)?u:{blocks:[]}}else typeof s.content=="object"&&(a=s.content,(!a.blocks||!Array.isArray(a.blocks))&&(a={blocks:[]}))}catch{a={blocks:[]}}return{...s,status:s.status||"draft",content:a,categories:Array.isArray(s.categories)?s.categories.map(u=>u.blog_category).filter(Boolean):[],tags:Array.isArray(s.tags)?s.tags:[],reading_time_minutes:s.reading_time_minutes||0,view_count:s.view_count||0,is_featured:s.is_featured||!1}});if(e?.category&&e.category!=="all"){const s=m.filter(a=>a.categories?.some(u=>u?.slug===e.category));c(e?.offset?a=>[...a,...s]:s),t(s.length===(e.limit||10))}else c(e?.offset?s=>[...s,...m]:m),t(m.length===(e.limit||10))}catch(o){console.error("Error fetching blog posts:",o),l(o instanceof Error?o.message:"Failed to fetch blog posts"),c([])}finally{i(!1)}};return r.useEffect(()=>{d()},[e?.category,e?.search,e?.status,e?.offset]),{posts:f,loading:b,error:y,hasMore:g,refetch:d}},w=e=>{const[f,c]=r.useState(null),[b,i]=r.useState(!0),[y,l]=r.useState(null);return r.useEffect(()=>{(async()=>{if(!e){i(!1);return}try{i(!0),l(null);const{data:t,error:d}=await _.from("blog_posts").select(`
            *,
            author:profiles(username, avatar_url),
            categories:blog_post_categories(
              blog_category:blog_categories(*)
            )
          `).eq("slug",e).eq("status","published").maybeSingle();if(d)throw d;if(t){let o={blocks:[]};if(t.content)try{if(typeof t.content=="string"){const n=JSON.parse(t.content);o=n&&typeof n=="object"&&Array.isArray(n.blocks)?n:{blocks:[]}}else typeof t.content=="object"&&(o=t.content,(!o.blocks||!Array.isArray(o.blocks))&&(o={blocks:[]}))}catch{o={blocks:[]}}const h={...t,status:t.status||"draft",content:o,categories:Array.isArray(t.categories)?t.categories.map(n=>n.blog_category).filter(Boolean):[],tags:Array.isArray(t.tags)?t.tags:[],reading_time_minutes:t.reading_time_minutes||0,view_count:t.view_count||0,is_featured:t.is_featured||!1};c(h),await _.from("blog_posts").update({view_count:(t.view_count||0)+1}).eq("id",t.id)}else c(null)}catch(t){console.error("Error fetching blog post:",t),l(t instanceof Error?t.message:"Failed to fetch blog post"),c(null)}finally{i(!1)}})()},[e]),{post:f,loading:b,error:y}},E=()=>{const[e,f]=r.useState([]),[c,b]=r.useState(!0),[i,y]=r.useState(null),l=async()=>{try{y(null);const{data:g,error:t}=await _.from("blog_categories").select("*").order("name");if(t)throw t;f(g||[])}catch(g){console.error("Error fetching categories:",g),y(g instanceof Error?g.message:"Failed to fetch categories"),f([])}finally{b(!1)}};return r.useEffect(()=>{l()},[]),{categories:e,loading:c,error:i,refetch:l}};export{A as a,w as b,E as u};
//# sourceMappingURL=useBlogData-BtUkmVHs.js.map