1import{r as c,n as C,s as h,j as e,C as T,l as S,D,b as k,c as E,d as A,B as j,A as G,o as N,R as _}from"./index-P0N1Je5T.js";import{P as I}from"./progress-BO5nxn_Q.js";import{u as L,I as w}from"./useContent-DvO-CAeJ.js";import{P}from"./PageHeader-zjaF7toz.js";import{L as R}from"./loader-circle-BQs4OzSl.js";import{F as b}from"./film-DYmoxHh6.js";import{M as F}from"./map-pin-vvbH8j3Q.js";import{C as $}from"./calendar-D19SFwpy.js";import"./useMutation-DJI0oEf-.js";function y(l){const[a,f]=c.useState(null),[g,t]=c.useState(!1),[m,d]=c.useState(!1),o=c.useRef(null),{toast:s}=C(),u=c.useCallback(async()=>{if(!l)return;const{data:r}=await h.from("trailer_jobs").select("id, gallery_id, status, progress, output_url, duration_seconds, error_message, created_at").eq("gallery_id",l).order("created_at",{ascending:!1}).limit(1).maybeSingle();r&&f(r)},[l]);c.useEffect(()=>{if(!a||!["pending","processing"].includes(a.status)){o.current&&(clearInterval(o.current),o.current=null);return}return o.current=setInterval(async()=>{const{data:r}=await h.from("trailer_jobs").select("id, gallery_id, status, progress, output_url, duration_seconds, error_message, created_at").eq("id",a.id).single();if(r){const i=r;f(i),i.status==="done"?s({title:"Trailer generated successfully!",description:"The gallery trailer is ready."}):i.status==="failed"&&s({title:"Trailer generation failed",description:i.error_message||"Please try again.",variant:"destructive"})}},3e3),()=>{o.current&&(clearInterval(o.current),o.current=null)}},[a==null?void 0:a.id,a==null?void 0:a.status,s]),c.useEffect(()=>{u()},[u]);const v=c.useCallback(async()=>{if(l){d(!0);try{const{data:r,error:i}=await h.functions.invoke("generate-trailer",{body:{gallery_id:l}});if(i)throw i;if(r!=null&&r.error){if(r.job_id)s({title:"Trailer already generating",description:"Please wait for it to complete."}),await u();else throw new Error(r.error);return}s({title:"Generating trailer...",description:"AI is creating the trailer storyboard."}),await u()}catch(r){s({title:"Error",description:r.message||"Failed to start trailer generation.",variant:"destructive"})}finally{d(!1)}}},[l,s,u]);return{job:a,loading:g,generating:m,generate:v,refetch:u}}const z=({galleryId:l,galleryTitle:a,open:f,onOpenChange:g})=>{const{job:t,generating:m,generate:d}=y(l),o=(t==null?void 0:t.status)==="pending"||(t==null?void 0:t.status)==="processing",s=(t==null?void 0:t.status)==="done";return e.jsx(D,{open:f,onOpenChange:g,children:e.jsxs(k,{className:"sm:max-w-2xl",children:[e.jsx(E,{children:e.jsxs(A,{children:["Trailer â ",a]})}),!t&&!m&&e.jsxs("div",{className:"flex flex-col items-center gap-4 py-8",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"No trailer generated yet for this gallery."}),e.jsx(j,{onClick:d,children:"Generate AI Trailer"})]}),(m||o)&&e.jsxs("div",{className:"flex flex-col items-center gap-4 py-8",children:[e.jsx(G,{size:96}),e.jsx("p",{className:"text-sm text-muted-foreground",children:m?"Starting generation...":(t==null?void 0:t.status)==="pending"?"Waiting for processing...":"Rendering trailer..."}),e.jsxs("div",{className:"w-full max-w-xs",children:[e.jsx(I,{value:(t==null?void 0:t.progress)||0,className:"h-2"}),e.jsxs("p",{className:"text-center text-xs text-muted-foreground mt-1",children:[(t==null?void 0:t.progress)||0,"%"]})]})]}),s&&(t==null?void 0:t.output_url)&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("video",{src:t.output_url,controls:!0,className:"w-full rounded-lg bg-black aspect-video",preload:"metadata"}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(N,{variant:"secondary",children:t.duration_seconds?`${Math.round(t.duration_seconds)}s`:"Trailer"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(j,{variant:"outline",size:"sm",onClick:d,className:"gap-1.5",children:[e.jsx(_,{className:"h-3.5 w-3.5"}),"Regenerate"]}),e.jsx(j,{size:"sm",asChild:!0,children:e.jsx("a",{href:t.output_url,download:!0,target:"_blank",rel:"noopener noreferrer",children:"Download"})})]})]})]}),(t==null?void 0:t.status)==="failed"&&e.jsxs("div",{className:"flex flex-col items-center gap-4 py-8",children:[e.jsx("p",{className:"text-sm text-destructive",children:t.error_message||"Failed to generate trailer."}),e.jsxs(j,{variant:"outline",onClick:d,className:"gap-1.5",children:[e.jsx(_,{className:"h-3.5 w-3.5"}),"Try Again"]})]})]})})},B=({galleryId:l})=>{const{job:a}=y(l);return a?a.status==="done"?e.jsx(N,{variant:"default",className:"text-[10px] px-1.5 py-0",children:"â Trailer"}):a.status==="pending"||a.status==="processing"?e.jsxs(N,{variant:"secondary",className:"text-[10px] px-1.5 py-0",children:["â³ ",a.progress,"%"]}):a.status==="failed"?e.jsx(N,{variant:"destructive",className:"text-[10px] px-1.5 py-0",children:"Error"}):null:null},V=()=>{const{content:l,loading:a}=L(),[f,g]=c.useState([]),[t,m]=c.useState(!0),[d,o]=c.useState(null);return c.useEffect(()=>{(async()=>
1{if(a)return;const u=[...new Set(l.filter(n=>n.gallery_id).map(n=>n.gallery_id))];if(u.length===0){g([]),m(!1);return}const{data:v}=await h.from("galleries").select("id, title, cover_image_url, date, location").in("id",u).order("created_at",{ascending:!1});if(!v){m(!1);return}const{data:r}=await h.from("gallery_items").select("gallery_id, file_type").in("gallery_id",u),i={};r==null||r.forEach(n=>{var x,p;i[n.gallery_id]||(i[n.gallery_id]={images:0,videos:0}),(x=n.file_type)!=null&&x.startsWith("image/")?i[n.gallery_id].images++:(p=n.file_type)!=null&&p.startsWith("video/")&&i[n.gallery_id].videos++}),g(v.map(n=>{var x,p;return{...n,imageCount:((x=i[n.id])==null?void 0:x.images)||0,videoCount:((p=i[n.id])==null?void 0:p.videos)||0}})),m(!1)})()},[l,a]),t||a?e.jsx("div",{className:"flex items-center justify-center py-20",children:e.jsx(R,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsx(P,{title:"AI Trailers",subtitle:"Select a gallery to create an AI-powered trailer"}),f.length===0?e.jsxs("div",{className:"flex flex-col items-center justify-center py-20 text-center",children:[e.jsx(b,{className:"h-12 w-12 text-muted-foreground mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"No galleries found."})]}):e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4",children:f.map(s=>e.jsxs(T,{className:"cursor-pointer hover:shadow-md transition-shadow group",onClick:()=>o({id:s.id,title:s.title}),children:[e.jsxs("div",{className:"relative aspect-[4/3] bg-muted rounded-t-lg overflow-hidden",children:[s.cover_image_url?e.jsx("img",{src:s.cover_image_url,alt:s.title,className:"w-full h-full object-cover group-hover:scale-105 transition-transform duration-300",loading:"lazy"}):e.jsx("div",{className:"w-full h-full flex items-center justify-center",children:e.jsx(w,{className:"h-10 w-10 text-muted-foreground"})}),e.jsx("div",{className:"absolute top-2 right-2",children:e.jsx(B,{galleryId:s.id})})]}),e.jsxs(S,{className:"p-4 space-y-2",children:[e.jsx("h3",{className:"font-semibold text-sm line-clamp-1",children:s.title}),e.jsxs("div",{className:"flex flex-wrap gap-x-3 gap-y-1 text-xs text-muted-foreground",children:[s.location&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(F,{className:"h-3 w-3"}),s.location]}),s.date&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx($,{className:"h-3 w-3"}),new Date(s.date).toLocaleDateString()]})]}),e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground",children:[e.jsx(w,{className:"h-3 w-3"}),e.jsxs("span",{children:[s.imageCount>0&&`${s.imageCount} photo${s.imageCount!==1?"s":""}`,s.imageCount>0&&s.videoCount>0&&" ⢠",s.videoCount>0&&`${s.videoCount} video${s.videoCount!==1?"s":""}`,s.imageCount===0&&s.videoCount===0&&"0 items"]})]})]})]},s.id))}),d&&e.jsx(z,{galleryId:d.id,galleryTitle:d.title,open:!!d,onOpenChange:s=>!s&&o(null)})]})};export{V as Trailers,V as default};
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.