1import{b2 as Qt,v as Wt,cf as I,cg as vt,u as q,j as t,av as Le,I as v,r,a6 as Ht,b as jt,c as qe,ch as kt,$ as Ne,F as Oe,b3 as Se,ci as Kt,bL as Nt,bK as _e,i as Yt,e as Jt,h as Xt,ax as Zt,o as en,ay as tn,t as nn,a7 as ln,l as sn,cj as on,ck as an,cl as rn,cm as cn,cn as ct,co as un,cp as dn,cq as mn,cr as ut,cs as dt,aK as pn,bv as xn,ag as ne,aE as bn,y as mt,ct as hn,ah as fn,cu as gn,aw as yn,cv as vn,cw as jn}from"./index-TYap5P-b.js";import{s as kn}from"./funnel-photo-DW9uX1y7.js";import{u as Nn,r as Mn}from"./useResolveTemplateTarget-DlM_qgvX.js";import{S as B}from"./index-J4dgdj_S.js";import{U as pt}from"./UploadDropZone-bjlZORkH.js";async function Fn(e,n){const s=await Qt(`/v1/templates/${e}/${n}/form`);if(s.status!=="ok")throw new Error("Failed to fetch template form");return s.data}const Cn={all:["template-form"],one:(e,n)=>["template-form",e,n]};function In(e,n){return Wt({queryKey:Cn.one(e??"image",n??0),queryFn:()=>Fn(e,n),enabled:e!=null&&n!=null,staleTime:5*6e4,retry:1})}const Pn=["image","text","number","select","choice","aspect","quality"],Tn=["face","face_secondary","outfit","object","product","location","style","motion"];function ke(e){if(typeof e=="string")return e.trim();if(!e||typeof e!="object")return"";const n=I.language?.split("-")[0]??"ru";return([e[n],e.ru,e.en,...Object.values(e)].find(o=>typeof o=="string"&&o.trim())??"").trim()}function le(e){return typeof e=="number"&&Number.isFinite(e)?e:null}function wn(e){return Pn.includes(e)}function zn(e){if(!e||typeof e!="object")return null;const n=Object.entries(e).map(([s,o])=>[s,(Array.isArray(o)?o:[o]).map(String)]).filter(([,s])=>s.length>0);return n.length>0?Object.fromEntries(n):null}const Sn=/^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;function En(e){return(e??[]).filter(n=>typeof n=="object"&&n!==null).filter(n=>typeof n.value=="string"&&n.value.length>0).map(n=>({value:n.value,label:ke(n.label)||n.value,color:typeof n.color=="string"&&Sn.test(n.color)?n.color:null}))}function An(e){if(!e?.key||typeof e.key!="string"||!wn(e.type))return null;const n=e.role&&Tn.includes(e.role)?e.role:null,s=(e.options??[]).filter(a=>typeof a=="string"&&a.length>0),o=e.type==="choice"?En(e.options):[];return e.type==="choice"&&o.length===0?null:{key:e.key,type:e.type,label:ke(e.label),hint:ke(e.hint),required:e.required===!0,defaultValue:e.default!=null?String(e.default):"",visibleIf:zn(e.visible_if),priceDelta:Math.max(0,le(e.price_delta)??0),role:e.type==="image"?n:null,slot:e.type==="image"?le(e.slot):null,example:typeof e.example=="string"?e.example:"",min:le(e.min),max:le(e.max),maxLen:le(e.max_len),regex:typeof e.regex=="string"?e.regex:"",options:s,choices:o,placeholder:ke(e.placeholder)}}function Ln(e){const n=typeof e?.title=="string"?e.title.trim():"",s=(e?.rules??[]).filter(o=>typeof o=="string"&&o.trim()).map(o=>o.trim());return!n&&s.length===0?null:{title:n,rules:s}}function On(e){if(!e||e.schemaVersion<=0)return null;const n=(e.formSchema?.fields??[]).map(An).filter(s=>s!=null);return n.length===0?null:{kind:e.kind,templateId:e.templateId,schemaVersion:e.schemaVersion,instruction:Ln(e.formSchema?.instruction),fields:n}}const _n=new Set(["aspect","quality"]);function R(e){return typeof e=="string"&&e.trim().length>0}function Rn(e){const n={};for(const s of e.fields)R(s.defaultValue)&&(n[s.key]=s.defaultValue);return n}function se(e,n){const s=[],o={};for(const a of e){const d=a.visibleIf;(d==null||Object.entries(d).every(([b,c])=>c.includes(o[b]??"")))&&(s.push(a),R(n[a.key])&&(o[a.key]=n[a.key]))}return s}function ve(e){return e.filter(n=>n.type==="image")}function qn(e){return e.filter(n=>n.type!=="image"&&!_n.has(n.type))}function xt(e,n){return e.find(s=>s.type===n)??null}function Vn(e,n){const s={};for(const o of se(e,n)){const a=n[o.key];R(a)&&(s[o.key]=a)}return s}function Un(e,n){return se(e,n).reduce((s,o)=>R(n[o.key])?s+o.priceDelta:s,0)}function Dn(e,n){return se(e,n).filter(s=>s.required&&!R(n[s.key]))}function $n(e,n){const s=(n??"").trim();if(!s)return e.required?e.type==="image"?I.t("templateForm.errorPhoto"):I.t("templateForm.errorRequired"):"";if(e.type==="number"){const o=Number(s.replace(",","."));return Number.isFinite(o)?e.min!=null&&e.max!=null&&(o<e.min||o>e.max)?I.t("templateForm.errorRange",{min:e.min,max:e.max}):e.min!=null&&o<e.min?I.t("templateForm.errorMin",{min:e.min}):e.max!=null&&o>e.max?I.t("templateForm.errorMax",{max:e.max}):"":I.t("templateForm.errorNumber")}if(e.type==="text"){if(e.maxLen!=null&&s.length>e.maxLen)return I.t("templateForm.errorMaxLen",{count:e.maxLen});if(e.regex)try{if(!new RegExp(e.regex).test(s))return I.t("templateForm.errorFormat")}catch{}}return(e.type==="select"||e.type==="aspect"||e.type==="quality")&&e.options.length>0&&!e.options.includes(s)?I.t("templateForm.errorOption"):e.type==="choice"&&!e.choices.some(o=>o.value===s)?I.t("templateForm.errorOption"):""}function Gn(e,n){const s={};for(const o of se(e,n)){const a=$n(o,n[o.key]);a&&(s[o.key]=a)}return s}function Mt(){try{const e=localStorage.getItem(vt),n=e?JSON.parse(e):null;return Array.isArray(n)?n.filter(s=>typeof s=="string"):[]}catch{return[]}}function Ft(e,n){return`${e}:${n}`}function Bn(e,n){return Mt().includes(Ft(e,n))}function Qn(e,n){const s=Ft(e,n),o=Mt();if(!o.includes(s))try{localStorage.setItem(vt,JSON.stringify([...o,s]))}catch{}}const bt="h-9 w-full rounded-[10px] border border-sidebar-border bg-card-bg/80 px-2.5 text-sm text-text-color outline-none transition-colors placeholder:text-subtitle-text-color focus:border-primary",ht="border-red focus:border-red",Wn="[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none";function Hn({label:e,choices:n,value:s,onSelect:o}){return t.jsx("div",{className:"flex flex-wrap gap-1.5",role:"radiogroup","aria-label":e,children:n.map(a=>{const d=a.value===s;return t.jsxs("button",{type:"button",role:"radio","aria-checked":d,onClick:()=>
1o(a.value),className:["flex grow cursor-pointer items-center justify-center gap-1.5 rounded-[10px] border px-2.5 py-2 text-xs font-medium transition-colors active:scale-[0.98]",d?"border-primary bg-primary/10 text-text-color":"border-sidebar-border bg-card-bg/80 text-subtitle-text-color hover:text-text-color"].join(" "),children:[a.color?t.jsx("span",{"aria-hidden":!0,className:"size-3 shrink-0 rounded-full ring-1 ring-text-color/25",style:{backgroundColor:a.color}}):null,a.label]},a.value)})})}function Kn(e,n){if(e.type!=="number")return"";const{min:s,max:o}=e;return s!=null&&o!=null?n("templateForm.numberRange",{min:s,max:o}):s!=null?n("templateForm.numberFrom",{min:s}):o!=null?n("templateForm.numberUpTo",{max:o}):n("templateForm.numberExample")}function Yn({fields:e,values:n,errors:s,openKey:o,onOpenChange:a,onChange:d,registerInput:p}){const{t:b}=q();return e.length===0?null:t.jsx("div",{className:"flex flex-col gap-2",children:e.map(c=>{const g=n[c.key]??"",y=s[c.key]??"",C=`detail:${c.key}`,S=c.placeholder||Kn(c,b);return t.jsxs("div",{className:"flex flex-col gap-1",children:[t.jsxs("span",{className:"text-xs font-medium leading-tight text-text-color",children:[c.label,c.required?t.jsx("span",{className:"text-primary",children:" *"}):null]}),c.type==="select"?t.jsx(Le,{fullWidth:!0,swallowOutsideClick:!0,menuPlacement:"above",open:o===C,onOpenChange:f=>a(f?C:null),triggerText:g||b("templateForm.choose"),options:c.options.map(f=>({value:f,label:f})),value:g,onValueChange:f=>d(c.key,f),ariaLabel:c.label}):c.type==="choice"?t.jsx(Hn,{label:c.label,choices:c.choices,value:g,onSelect:f=>d(c.key,f)}):c.type==="number"?t.jsx("input",{ref:f=>p(c.key,f),type:"number",inputMode:"numeric",className:`${bt} ${Wn} ${y?ht:""}`,value:g,placeholder:S,min:c.min??void 0,max:c.max??void 0,"aria-label":c.label,onChange:f=>d(c.key,f.target.value)}):t.jsx("input",{ref:f=>p(c.key,f),type:"text",className:`${bt} ${y?ht:""}`,value:g,placeholder:S,maxLength:c.maxLen??void 0,"aria-label":c.label,onChange:f=>d(c.key,f.target.value)}),y?t.jsx("p",{className:"text-[10px] leading-tight text-red",children:y}):c.hint?t.jsx("p",{className:"text-[10px] leading-tight text-subtitle-text-color",children:c.hint}):null]},c.key)})})}function ft({title:e,onClose:n,footer:s,children:o}){const{t:a}=q();return t.jsx("div",{className:"absolute inset-0 z-20 flex flex-col justify-end bg-black/50 animate-[fadeIn_0.15s_ease-out]",onClick:n,children:t.jsxs("div",{className:"flex max-h-full min-h-0 flex-col overflow-hidden rounded-t-[20px] border-t border-sidebar-border bg-bg-color animate-[slideUp_0.2s_ease-out]",onClick:d=>d.stopPropagation(),children:[t.jsxs("div",{className:"flex shrink-0 items-center justify-between gap-3 px-4 pb-2 pt-4",children:[t.jsx("h4",{className:"min-w-0 text-base font-semibold leading-tight text-text-color",children:e}),t.jsx("button",{type:"button",onClick:n,"aria-label":a("common.close"),className:"grid size-7 shrink-0 place-items-center rounded-full border border-sidebar-border text-subtitle-text-color transition-colors hover:bg-bg-hover",children:t.jsx(v,{name:"closeIcon",className:"size-3.5"})})]}),t.jsx("div",{className:"min-h-0 flex-1 overflow-y-auto px-4 pb-2",children:o}),s?t.jsx("div",{className:"shrink-0 px-4 pb-4 pt-3",children:s}):t.jsx("div",{className:"h-2 shrink-0"})]})})}const Jn=r.memo(function({onPrev:n,onNext:s,hasPrev:o,hasNext:a,prevLabel:d,nextLabel:p}){if(!o&&!a)return null;const b="grid size-9 shrink-0 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition active:scale-95",c=g=>g?"cursor-pointer hover:bg-black/75":"cursor-not-allowed opacity-35";return t.jsxs("div",{className:"absolute right-3 top-1/2 z-10 flex -translate-y-1/2 flex-col gap-2",children:[t.jsx("button",{type:"button",onClick:n,disabled:!o,"aria-label":d,className:`${b} ${c(o)}`,children:t.jsx(v,{name:"commonArrow",className:"size-4 rotate-180","aria-hidden":!0})}),t.jsx("button",{type:"button",onClick:s,disabled:!a,"aria-label":p,className:`${b} ${c(a)}`,children:t.jsx(v,{name:"commonArrow",className:"size-4","aria-hidden":!0})})]})}
1),Xn={face:"faceApp",face_secondary:"usersIcon",outfit:"createRefIcon",object:"clipIcon",product:"PictureIcon",location:"globeIcon",style:"starsIcon",motion:"MotionIcon"};function Re(e){return e?Xn[e]??"PictureIcon":"PictureIcon"}function Zn({field:e,value:n,loading:s,error:o,onOpen:a,onRemove:d}){const{t:p}=q(),b=n.length>0;return t.jsxs("div",{className:"flex min-w-0 flex-col gap-1",children:[t.jsx("div",{className:"relative min-h-0 flex-1",children:b?t.jsxs(t.Fragment,{children:[t.jsx("button",{type:"button",onClick:a,className:"size-full overflow-hidden rounded-xl bg-bg-secondary","aria-label":e.label,children:t.jsx("img",{src:n,alt:"",className:"size-full object-cover"})}),t.jsx("span",{className:"pointer-events-none absolute inset-x-0 bottom-0 truncate rounded-b-xl bg-black/55 px-1.5 py-1 text-[10px] leading-none text-white backdrop-blur",children:e.label}),t.jsx("button",{type:"button",onClick:d,className:"absolute right-1 top-1 grid size-6 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition hover:bg-black/70","aria-label":p("templateModal.removeFile"),children:t.jsx(v,{name:"closeIcon",className:"size-3"})})]}):s?t.jsx("div",{className:"grid size-full place-items-center rounded-xl border border-dashed border-(--sidebar-border) bg-bg-secondary/60",children:t.jsx(B,{className:"size-5 border-2"})}):t.jsxs("button",{type:"button",onClick:a,className:["flex size-full flex-col items-center justify-center gap-1 rounded-xl border border-dashed bg-bg-secondary/60 px-1.5 py-2 text-center transition-colors hover:bg-bg-hover",o?"border-red text-red":"border-(--sidebar-border) text-subtitle-text-color hover:text-text-color"].join(" "),children:[t.jsx(v,{name:Re(e.role),className:"size-5 shrink-0","aria-hidden":!0}),t.jsx("span",{className:"line-clamp-2 text-[11px] font-medium leading-tight",children:e.label}),t.jsx("span",{className:["rounded-full px-1.5 py-px text-[9px] leading-[1.4]",e.required?"bg-primary/15 text-primary":"bg-bg-hover text-subtitle-text-color"].join(" "),children:e.required?p("templateForm.required"):p("templateForm.optional")})]})}),o?t.jsx("p",{className:"px-0.5 text-[10px] leading-tight text-red",children:o}):null]})}let gt=!1;const el=r.memo(function({src:n,className:s}){const{t:o}=q(),a=r.useRef(null),[d,p]=r.useState(gt);r.useEffect(()=>{const c=a.current;if(!c)return;let g=!1;return c.muted=d,c.play().catch(y=>{g||d||!(y instanceof DOMException)||y.name!=="NotAllowedError"||p(!0)}),()=>{g=!0}},[n,d]);const b=()=>{const c=!d;gt=c;const g=a.current;g&&(g.muted=c,c||g.play().catch(()=>{})),p(c)};return t.jsxs(t.Fragment,{children:[t.jsx("video",{ref:a,src:n,className:s,loop:!0,playsInline:!0}),t.jsx("button",{type:"button",onClick:b,"aria-label":o(d?"templateModal.soundOn":"templateModal.soundOff"),className:"absolute bottom-3 right-3 z-10 grid size-9 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition hover:bg-black/75 active:scale-95",children:t.jsx(v,{name:d?"soundOffIcon":"soundOnIcon",className:"size-4","aria-hidden":!0})})]})}),tl=30,nl=100,je="image/jpeg,image/png,image/webp",ll="video/mp4,video/quicktime,video/webm",Ee="w-full",Ae="max-h-64 w-full object-contain",yt="grid auto-rows-[8.5rem] grid-cols-2 gap-2",sl="flex h-11 w-full items-center gap-2 rounded-xl border border-sidebar-border bg-bg-secondary px-3 text-left text-sm font-medium leading-tight text-text-color transition-colors hover:bg-bg-hover";function ol({attachment:e,loading:n,label:s,accept:o,onFile:a,onRemove:d,removeLabel:p}){return e?t.jsxs("div",{className:"relative size-full overflow-hidden rounded-xl bg-bg-secondary",children:[t.jsx("img",{src:e.dataUrl,alt:"",className:"size-full object-cover"}),t.jsx("button",{type:"button",onClick:d,className:"absolute right-1 top-1 grid size-6 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition hover:bg-black/70","aria-label":p,children:t.jsx(v,{name:"closeIcon",className:"size-3"})})]}):n?t.jsx("div",{className:"grid size-full place-items-center rounded-xl border border-dashed border-(--sidebar-border) bg-bg-secondary/60",children:t.jsx(B,{className:"size-5 border-2"})}):t.jsxs("label",{className:"flex size-full cursor-pointer flex-col items-center justify-center gap-1 rounded-xl border border-dashed border-(--sidebar-border) bg-bg-secondary/60 px-1 text-subtitle-text-color transition-colors hover:bg-bg-hover hover:text-text-color",children:[t.jsx("input",{type:"file",accept:o,className:"sr-only",onChange:b=>{const c=b.target.files?.[0];b.target.value="",c&&a(c)}}),t.jsx(v,{name:"commonPlus",className:"size-4 shrink-0","aria-hidden":!0}),t.jsx("span",{className:"truncate text-[11px] leading-none",children:s})]})}const hl=({target:e,funnelKey:n,initialAttachment:s,siblings:o})=>(Ht(),e.kind==="ref"?t.jsx(al,{refTarget:e,funnelKey:n}):t.jsx(Ct,{target:e,funnelKey:n,initialAttachment:s,siblings:o}));function Ct({target:e,funnelKey:n,initialAttachment:s,siblings:o}){const a=qe(),d=Se(e),p=o?.length?o:[e],[b,c]=r.useState(()=>{const E=p.findIndex(w=>Se(w)===d);return E>=0?E:0}),[g,y]=r.useState(s),C=p[b]??e,S=r.useMemo(()=>n?null:kt(C),[n,C]);Kt(S);const f=r.useCallback(E=>c(w=>Math.min(p.length-1,Math.max(0,w+E))),[p.length]),oe=r.useCallback(()=>{a(Ne({name:"TemplateGenerationModal"}))},[a]);return t.jsx(It,{onClose:oe,title:_e(C),preview:Nt(C),steps:{hasPrev:b>0,hasNext:b<p.length-1,onPrev:()=>f(-1),onNext:()=>f(1)},children:t.jsx(rl,{target:C,funnelKey:n,initialAttachment:g,onAttachmentChange:y},Se(C))})}function al({refTarget:e,funnelKey:n}){const s=jt(),o=qe(),a=r.useMemo(()=>kt(e),[e]),{target:d,notFound:p}=Nn(a);return r.useEffect(()=>{if(!p)return;const b=n?Oe[n]:void 0;o(Ne({name:"TemplateGenerationModal"})),s(b?.ownsApp?b.chatPath:e.mediaKind==="image"?"/image-create":"/video-create")},[p,o,s,e.mediaKind,n]),d?t.jsx(Ct,{target:d,funnelKey:n}):t.jsx(It,{onClose:()=>o(Ne({name:"TemplateGenerationModal"})),children:t.jsx("div",{className:"grid min-h-40 flex-1 place-items-center",children:t.jsx(B,{className:"size-7 border-2"})})})}function rl({target:e,funnelKey:n,initialAttachment:s,onAttachmentChange:o}){const{t:a}=q(),d=qe(),p=jt(),b=Jt(),{impact:c}=Xt(),g=Zt(),y=en(),{data:C,isSuccess:S}=tn(),{data:f}=nn(),{showPaywall:oe,trialActive:E}=ln(),w=n!=null,Pt=n?Oe[n]:void 0,Tt=e.kind,wt=e.kind==="pipeline"?e.pipeline.id:e.template.id,Me=In(Tt,wt),zt=r.useMemo(()=>On(Me.data),[Me.data]),St=Me.isPending,[ae,re]=r.useState(s?[s]:[]),[j,Ve]=r.useState({}),[ie,Ue]=r.useState({}),[O,A]=r.useState(null),Fe=r.useRef({}),De=r.useRef({}),[ce,$e]=r.useState(!1),[Ge,Be]=r.useState(null),[Qe,We]=r.useState(!1),Q=r.useCallback(()=>{d(Ne({name:"TemplateGenerationModal"}))},[d]),Ce=r.useCallback(l=>{d(sn({type:"error",title:a("common.error"),subtitle:l}))}
1,[d,a]),W=on(e),H=C?.balance??0,He=f?.status==="active",ue=w&&!He&&S,K=r.useMemo(()=>{const l=an(e,g);return ue?rn(l,H):l},[e,g,ue,H]),[Ke,Et]=r.useState(null),x=r.useMemo(()=>K.find(l=>l.id===Ke)??K[0]??null,[K,Ke]),At=(x?.inputSchema?.mode??"")==="motion",u=e.kind==="video"&&At?null:zt,[Y,Ye]=r.useState(""),[V,Je]=r.useState(""),[de,J]=r.useState(null),L=r.useMemo(()=>u?se(u.fields,j):[],[u,j]),U=r.useMemo(()=>ve(L),[L]),Ie=r.useMemo(()=>qn(L),[L]),me=r.useMemo(()=>xt(L,"aspect"),[L]),pe=r.useMemo(()=>xt(L,"quality"),[L]),Xe=r.useRef(!1);r.useEffect(()=>{if(!u||Xe.current)return;Xe.current=!0;const l=Rn(u),i=ve(u.fields)[0];s?.type==="image"&&i&&(l[i.key]=s.dataUrl),Ve(l)},[u,s]),r.useEffect(()=>{u?.instruction&&(Bn(u.kind,u.templateId)||(Qn(u.kind,u.templateId),A({kind:"instruction"})))},[u]);const D=r.useCallback((l,i)=>{Ve(m=>({...m,[l]:i})),Ue(m=>{if(!m[l])return m;const h={...m};return delete h[l],h})},[]),z=r.useMemo(()=>Nt(e),[e]),xe=cn(z.url,z.type),$=r.useMemo(()=>{if(!ue||!x||e.kind!=="image")return null;const l=ct(e,x,xe);if(l.kind!=="image")return null;const i=Y||l.size,m=x.options??{};return un(x,i,m.qualities??[],H)},[ue,x,e,Y,H,xe]),N=r.useMemo(()=>{if(!x)return null;const l=dn(e,x),i=$?$.length>1?$:[]:l.qualities,m=(h,G)=>{if(G.length===0)return h;const it=h.filter(Bt=>G.includes(Bt));return it.length>0?it:h};return{...l,aspects:m(l.aspects,me?.options??[]),qualities:m(i,pe?.options??[])}},[e,x,$,me,pe]),k=r.useMemo(()=>{if(!x)return null;const l=ct(e,x,xe),i=(m,h)=>m&&R(m.defaultValue)&&h.includes(m.defaultValue)?m.defaultValue:"";if(l.kind==="image"){const m=$,h=m?(V&&m.includes(V)?V:m[0])??l.quality:V||i(pe,N?.qualities??[])||l.quality;return{...l,size:Y||i(me,N?.aspects??[])||l.size,quality:h}}return{...l,aspectRatio:Y||l.aspectRatio,resolution:V||l.resolution}},[e,x,Y,V,$,me,pe,N,xe]),Ze=k?k.kind==="image"?k.size:k.aspectRatio:"",et=k?k.kind==="image"?k.quality:k.resolution:"",be=r.useMemo(()=>x&&k?mn(e,x,k):null,[e,x,k]),he=r.useMemo(()=>u?Un(u.fields,j):0,[u,j]),P=be!=null?be+he:null,T=r.useMemo(()=>ut(e,x),[e,x]),X=dt(e,x,T),Lt=X>1?Math.min(Math.max(2,ae.length+1),X):1,Ot=r.useMemo(()=>U.map(l=>j[l.key]??"").filter(R).map(l=>({dataUrl:l,type:"image"})),[U,j]),tt=u?Ot:ae,M=tt[0]??null;r.useEffect(()=>{o?.(M??void 0)},[M,o]),r.useEffect(()=>{re(l=>l.every(i=>i.type===T)?l:l.filter(i=>i.type===T))},[T]);const _t=r.useCallback(l=>{if(Et(l.id),Ye(""),Je(""),u)return;const i=ut(e,l),m=dt(e,l,i);re(h=>h.filter(G=>G.type===i).slice(0,m))},[e,u]),Rt=r.useCallback(l=>{const i=W==="image"?pn(l):xn(l);return i!=null?ne(i):"â"},[W]),Z=y&&P!=null&&H<P,Pe=T==="video"?nl:tl,Te=!w||y,qt=K.length>0&&(!Pt?.hideModelPicker||He),fe=r.useCallback(async(l,i)=>{if(l.size>Pe*1024*1024)return Ce(a("templateModal.fileTooLarge",{max:Pe})),null;$e(!0),Be(i);try{return await bn(l)}catch{return Ce(a("templateModal.readError")),null}finally{$e(!1),Be(null)}},[Pe,Ce,a]),nt=r.useCallback(async l=>{const i=await fe(l,null);if(i==null)return;const m={dataUrl:i,type:T};re(h=>h.length>=X?h:[...h,m])},[T,X,fe]),lt=r.useCallback(async(l,i)=>{const m=await fe(i,l);m!=null&&D(l,m)},[fe,D]),st=r.useCallback(l=>{re(i=>i.filter((m,h)=>h!==l))},[]),ge=r.useCallback(()=>{if(!n){Q(),oe();return}d(mt({name:"PhotoTrialModal",data:{funnelKey:n,photoUrl:M?.dataUrl,templateUrl:z.url,templateType:z.type}}))},[M,Q,d,n,z,oe]);r.useEffect(()=>{!Qe||!y||!S||P==null||(We(!1),(Z||W==="video"&&E)&&ge())},[Qe,y,S,P,Z,W,E,ge]);const Vt=()=>{if(c("medium"),!y){if(w){M?.type==="image"&&kn(M.dataUrl,e),We(!0),d(mt({name:"AuthModal",data:{variant:"photoUploaded"}}));return}Q(),p("/login");return}if(W==="video"&&E){ge();return}if(Z){ge();return}if(!x||!k)return;if(u){const i=Gn(u.fields,j);if(Object.keys(i).length>0){Ue(i);const m=Ie.find(G=>i[G.key]),h=m?De.current[m.key]:null;h?(h.scrollIntoView({block:"center",behavior:"smooth"}),h.focus()):m&&J(`detail:${m.key}`);return}}else if(!M)return;Mn(vn(e,x,tt,k,u?{values:Vn(u.fields,j),schemaVersion:u.schemaVersion,priceDelta:he}:void 0)),Q();const l=jn(e,n?Oe[n].templateBasePath:void 0);b.pathname!==l&&p(l)},ot=M!=null&&!ce,we=r.useMemo(()=>u?Dn(u.fields,j):[],[u,j]),at=u?x!=null&&!ce&&we.length===0:x!=null&&ot;let ee;w?ee=y?at:ot:y?ee=Z||at:ee=!0;
1const ye=Z&&!w,te=u&&we.length>0?we[0]:null,rt=ye?a("templateModal.topup"):te?te.type==="image"?a("templateForm.needPhoto"):a("templateForm.needField",{field:te.label}):a("templateModal.generate"),ze=u!=null&&!ye&&!te&&Te&&P!=null&&P!=null?ne(P):"",Ut=r.useCallback(l=>{l.example||l.hint?A({kind:"photo",key:l.key}):Fe.current[l.key]?.click()},[]),Dt=t.jsxs("div",{className:"shrink-0 border-t border-sidebar-border px-4 pb-4 pt-3",children:[!u&&Te&&P!=null?t.jsx("p",{className:"mb-2 text-center text-sm text-subtitle-text-color",children:a("templateModal.cost",{count:Math.round(hn(P)),amount:ne(P)})}):g!=null&&x==null?t.jsx("p",{className:"mb-2 text-center text-sm text-red",children:a("templateModal.unavailable")}):null,t.jsx("button",{type:"button",onClick:Vt,disabled:!ee,className:["btn-shine flex h-12 w-full items-center justify-center gap-2 rounded-[12px] text-base font-semibold transition-all",ye?"bg-red text-on-primary":"bg-primary text-on-primary",ee?"active:scale-[0.99]":"cursor-not-allowed opacity-60"].join(" "),children:t.jsxs("span",{className:"flex items-center gap-2",children:[ye||te?null:t.jsx(v,{name:"createGenerateIcon",className:"size-5 shrink-0","aria-hidden":!0}),rt,ze?t.jsxs("span",{className:"inline-flex items-center gap-1",children:["· ",ze,t.jsx(fn,{className:"size-3.5 shrink-0 text-on-primary"})]}):null]},`${rt}|${ze}`)}),u&&Te&&he>0&&be!=null?t.jsx("p",{className:"mt-2 text-center text-xs text-subtitle-text-color",children:a("templateForm.priceBreakdown",{base:ne(be),extra:ne(he)})}):null]});let _;if(St)_=t.jsx("div",{className:`${Ee} grid h-40 place-items-center rounded-2xl bg-bg-secondary/60`,children:t.jsx(B,{className:"size-6 border-2"})});else if(u&&U.length>1)_=t.jsx("div",{className:yt,children:U.map(l=>t.jsx(Zn,{field:l,value:j[l.key]??"",loading:Ge===l.key,error:ie[l.key]??"",onOpen:()=>Ut(l),onRemove:()=>D(l.key,"")},l.key))});else if(u&&U.length===1){const l=U[0],i=j[l.key]??"";_=t.jsx("div",{className:Ee,children:i?t.jsxs("div",{className:"relative overflow-hidden rounded-2xl border border-sidebar-border bg-bg-secondary",children:[t.jsx("img",{src:i,alt:"",className:Ae}),t.jsx("button",{type:"button",onClick:()=>
1D(l.key,""),className:"absolute right-2 top-2 grid size-8 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition hover:bg-black/70","aria-label":a("templateModal.removeFile"),children:t.jsx(v,{name:"closeIcon",className:"size-3.5"})})]}):Ge===l.key?t.jsx("div",{className:"grid h-40 place-items-center rounded-2xl border border-dashed border-(--sidebar-border) bg-bg-secondary/60",children:t.jsx(B,{className:"size-6 border-2"})}):t.jsx(pt,{variant:"panel",className:"max-w-none py-8",accept:je,icon:Re(l.role),onFile:m=>{lt(l.key,m)},title:l.label||a("templateModal.uploadTitle"),hint:l.hint||void 0})})}else u?_=null:X>1?_=t.jsx("div",{className:yt,children:Array.from({length:Lt},(l,i)=>t.jsx(ol,{attachment:ae[i]??null,loading:ce&&i===ae.length,label:a("templateModal.photoSlot",{number:i+1}),accept:je,onFile:nt,onRemove:()=>st(i),removeLabel:a("templateModal.removeFile")},i))}):_=t.jsx("div",{className:Ee,children:M?t.jsxs("div",{className:"relative overflow-hidden rounded-2xl border border-sidebar-border bg-bg-secondary",children:[M.type==="video"?t.jsx("video",{src:M.dataUrl,className:Ae,muted:!0,playsInline:!0,loop:!0,autoPlay:!0}):t.jsx("img",{src:M.dataUrl,alt:"",className:Ae}),t.jsx("button",{type:"button",onClick:()=>st(0),className:"absolute right-2 top-2 grid size-8 place-items-center rounded-full bg-black/55 text-white backdrop-blur transition hover:bg-black/70","aria-label":a("templateModal.removeFile"),children:t.jsx(v,{name:"closeIcon",className:"size-3.5"})})]}):ce?t.jsx("div",{className:"grid h-40 place-items-center rounded-2xl border border-dashed border-(--sidebar-border) bg-bg-secondary/60",children:t.jsx(B,{className:"size-6 border-2"})}):t.jsx(pt,{variant:"panel",className:"max-w-none py-8",accept:T==="video"?ll:je,onFile:l=>{nt(l)},title:a(T==="video"?"templateModal.uploadVideo":"templateModal.uploadTitle"),hint:T==="video"?void 0:a("templateModal.uploadHint")})});const $t=(e.kind==="pipeline"?e.pipeline.category:e.template.category)?.title||x?.name||x?.model||"",F=r.useMemo(()=>!u||!O||O.kind==="instruction"?null:u.fields.find(l=>l.key===O.key)??null,[u,O]),Gt=(()=>{if(!u||!O)return null;if(O.kind==="instruction"&&u.instruction){const l=ve(u.fields).filter(i=>i.example);return t.jsxs(ft,{title:u.instruction.title||_e(e),onClose:()=>A(null),footer:t.jsx("button",{type:"button",onClick:()=>A(null),className:"h-11 w-full rounded-[12px] bg-primary text-sm font-semibold text-on-primary transition-all active:scale-[0.99]",children:a("templateForm.gotIt")}),children:[l.length>0?t.jsx("div",{className:"mb-3 flex gap-2 overflow-x-auto pb-1",children:l.map(i=>t.jsxs("figure",{className:"w-24 shrink-0",children:[t.jsx("img",{src:i.example,alt:"",className:"h-28 w-full rounded-xl bg-bg-secondary object-cover"}),t.jsx("figcaption",{className:"mt-1 text-center text-[10px] leading-tight text-subtitle-text-color",children:i.label})]},i.key))}):null,t.jsx("ul",{className:"flex flex-col gap-1.5",children:u.instruction.rules.map(i=>t.jsxs("li",{className:"flex gap-2 text-sm leading-snug text-text-color",children:[t.jsx(v,{name:"CheckIcon",className:"mt-0.5 size-3.5 shrink-0 text-primary","aria-hidden":!0}),t.jsx("span",{children:i})]},i))})]})}if(O.kind==="photo"&&F){const l=j[F.key]??"";return t.jsx(ft,{title:F.label,onClose:()=>A(null),children:t.jsxs("div",{className:"flex flex-col gap-3 pb-1",children:[F.example?t.jsx("img",{src:F.example,alt:"",className:"max-h-40 w-full rounded-xl bg-bg-secondary object-contain"}):null,F.hint?t.jsx("p",{className:"text-sm leading-snug text-subtitle-text-color",children:F.hint}):null,ie[F.key]?t.jsx("p",{className:"text-xs leading-tight text-red",children:ie[F.key]}):null,t.jsxs("div",{className:"flex gap-2",children:[t.jsxs("button",{type:"button",onClick:()=>Fe.current[F.key]?.click(),className:"flex h-11 flex-1 items-center justify-center gap-2 rounded-[12px] bg-primary text-sm font-semibold text-on-primary transition-all active:scale-[0.99]",children:[t.jsx(v,{name:Re(F.role),className:"size-4 shrink-0","aria-hidden":!0}),a(l?"templateForm.replacePhoto":"templateForm.choosePhoto")]}),l?t.jsx("button",{type:"button",onClick:()=>{D(F.key,""),A(null)},className:"h-11 rounded-[12px] border border-sidebar-border px-4 text-sm font-medium text-subtitle-text-color transition-colors hover:bg-bg-hover",children:a("templateModal.removeFile")}):null]})]})})}return null})();return t.jsxs(il,{onClose:Q,footer:Dt,overlay:Gt,headerAction:u?.instruction?t.jsxs("button",{type:"button",onClick:()=>A({kind:"instruction"}),className:"flex h-8 shrink-0 items-center gap-1 rounded-full border border-sidebar-border px-2.5 text-xs font-medium text-subtitle-text-color transition-colors hover:bg-bg-hover hover:text-text-color",children:[t.jsx(v,{name:"infoIcon",className:"size-3.5 shrink-0","aria-hidden":!0}),a("templateForm.instruction")]}
1):null,children:[u?ve(u.fields).map(l=>t.jsx("input",{ref:i=>{Fe.current[l.key]=i},type:"file",accept:je,className:"sr-only",onChange:i=>{const m=i.target.files?.[0];i.target.value="",m&&(lt(l.key,m),A(h=>h?.kind==="photo"?null:h))}},l.key)):null,t.jsxs("section",{className:"flex items-center gap-3 rounded-2xl border border-sidebar-border bg-bg-secondary p-3",children:[t.jsx("span",{className:"size-12 shrink-0 overflow-hidden rounded-xl bg-bg-hover",children:z.url?z.type==="video"?t.jsx("video",{src:z.url,className:"size-full object-cover",muted:!0,playsInline:!0,preload:"metadata"}):t.jsx("img",{src:z.url,alt:"",className:"size-full object-cover"}):null}),t.jsxs("span",{className:"min-w-0 flex-1",children:[t.jsx("span",{className:"block truncate text-[17px] font-semibold leading-tight text-text-color",children:_e(e)}),t.jsx("span",{className:"mt-0.5 block truncate text-[13px] text-subtitle-text-color",children:$t})]})]}),_,u&&Ie.length>0?t.jsx(Yn,{fields:Ie,values:j,errors:ie,openKey:de,onOpenChange:J,onChange:D,registerInput:(l,i)=>{De.current[l]=i}}):null,qt?t.jsx(gn,{models:K,selectedModel:x,open:de==="model",onOpenChange:l=>J(l?"model":null),onSelectModel:_t,getMinPriceLabel:Rt,placement:"above",triggerClassName:sl}):null,N&&(N.aspects.length>0||N.qualities.length>0)?t.jsxs("div",{className:"flex items-center gap-2",children:[N.aspects.length>0?t.jsx(Le,{fullWidth:!0,swallowOutsideClick:!0,menuPlacement:"above",open:de==="aspect",onOpenChange:l=>J(l?"aspect":null),triggerText:N.aspectLabel(Ze),options:N.aspects.map(l=>({value:l,label:N.aspectLabel(l)})),value:Ze,onValueChange:Ye,ariaLabel:a("composer.aspectRatio"),renderOptionLeading:l=>t.jsx(yn,{label:l.label})}):null,N.qualities.length>0?t.jsx(Le,{fullWidth:!0,swallowOutsideClick:!0,menuPlacement:"above",open:de==="quality",onOpenChange:l=>J(l?"quality":null),triggerText:et,options:N.qualities.map(l=>({value:l,label:l})),value:et,onValueChange:Je,ariaLabel:a("composer.quality")}):null]}):null]})}function It({children:e,onClose:n,title:s,preview:o,steps:a}){const{t:d}=q(),p=Yt()?"pt-[calc(0.5rem+var(--app-safe-area-inset-top)+24px)]":"pt-[calc(0.5rem+var(--app-safe-area-inset-top))]";return t.jsx("div",{className:`fixed inset-0 z-1000 flex items-center justify-center bg-black/60 p-2 pb-[calc(0.5rem+var(--app-safe-area-inset-bottom))] backdrop-blur-lg animate-[fadeIn_0.2s_ease-out] md:p-8 md:pb-8 md:pt-8 ${p}`,onClick:n,role:"dialog","aria-modal":"true","aria-label":s,children:t.jsxs("div",{className:"relative flex size-full max-h-full w-full max-w-[1280px] flex-col overflow-hidden rounded-2xl border border-sidebar-border bg-bg-color shadow-[0_24px_80px_rgba(0,0,0,0.5)] animate-[modalAppear_0.2s_ease-out] md:h-auto md:max-h-full md:flex-row md:rounded-3xl",onClick:b=>b.stopPropagation(),children:[t.jsxs("div",{className:"relative shrink-0 bg-bg-secondary md:min-h-0 md:flex-1",children:[o?.url?o.type==="video"?t.jsx(el,{src:o.url,className:"size-full max-h-[34dvh] object-contain md:max-h-full"}):t.jsx("img",{src:o.url,alt:s??"",className:"size-full max-h-[34dvh] object-contain md:max-h-full"}):t.jsx("div",{className:"grid h-40 place-items-center md:h-full",children:t.jsx(v,{name:"PictureIcon",className:"size-10 text-subtitle-text-color","aria-hidden":!0})}),a?t.jsx(Jn,{hasPrev:a.hasPrev,hasNext:a.hasNext,onPrev:a.onPrev,onNext:a.onNext,prevLabel:d("templateModal.prevTemplate"),nextLabel:d("templateModal.nextTemplate")}):null]}),e]})})}function il({children:e,footer:n,onClose:s,headerAction:o,overlay:a}){const{t:d}=q();return t.jsxs("div",{className:"relative flex min-h-0 flex-1 flex-col border-sidebar-border md:w-[420px] md:flex-none md:border-l lg:w-[460px]",children:[t.jsxs("header",{className:"flex h-12 shrink-0 items-center justify-between gap-3 border-b border-sidebar-border bg-bg-secondary/60 px-4",children:[t.jsx("h3",{className:"min-w-0 truncate text-[15px] font-semibold text-text-color",children:d("templateModal.title")}),t.jsxs("div",{className:"flex shrink-0 items-center gap-2",children:[o,t.jsx("button",{type:"button",className:"grid size-8 shrink-0 place-items-center rounded-full text-subtitle-text-color transition-colors hover:bg-bg-hover hover:text-text-color",onClick:s,"aria-label":d("common.close"),children:t.jsx(v,{name:"closeIcon",className:"size-4"})})]})]}),t.jsx("div",{className:"scrollbar-hide isolate flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto overscroll-contain px-4 py-3",children:e}),n,a]})}export{hl as TemplateGenerationModal};
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.