import{r as c,z as t,w as m}from"./index-oAPVDf-r.js";const S=s=>{const[o,u]=c.useState([]),[x,d]=c.useState(!0),[E,f]=c.useState(!1),l=c.useCallback(async()=>{if(!s){d(!1);return}d(!0);try{const{data:e,error:a}=await t.from("mission_example_images").select("*").eq("mission_id",s).order("order_index",{ascending:!0});a?(console.warn("useMissionExamples: could not fetch examples",a.message),u([])):u(e||[])}catch(e){console.warn("useMissionExamples: unexpected error",e),u([])}finally{d(!1)}},[s]);c.useEffect(()=>{l()},[l]);const h=async(e,a)=>{if(!s)return m.error("Mission ID saknas"),null;f(!0);try{const r=e.name.split(".").pop(),n=`${s}/${Date.now()}-${Math.random().toString(36).substring(7)}.${r}`,{error:i}=await t.storage.from("mission-examples").upload(n,e,{cacheControl:"3600",upsert:!1});if(i)throw i;const{data:{publicUrl:p}}=t.storage.from("mission-examples").getPublicUrl(n),{data:y,error:g}=await t.from("mission_example_images").insert({mission_id:s,image_url:p,image_type:"good",caption:a||null,order_index:o.length}).select().single();if(g)throw g;return m.success("Exempelbild uppladdad!"),await l(),y}catch(r){return console.error("Upload error:",r),m.error("Kunde inte ladda upp bild: "+r.message),null}finally{f(!1)}},w=async(e,a)=>{try{const r=a.split("/mission-examples/"),n=r[r.length-1];if(n){const{error:p}=await t.storage.from("mission-examples").remove([n]);p&&console.warn("Could not delete from storage:",p)}const{error:i}=await t.from("mission_example_images").delete().eq("id",e);if(i)throw i;m.success("Exempelbild borttagen"),await l()}catch(r){console.error("Delete error:",r),m.error("Kunde inte ta bort bild: "+r.message)}},b=o.filter(e=>e.image_type==="good"),_=o.filter(e=>e.image_type==="bad");return{examples:o,goodExamples:b,badExamples:_,loading:x,uploading:E,hasExamples:o.length>0,uploadExample:h,deleteExample:w,refetch:l}};export{S as u};