PageSourceSearch

https://expertmedia.app/assets/SimplifiedAppBuilder-CvmNiOg0.js

js expertmedia.app collected 2026-10-09 22:02:14 UTC 67,861 bytes, 2 lines download raw bytes

1const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/s3Storage-D07R-_T2.js","assets/index-DqWeGgdl.js","assets/index-DGcEhQOO.css"])))=>i.map(i=>d[i]);
2import{i as ps,r as g,j as e,m as $,B as b,d as ee,s as I,g as T,_ as Ms,ad as us,X as qe,e as Rs,f as Es,L as Os,c as Ls}from"./index-DqWeGgdl.js";import{T as zs,a as Fs,b as ke,c as Ae}from"./tabs-F1NwRQLc.js";import{B as w}from"./badge-Bp380uep.js";import{T as je,I as fe,A as Be}from"./modelConfig-DMdHUdbX.js";import{C as q,a as J,b as X,d as B}from"./card-DqITKmdj.js";import{L as _}from"./label-DLjXeE6d.js";import{I as k}from"./input-c-eczgZh.js";import{T as $e}from"./textarea-Bem7iRjE.js";import{S as Te}from"./switch-Cbfu56pw.js";import{S as se,a as te,b as ae,c as re,d as Q}from"./select-7or67l2U.js";import{P as Ge,a as We,b as He}from"./popover-BwGPb1nx.js";import{A as Vs,i as os,B as qs}from"./BuilderModeSwitcher-CmkJx1JN.js";import{U as Bs}from"./upload-D3VmnkMa.js";import{T as ve}from"./trash-2-DfAlOGrJ.js";import{I as Me}from"./image-DeG-niuc.js";import{U as $s}from"./users-VFKwMrTd.js";import{B as Ke}from"./brain-BguCDOhs.js";import{P as Gs}from"./pen-line-C7t4ZMTM.js";import{P as Ws}from"./palette-rrW_X71v.js";import{F as Hs}from"./file-text-DcpK7RKg.js";import{C as hs}from"./chevrons-up-down-DII7NNvM.js";import{C as be}from"./check-CHksFl-A.js";import{u as Ue,a as me,s as Ye,K as Je,P as Xe,D as Qe,c as Ze,S as es,v as ss,d as ts,b as as,C as rs}from"./sortable.esm-BZBhKqq-.js";import{A as gs,a as js,b as fs,c as bs,d as vs,e as Ns,f as _s,g as ys,h as ws}from"./alert-dialog-BXgkG-vs.js";import{P as xe}from"./plus-COpIj9j5.js";import{L as Cs}from"./layers-uzeP31S2.js";import{G as ls}from"./grip-vertical-CruB4FP1.js";import{C as is}from"./chevron-up-DNCJMtvH.js";import{C as Re}from"./chevron-down-CrMyl6pQ.js";import{V as Ks}from"./variable-BEhyeYQc.js";import{C as Us,a as Ys,b as Js}from"./collapsible-CnFAKkV4.js";import{C as Xs}from"./circle-alert-BAA-BmZz.js";import{C as Qs}from"./chevron-right-CQ0bADqS.js";import{S as Ss}from"./save-DROPt-pR.js";import{R as ks}from"./rocket-DlfaXryW.js";import{S as Zs,b as et}from"./sheet-BTbwVc9W.js";import{u as st}from"./use-mobile-BW6Hg1yb.js";import{E as De}from"./eye-64GuGPNH.js";import{M as tt}from"./monitor-DPi_H2PQ.js";import{S as at}from"./smartphone-CHxa4-y3.js";import{E as rt}from"./eye-off-DIwM9hMt.js";import{A as lt}from"./arrow-right-2B3mm3C5.js";import{S as it}from"./sparkles-BUY2uXQC.js";import{W as nt}from"./wand-sparkles-QplHiUqc.js";import"./index-0knE1gyv.js";import"./index-D0bqXNQt.js";import"./circle-x-DFfi7KTl.js";import"./arrow-right-left-aJwxTk_T.js";import"./index-D2fHITvc.js";import"./index-t4r_rQn3.js";const ns=()=>typeof crypto<"u"&&"randomUUID"in crypto?crypto.randomUUID():Math.random().toString(36).slice(2),Pe=s=>s.toLowerCase().trim().replace(/[^a-z0-9_\s-]/g,"").replace(/[\s-]+/g,"_").replace(/^_+|_+$/g,"").slice(0,50),As=()=>({id:ns(),name:"",label:"",type:"text",required:!0,placeholder:""}),Is=()=>({id:ns(),title:"",description:"",fields:[As()],expanded:!0}),Ps=s=>({id:ns(),label:`Chain ${s}`,output_variable_name:`step_${s}_output`,output_type:"text",is_hidden:!1,ai_model:"gemini",max_tokens:4e3,temperature:.7,prompt_template:"",expanded:!0}),dt=()=>({name:"",description:"",header_image_url:"",suite_id:"",available_text_models:[],available_image_models:[],persona_enabled:!1,memory_enabled:!1,writing_style_enabled:!1,brand_dna_enabled:!1,rag_enabled:!1});function ot({basic:s,setBasic:t,creditCost:a,selectedPersonaId:i,setSelectedPersonaId:o,selectedBrandId:p,setSelectedBrandId:f,selectedWritingStyleId:d,setSelectedWritingStyleId:r,selectedMemoryDocIds:l,setSelectedMemoryDocIds:u,selectedRagDocIds:h,setSelectedRagDocIds:m,appId:n}){const{user:x}=ps(),N=g.useRef(null),[S,pe]=g.useState([]),[G,le]=g.useState(!1),[W,ie]=g.useState([]),[j,Ne]=g.useState([]),[ue,Z]=g.useState([]),[E,H]=g.useState([]),[F,_e]=g.useState([]);g.useEffect(()=>{let c=!1;return(async()=>{const[z,K,oe,U,we,Ce]=await Promise.all([I.from("app_suites").select("id, name").eq("is_active",!0).order("name"),x?I.from("audience_personas").select("id, name").eq("user_id",x.id).order("name"):Promise.resolve({data:[]}),x?I.from("brand_profiles").select("id, name").eq("user_id",x.id).order("name"):Promise.resolve({data:[]}),x?I.from("writing_style_personas").select("id, name").eq("user_id",x.id).order("name"):Promise.resolve({data:[]}),x?I.from("memory_documents").select("id, title").eq("user_id",x.id).order("title"):Promise.resolve({data:[]}),x?I.from("rag_documents").select("id, title").eq("user_id",x.id).order("title"):Promise.resolve({data:[]})]);c||(pe(z.data??[]),ie(K.data??[]),Ne(oe.data??[]),Z(U.data??[]),H(we.data??[]),_e(Ce.data??[]))})(),()=>{c=!0}},[x]);const O=(c,L)=>t({...s,[c]:L}),he=async c=>{if(x){if(!["image/jpeg","image/png","image/webp"].includes(c.type)){T.error("Please upload a JPG, PNG or WebP image.");return}if(c.size>5*1024*1024){T.error("Image must be smaller than 5 MB.");return}le(!0);try{const{uploadToS3:L}=await Ms(async()=>{const{uploadToS3:K}=await import("./s3Storage-D07R-_T2.js");return{uploadToS3:K}},__vite__mapDeps([0,1,2])),{publicUrl:z}=await L(c,"app-headers");O("header_image_url",z),T.success("Header image uploaded.")}catch(L){T.error(`Upload failed: ${(L==null?void 0:L.message)||"Unknown error"}`)}finally{le(!1)}}},ye=()=>O("header_image_url",""),ne=s.description.length,de=ne===0?"text-muted-foreground":ne<100?"text-amber-600 dar
2k:text-amber-400":ne<=300?"text-emerald-600 dark:text-emerald-400":"text-amber-600 dark:text-amber-400";return e.jsxs("div",{className:"space-y-5",children:[e.jsxs(q,{children:[e.jsx(J,{className:"pb-3",children:e.jsx(X,{className:"text-base",children:"App Identity"})}),e.jsxs(B,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{htmlFor:"app-name",children:["App Name ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(k,{id:"app-name",value:s.name,onChange:c=>O("name",c.target.value),placeholder:"e.g., Blog Post Generator"}),e.jsx(Vs,{name:s.name,excludeId:n??null})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(_,{htmlFor:"app-desc",children:["Description ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs("span",{className:$("text-xs",de),children:[ne," chars (recommend 100–300)"]})]}),e.jsx($e,{id:"app-desc",value:s.description,onChange:c=>O("description",c.target.value),placeholder:"Describe what this app does and who it's for...",rows:3})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-baseline justify-between gap-2",children:[e.jsx(_,{children:"Header Image"}),e.jsx("span",{className:"text-[11px] text-muted-foreground",children:"Recommended: 1200 × 400 px (3:1)"})]}),e.jsx("div",{className:"rounded-lg border border-dashed border-border bg-muted/30 overflow-hidden",children:s.header_image_url?e.jsxs("div",{className:"relative",children:[e.jsx("img",{src:s.header_image_url,alt:"App header",className:"w-full h-40 object-cover"}),e.jsxs("div",{className:"absolute top-2 right-2 flex gap-1.5",children:[e.jsxs(b,{size:"sm",variant:"secondary",onClick:()=>{var c;return(c=N.current)==null?void 0:c.click()},disabled:G,children:[G?e.jsx(ee,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Bs,{className:"h-3.5 w-3.5"}),"Replace"]}),e.jsx(b,{size:"sm",variant:"destructive",onClick:ye,children:e.jsx(ve,{className:"h-3.5 w-3.5"})})]})]}):e.jsxs("button",{type:"button",onClick:()=>{var c;return(c=N.current)==null?void 0:c.click()},disabled:G,className:"w-full h-40 flex flex-col items-center justify-center gap-2 text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors",children:[G?e.jsx(ee,{className:"h-8 w-8 animate-spin"}):e.jsx(Me,{className:"h-8 w-8"}),e.jsx("span",{className:"text-sm",children:G?"Uploading...":"Click to upload header image"}),e.jsx("span",{className:"text-xs",children:"JPG, PNG or WebP · max 5 MB · 1200 × 400 px recommended"})]})}),e.jsx("input",{ref:N,type:"file",accept:"image/jpeg,image/png,image/webp",className:"hidden",onChange:c=>{var z;const L=(z=c.target.files)==null?void 0:z[0];L&&he(L),c.target.value=""}})]})]})]}),e.jsxs(q,{children:[e.jsx(J,{className:"pb-3",children:e.jsx(X,{className:"text-base",children:"Suite & Credit Cost"})}),e.jsx(B,{className:"space-y-4",children:e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-[1fr_auto] gap-3 items-end",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{htmlFor:"suite",children:["Suite ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(se,{value:s.suite_id,onValueChange:c=>O("suite_id",c),children:[e.jsx(te,{id:"suite",children:e.jsx(ae,{placeholder:"Select a suite..."})}),e.jsx(re,{children:S.map(c=>e.jsx(Q,{value:c.id,children:c.name},c.id))})]})]}),e.jsxs(w,{variant:"secondary",className:"h-10 px-3 text-sm whitespace-nowrap",children:["Estimated: ",a," credits per run"]})]})})]}),e.jsxs(q,{children:[e.jsxs(J,{className:"pb-3",children:[e.jsx(X,{className:"text-base",children:"Available Models for Users"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Select which AI models end users can choose from. Credit costs scale by multiplier."})]}),e.jsxs(B,{className:"space-y-4",children:[e.jsx(cs,{label:"Text Models",placeholder:"Select text models...",selected:s.available_text_models,options:je,onChange:c=>O("available_text_models",c)}),e.jsx(cs,{label:"Image Models",placeholder:"Select image models...",selected:s.available_image_models,options:fe,onChange:c=>O("available_image_models",c)})]})]}),e.jsxs(q,{children:[e.jsxs(J,{className:"pb-3",children:[e.jsx(X,{className:"text-base",children:"Memory & Context"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Connect your app to personalized content sources. These are optional — turn on what your app needs."})]}),e.jsxs(B,{className:"space-y-3",children:[e.jsx(ge,{icon:e.jsx($s,{className:"h-4 w-4"}),label:"Audience Persona",description:"Inject {{persona.full_summary}} into prompt context.",enabled:s.persona_enabled,onToggle:c=>O("persona_enabled",c),value:i,onValueChange:o,options:W.map(c=>({value:c.id,label:c.name})),placeholder:"Select a persona..."}),e.jsx(ge,{icon:e.jsx(Ke,{className:"h-4 w-4"}),label:"Memory Vault",description:"Pick which memory documents to include.",enabled:s.memory_enabled,onToggle:c=>O("memory_enabled",c),multi:!0,multiValue:l,onMultiChange:u,options:E.map(c=>({value:c.id,label:c.title})),placeholder:"Select memory documents..."}),e.jsx(ge,{icon:e.jsx(Gs,{className:"h-4 w-4"}),label:"Writing Style",description:"Inject the selected style into prompt context for tone matching.",enabled:s.writing_style_enabled,onToggle:c=>O("writing_style_enabled",c),value:d,onValueChange:r,options:ue.map(c=>({value:c.id,label:c.name})),placeholder:"Select a writing style..."}),e.jsx(ge,{icon:e.jsx(Ws,{className:"h-4 w-4"}),label:"Brand DNA",description:"Inject logo, colors, tone, and taglines from the selected brand.",enabled:s.brand_dna_enabled,onToggle:c=>O("brand_dna_enabled",c),value:p,onValueChange:f,options:j.map(c=>({value:c.id,label:c.name})),placeholder:"Select a brand profile..."}),e.jsx(ge,{icon:e.jsx(Hs,{className:"h-4 w-4"}),label:"RAG Documents",description:"Include selected documents as reference material.",enabled:s.rag_enabled,onToggle:c=>O("rag_enabled",c),multi:!0,multiValue:h,onMultiChange:m,options:F.map(c=>({value:c.id,label:c.title})),placeholder:"Select RAG documents..."})]})]})]})}function cs({label:s,placeholder:t,selected:a,options:i,onChange:o}){const[p,f]=g.useState(!1),d=r=>{o(a.includes(r)?a.filter(l=>l!==r):[...a,r])};return e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{children:s}),e.jsxs(Ge,{open:p,onOpenChange:f,children:[e.jsx(We,{asChild:!0,children:e.jsxs(b,{type:"button",variant:"outline",className:"w-full justify-between font-normal",children:[e.jsx("span",{className:"truncate text-left",children:a.length===0?t:`${a.length} model${a.length===1?"":"s"} selected`}),e.jsx(hs,{className:"h-4 w-4 opacity-50 shrink-0"})]})}),e.jsx(He,{className:"w-[var(--radix-popover-trigger-width)] p-0",align:"start",children:e.jsx("div",{className:"max-h-72 overflow-y-auto",children:i.map(r=>{const l=a.includes(r.id);return e.jsxs("button",{type:"button",onClick:()=>d(r.id),className:$("w-full flex items-center gap-3 px-3 py-2 text-left hover:bg-muted transition-colors",l&&"bg-muted/60"),
2children:[e.jsx("div",{className:$("h-4 w-4 rounded border flex items-center justify-center shrink-0",l?"bg-primary border-primary text-primary-foreground":"border-input"),children:l&&e.jsx(be,{className:"h-3 w-3"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("div",{className:"text-sm font-medium text-foreground truncate",children:r.label}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:r.description})]}),e.jsxs(w,{variant:"secondary",className:"text-[10px] shrink-0",children:[r.creditMultiplier,"×"]})]},r.id)})})})]}),a.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1 pt-1",children:a.map(r=>{const l=i.find(u=>u.id===r);return l?e.jsxs(w,{variant:"outline",className:"text-[11px] gap-1",children:[l.label,e.jsx("button",{type:"button",onClick:()=>d(r),className:"hover:text-destructive",children:"×"})]},r):null})})]})}function ge({icon:s,label:t,description:a,enabled:i,onToggle:o,options:p,placeholder:f,value:d,onValueChange:r,multi:l,multiValue:u,onMultiChange:h}){return e.jsx("div",{className:$("rounded-lg border bg-background p-3 transition-colors",i?"border-primary/40":"border-border"),children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:$("rounded-md p-2 shrink-0",i?"bg-primary/10 text-primary":"bg-muted text-muted-foreground"),children:s}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-semibold text-foreground",children:t}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:a})]}),e.jsx(Te,{checked:i,onCheckedChange:o})]}),i&&e.jsx("div",{className:"mt-3",children:l?e.jsx(ct,{options:p,selected:u??[],onChange:h??(()=>{}),placeholder:f}):e.jsxs(se,{value:d??"",onValueChange:m=>r==null?void 0:r(m||null),children:[e.jsx(te,{children:e.jsx(ae,{placeholder:f})}),e.jsx(re,{children:p.length===0?e.jsx("div",{className:"px-2 py-3 text-sm text-muted-foreground italic",children:"Nothing to pick yet — create one first."}):p.map(m=>e.jsx(Q,{value:m.value,children:m.label},m.value))})]})})]})]})})}function ct({options:s,selected:t,onChange:a,placeholder:i}){const[o,p]=g.useState(!1),f=d=>a(t.includes(d)?t.filter(r=>r!==d):[...t,d]);return e.jsxs(e.Fragment,{children:[e.jsxs(Ge,{open:o,onOpenChange:p,children:[e.jsx(We,{asChild:!0,children:e.jsxs(b,{variant:"outline",className:"w-full justify-between font-normal",children:[e.jsx("span",{className:"truncate text-left",children:t.length===0?i:`${t.length} selected`}),e.jsx(hs,{className:"h-4 w-4 opacity-50 shrink-0"})]})}),e.jsx(He,{className:"w-[var(--radix-popover-trigger-width)] p-0",align:"start",children:e.jsx("div",{className:"max-h-64 overflow-y-auto",children:s.length===0?e.jsx("div",{className:"px-3 py-4 text-sm text-muted-foreground italic",children:"Nothing to pick yet — create one first."}):s.map(d=>{const r=t.includes(d.value);return e.jsxs("button",{type:"button",onClick:()=>f(d.value),className:$("w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-muted",r&&"bg-muted/60"),children:[e.jsx("div",{className:$("h-4 w-4 rounded border flex items-center justify-center",r?"bg-primary border-primary text-primary-foreground":"border-input"),children:r&&e.jsx(be,{className:"h-3 w-3"})}),e.jsx("span",{className:"text-sm truncate",children:d.label})]},d.value)})})})]}),t.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1 mt-2",children:t.map(d=>{const r=s.find(l=>l.value===d);return r?e.jsxs(w,{variant:"outline",className:"text-[11px] gap-1",children:[r.label,e.jsx("button",{onClick:()=>f(d),className:"hover:text-destructive",children:"×"})]},d):null})})]})}const ms=[{value:"text",label:"Text (single line)"},{value:"textarea",label:"Textarea (multi-line)"},{value:"dropdown",label:"Dropdown (select one)"},{value:"multi_select",label:"Multi-Select"},{value:"number",label:"Number"},{value:"toggle",label:"Toggle (yes/no)"},{value:"slider",label:"Slider (range)"},{value:"image_cards",label:"Image Cards"},{value:"color",label:"Color Picker"},{value:"date",label:"Date"},{value:"file",label:"File Upload"},{value:"rich_text",label:"Rich Text"},{value:"radio",label:"Radio Buttons"}],mt=["dropdown","multi_select","radio","image_cards"];function xt({steps:s,setSteps:t}){const a=Ue(me(Xe,{activationConstraint:{distance:6}}),me(Je,{coordinateGetter:Ye})),i=d=>{const{active:r,over:l}=d;if(!l||r.id===l.id)return;const u=s.findIndex(m=>m.id===r.id),h=s.findIndex(m=>m.id===l.id);u<0||h<0||t(ts(s,u,h))},o=(d,r)=>t(s.map(l=>l.id===d?{...l,...r}:l)),p=d=>t(s.filter(r=>r.id!==d)),f=()=>t([...s,Is()]);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-foreground",children:"Wizard Steps"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Define the form steps users will fill out before generation. Drag to reorder."})]}),e.jsxs(b,{onClick:f,className:"gap-1.5",children:[e.jsx(xe,{className:"h-4 w-4"})," Add Step"]})]}),s.length===0?e.jsx(q,{children:e.jsxs(B,{className:"py-14 text-center space-y-4",children:[e.jsx("div",{className:"mx-auto h-14 w-14 roun
2ded-full bg-primary/10 flex items-center justify-center",children:e.jsx(Cs,{className:"h-7 w-7 text-primary"})}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-base font-semibold text-foreground",children:"Add your first wizard step"}),e.jsx("p",{className:"text-sm text-muted-foreground max-w-sm mx-auto",children:"Wizard steps are what users fill out before generation — like topic, tone, audience."})]}),e.jsxs(b,{onClick:f,className:"gap-1.5 mt-1",children:[e.jsx(xe,{className:"h-4 w-4"})," Add Step"]})]})}):e.jsx(Qe,{sensors:a,collisionDetection:Ze,onDragEnd:i,children:e.jsx(es,{items:s.map(d=>d.id),strategy:ss,children:e.jsx("div",{className:"space-y-3",children:s.map((d,r)=>e.jsx(pt,{step:d,index:r,onUpdate:l=>o(d.id,l),onRemove:()=>p(d.id)},d.id))})})})]})}function pt({step:s,index:t,onUpdate:a,onRemove:i}){const{attributes:o,listeners:p,setNodeRef:f,transform:d,transition:r,isDragging:l}=as({id:s.id}),u={transform:rs.Transform.toString(d),transition:r,opacity:l?.5:1},h=(x,N)=>a({fields:s.fields.map(S=>S.id===x?{...S,...N}:S)}),m=x=>a({fields:s.fields.filter(N=>N.id!==x)}),n=()=>a({fields:[...s.fields,As()]});return e.jsx(q,{ref:f,style:u,children:e.jsxs(B,{className:"p-0",children:[e.jsxs("div",{className:"flex items-center gap-2 p-3 border-b border-border",children:[e.jsx("button",{type:"button",...o,...p,className:"cursor-grab text-muted-foreground hover:text-foreground p-1",title:"Drag to reorder",children:e.jsx(ls,{className:"h-4 w-4"})}),e.jsxs(w,{variant:"secondary",className:"text-[10px] shrink-0",children:["Step ",t+1]}),e.jsx("button",{type:"button",onClick:()=>a({expanded:!s.expanded}),className:"flex-1 text-left text-sm font-medium text-foreground truncate hover:text-primary transition-colors",children:s.title||"Untitled step"}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[s.fields.length," field",s.fields.length===1?"":"s"]}),e.jsx(b,{variant:"ghost",size:"sm",onClick:()=>a({expanded:!s.expanded}),children:s.expanded?e.jsx(is,{className:"h-4 w-4"}):e.jsx(Re,{className:"h-4 w-4"})}),e.jsx(ft,{label:`Delete step "${s.title||`Step ${t+1}`}"?`,description:"This will remove the step and all its fields. You can't undo this.",onConfirm:i})]}),s.expanded&&e.jsxs("div",{className:"p-4 space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{children:["Step Title ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(k,{value:s.title,onChange:x=>a({title:x.target.value}),placeholder:"e.g., Choose Your Topic"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{children:"Step Description"}),e.jsx(k,{value:s.description,onChange:x=>a({description:x.target.value}),placeholder:"Brief instruction for the user..."})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h4",{className:"text-sm font-semibold text-foreground",children:"Fields"}),e.jsxs(b,{size:"sm",variant:"outline",onClick:n,className:"gap-1",children:[e.jsx(xe,{className:"h-3.5 w-3.5"})," Add Field"]})]}),s.fields.length===0?e.jsxs("p",{className:"text-xs text-muted-foreground italic py-3 text-center border border-dashed border-border rounded-md",children:["No fields yet. Click ",e.jsx("strong",{children:"Add Field"})," to add one."]}):e.jsx("div",{className:"space-y-2",children:s.fields.map((x,N)=>e.jsx(ut,{index:N,field:x,onUpdate:S=>h(x.id,S),onRemove:()=>m(x.id)},x.id))})]})]})]})})}function ut({index:s,field:t,onUpdate:a,onRemove:i}){var r;const[o,p]=g.useState(!0),f=mt.includes(t.type),d=l=>{const u=Pe(t.label),h=!t.name||t.name===u;a({label:l,...h?{name:Pe(l)}:{}})};return e.jsxs("div",{className:"rounded-lg border border-border bg-muted/20",children:[e.jsxs("div",{className:"flex items-center gap-2 p-2.5 border-b border-border/60",children:[e.jsxs(w,{variant:"outline",className:"text-[10px] shrink-0",children:["#",s+1]}),e.jsx("button",{type:"button",onClick:()=>p(!o),className:"flex-1 text-left text-sm font-medium text-foreground truncate hover:text-primary",children:t.label||"Untitled field"}),e.jsx(w,{variant:"secondary",className:"text-[10px] shrink-0",children:((r=ms.find(l=>l.value===t.type))==null?void 0:r.label.split(" ")[0])||t.type}),e.jsx(b,{variant:"ghost",size:"sm",onClick:()=>p(!o),children:o?e.jsx(is,{className:"h-3.5 w-3.5"}):e.jsx(Re,{className:"h-3.5 w-3.5"})}),e.jsx(b,{variant:"ghost",size:"sm",onClick:i,children:e.jsx(ve,{className:"h-3.5 w-3.5 text-destructive"})})]}),o&&e.jsxs("div",{className:"p-3 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{className:"text-xs",children:["Field Label ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(k,{value:t.label,onChange:l=>d(l.target.value),placeholder:"e.g., Genre",className:"h-9"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{className:"text-xs",children:["Variable Name ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(k,{value:t.name,onChange:l=>a({name:Pe(l.target.value)}),placeholder:"genre",className:"h-9 font-mono text-xs"}),e.jsx(w,{variant:"outline",className:"text-[10px] shrink-0 font-mono",children:`{{${t.name||"name"}}}`})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-[2fr_auto] gap-3 items-end",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{className:"text-xs",children:["Field Type ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(se,{value:t.type,onValueChange:l=>a({type:l}),children:[e.jsx(te,{className:"h-9",children:e.jsx(ae,{})}),e.jsx(re,{children:ms.map(l=>e.jsx(Q,{value:l.value,children:l.label},l.value))})]})]}),e.jsxs("div",{className:"flex items-center gap-2 h-9",children:[e.jsx(Te,{checked:t.required,onCheckedChange:l=>a({required:l})}),e.jsx(_,{className:"text-xs",children:"Required"})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Placeholder"}),e.jsx(k,{value:t.placeholder,onChange:l=>a({placeholder:l.target.value}),placeholder:"What the user sees in the empty field",className:"h-9"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Help Text"}),e.jsx(k,{value:t.help_text??"",onChange:l=>a({help_text:l.target.value}),placeholder:"Optional tooltip / hint",className:"h-9"})]})]}),t.type==="slider"&&e.jsx(ht,{field:t,onUpdate:a}),f&&e.jsx(gt,{field:t,onUpdate:a})]})]})}function ht({field:s,onUpdate:t}){return e.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-4 gap-2 p-3 rounded-md bg-background border border-border",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(_,{className:"text-[10px] uppercase tracking-wide text-muted-foreground",children:"Min"}),e.jsx(k,{type:"number",value:s.slider_min??0,onChange:a=>t({slider_min:Number(a.target.value)}),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(_,{className:"text-[10px] uppercase tracking-wide text-muted-foreground",children:"Max"}),e.jsx(k,{type:"number",value:s.slider_max??100,onChange:a=>t({slider_max:Number(a.target.value)}),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(_,{className:"text-[10px] uppercase tracking-wide text-muted-foreground",children:"Step"}),e.jsx(k,{type:"number",value:s.slider_step??1,onChange:a=>t({slider_step:Number(a.target.value)}),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(_,{className:"text-[10px] uppercase tracking-wide text-muted-foreground",children:"Default"}),e.jsx(k,{type:"number",value:s.slider_default??0,onChange:a=>t({slider_default:Number(a.target.value)}),className:"h-8 text-xs"})]})]})}function gt({field:s,onUpdate:t}){const a=s.options??[],[i,o]=g.useState(null),p=(h,m)=>t({options:a.map((n,x)=>x===h?{...n,...m}:n)}),f=h=>t({options:a.filter((m,n)=>n!==h)}),d=()=>t({options:[...a,{title:"",prompt:""}]}),r=Ue(me(Xe,{activationConstraint:{distance:6}}),me(Je,{coordinateGetter:Ye})),l=a.map((h,m)=>`opt-${m}`),u=h=>{const{active:m,over:n}=h;if(!n||m.id===n.id)return;const x=l.indexOf(String(m.id)),N=l.indexOf(String(n.id));x<0||N<0||(t({options:ts(a,x,N)}),o(S=>S===null?S:S===x?N:S===N?x:S))};return e.jsxs("div",{className:"space-y-2 p-3 rounded-md bg-background border border-border",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(_,{className:"text-xs",children:"Options"}),e.jsxs(b,{size:"sm",variant:"outline",onClick:d,className:"h-7 gap-1 text-xs",children:[e.jsx(xe,{className:"h-3 w-3"})," Add Option"]})]}),a.length===0?e.jsx("p",{className:"text-xs text-muted-foreground italic py-2 text-center",children:"No options yet."}):e.jsx(Qe,{sensors:r,collisionDetection:Ze,onDragEnd:u,children:e.jsx(es,{items:l,strategy:ss,children:e.jsx("div",{className:"space-y-1.5",children:a.map((h,m)=>e.jsx(jt,{id:l[m],option:h,index:m,isImageCards:s.type==="image_cards",expanded:i===m,onToggleExpanded:()=>o(i===m?null:m),onUpdate:n=>p(m,n),onRemove:()=>f(m)},l[m]))})})})]})}function jt({id:s,option:t,isImageCards:a,expanded:i,onToggleExpanded:o,onUpdate:p,onRemove:f}){const{attributes:d,listeners:r,setNodeRef:l,transform:u,transition:h,isDragging:m}=as({id:s}),n={transform:rs.Transform.toString(u),transition:h,opacity:m?.5:1};return e.jsxs("div",{ref:l,style:n,className:"rounded-md border border-border/70 bg-muted/30 p-2 space-y-1.5",children:[e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx("button",{type:"button",...d,...r,className:"cursor-grab text-muted-foreground hover:text-foreground p-1 shrink-0",title:"Drag to reorder",children:e.jsx(ls,{className:"h-3.5 w-3.5"})}),e.jsx(k,{value:t.title,onChange:x=>p({title:x.target.value}),placeholder:"Display label",className:"h-8 text-xs"}),a&&e.jsx(k,{value:t.thumbnail_url??"",onChange:x=>p({thumbnail_url:x.target.value}),placeholder:"Image URL",className:"h-8 text-xs"}),e.jsx(b,{size:"sm",variant:"ghost",onClick:o,className:"h-8 px-2 text-xs",children:i?"Hide":"Prompt"}),e.jsx(b,{size:"sm",variant:"ghost",onClick:f,className:"h-8 px-2",children:e.jsx(ve,{className:"h-3 w-3 text-destructive"})})]}),i&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(k,{value:t.prompt,onChange:x=>p({prompt:x.target.value}),placeholder:"Prompt value sent to AI (optional)",className:"h-8 text-xs"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Optional: enter a more detailed value to send to the AI instead of the display label."})]})]})}function ft({label:s,description:t,onConfirm:a}){return e.jsxs(gs,{children:[e.jsx(js,{asChild:!0,children:e.jsx(b,{variant:"ghost",size:"sm",children:e.jsx(ve,{className:"h-4 w-4 text-destructive"})})}),e.jsxs(fs,{children:[e.jsxs(bs,{children:[e.jsx(vs,{children:s}),e.jsx(Ns,{children:t})]}),e.jsxs(_s,{children:[e.jsx(ys,{children:"Cancel"}),e.jsx(ws,{onClick:a,children:"Delete"})]})]})]})}const bt=[{value:"text",label:"Text"},{value:"short_text",label:"Short Text"},{value:"image",label:"Image"},{value:"json",label:"JSON"},{value:"markdown",label:"Markdown"},{value:"html",label:"HTML"}];
2function vt({chains:s,setChains:t,steps:a,basic:i}){const o=Ue(me(Xe,{activationConstraint:{distance:6}}),me(Je,{coordinateGetter:Ye})),p=l=>{const{active:u,over:h}=l;if(!h||u.id===h.id)return;const m=s.findIndex(x=>x.id===u.id),n=s.findIndex(x=>x.id===h.id);m<0||n<0||t(ts(s,m,n))},f=(l,u)=>t(s.map(h=>h.id===l?{...h,...u}:h)),d=l=>t(s.filter(u=>u.id!==l)),r=()=>t([...s,Ps(s.length+1)]);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-foreground",children:"Prompt Chains"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Define the AI generation pipeline. Each chain runs in order. Drag to reorder."})]}),e.jsxs(b,{onClick:r,className:"gap-1.5",children:[e.jsx(xe,{className:"h-4 w-4"})," Add Chain"]})]}),s.length===0?e.jsx(q,{children:e.jsxs(B,{className:"py-14 text-center space-y-4",children:[e.jsx("div",{className:"mx-auto h-14 w-14 rounded-full bg-primary/10 flex items-center justify-center",children:e.jsx(Ke,{className:"h-7 w-7 text-primary"})}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-base font-semibold text-foreground",children:"Add your first prompt chain"}),e.jsxs("p",{className:"text-sm text-muted-foreground max-w-sm mx-auto",children:["A prompt chain is the AI prompt that turns user inputs into outputs. Reference user fields like ",e.jsx("code",{className:"text-xs bg-muted px-1 rounded",children:"{{topic}}"}),"."]})]}),e.jsxs(b,{onClick:r,className:"gap-1.5 mt-1",children:[e.jsx(xe,{className:"h-4 w-4"})," Add Chain"]})]})}):e.jsx(Qe,{sensors:o,collisionDetection:Ze,onDragEnd:p,children:e.jsx(es,{items:s.map(l=>l.id),strategy:ss,children:e.jsx("div",{className:"space-y-3",children:s.map((l,u)=>e.jsx(Nt,{chain:l,index:u,steps:a,basic:i,chains:s,onUpdate:h=>f(l.id,h),onRemove:()=>d(l.id)},l.id))})})})]})}function Nt({chain:s,index:t,steps:a,basic:i,chains:o,onUpdate:p,onRemove:f}){const{attributes:d,listeners:r,setNodeRef:l,transform:u,transition:h,isDragging:m}=as({id:s.id}),n={transform:rs.Transform.toString(u),transition:h,opacity:m?.5:1},x=s.output_type==="image",N=fe.some(j=>j.id===s.ai_model)?!0:je.some(j=>j.id===s.ai_model)?!1:x,S=["gpt-image-2","gpt-image-2.5-flare","gpt-image-2.5-sunburst","qwen-image","qwen-image-plus","qwen-image-edit","wan2.7-image","wan2.7-image-pro"],pe=i.available_image_models.length?i.available_image_models:fe.map(j=>j.id),G=i.available_text_models.length?i.available_text_models:je.map(j=>j.id),le=N?Array.from(new Set([...pe,...S])):G,W=Be.filter(j=>le.includes(j.id)),ie=j=>{var Z,E;if(j==="image"===N)return;const ue=(j==="image"?i.available_image_models.length?i.available_image_models:fe.map(H=>H.id):i.available_text_models.length?i.available_text_models:je.map(H=>H.id))[0]??(j==="image"?(Z=fe[0])==null?void 0:Z.id:(E=je[0])==null?void 0:E.id)??s.ai_model;p({ai_model:ue})};return e.jsx(q,{ref:l,style:n,children:e.jsxs(B,{className:"p-0",children:[e.jsxs("div",{className:"flex items-center gap-2 p-3 border-b border-border",children:[e.jsx("button",{...d,...r,className:"cursor-grab text-muted-foreground hover:text-foreground p-1",children:e.jsx(ls,{className:"h-4 w-4"})}),e.jsxs(w,{variant:"secondary",className:"text-[10px] shrink-0",children:["#",t+1]}),e.jsx("button",{type:"button",onClick:()=>p({expanded:!s.expanded}),className:"flex-1 text-left text-sm font-medium text-foreground truncate hover:text-primary",children:s.label}),e.jsx(w,{variant:"outline",className:"text-[10px] shrink-0",children:s.output_type}),s.is_hidden&&e.jsx(w,{variant:"outline",className:"text-[10px]",children:"hidden"}),e.jsx(b,{variant:"ghost",size:"sm",onClick:()=>p({expanded:!s.expanded}),children:s.expanded?e.jsx(is,{className:"h-4 w-4"}):e.jsx(Re,{className:"h-4 w-4"})}),e.jsx(yt,{label:`Delete ${s.label}?`,onConfirm:f})]}),s.expanded&&e.jsxs("div",{className:"p-4 space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Chain Label"}),e.jsx(k,{value:s.label,onChange:j=>p({label:j.target.value}),className:"h-9"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(_,{className:"text-xs",children:["Output Variable ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(k,{value:s.output_variable_name,onChange:j=>p({output_variable_name:Pe(j.target.value)}),placeholder:"blog_post",className:"h-9 font-mono text-xs"})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-[1fr_1fr_1fr_auto] gap-3 items-end",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Output Type"}),e.jsxs(se,{value:s.output_type,onValueChange:j=>p({output_type:j}),children:[e.jsx(te,{className:"h-9",children:e.jsx(ae,{})}),e.jsx(re,{children:bt.map(j=>e.jsx(Q,{value:j.value,children:j.label},j.value))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Model Type"}),e.jsxs(se,{value:N?"image":"text",onValueChange:j=>ie(j),children:[e.jsx(te,{className:"h-9",children:e.jsx(ae,{})}),e.jsxs(re,{children:[e.jsx(Q,{value:"text",children:"Text Models"}),e.jsx(Q,{value:"image",children:"Image Models"})]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"AI Model"}),e.jsxs(se,{value:s.ai_model,onValueChange:j=>p({ai_model:j}),children:[e.jsx(te,{className:"h-9",children:e.jsx(ae,{})}),e.jsx(re,{children:W.map(j=>e.jsxs(Q,{value:j.id,children:[j.label," ",e.jsxs("span",{className:"text-muted-foreground",children:["· ",j.creditMultiplier,"×"]})]},j.id))})]})]}),e.jsxs("div",{className:"flex items-center gap-2 h-9",children:[e.jsx(Te,{checked:s.is_hidden,onCheckedChange:j=>p({is_hidden:j})}),e.jsx(_,{className:"text-xs whitespace-nowrap",children:"Hide from output"})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Max Output Tokens"}),e.jsx(k,{type:"number",min:100,max:128e3,value:s.max_tokens,onChange:j=>p({max_tokens:Number(j.target.value)}),className:"h-9"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Higher values allow longer outputs. 4,000 is good for most content."})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(_,{className:"text-xs",children:"Temperature"}),e.jsx("span",{className:"text-xs font-mono text-muted-foreground",children:s.temperature.toFixed(2)})]}
2),e.jsx(us,{min:0,max:1,step:.05,value:[s.temperature],onValueChange:([j])=>p({temperature:j})}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Lower = focused. Higher = creative. 0.7 is a good default."})]})]}),e.jsx(_t,{chain:s,steps:a,basic:i,chainsBefore:o.slice(0,t),onChange:j=>p({prompt_template:j})})]})]})})}function _t({chain:s,steps:t,basic:a,chainsBefore:i,onChange:o}){const p=g.useRef(null),f=g.useMemo(()=>{const r=t.flatMap(h=>h.fields.filter(m=>m.name).map(m=>`{{${m.name}}}`)),l=i.filter(h=>h.output_variable_name).map(h=>`{{${h.output_variable_name}}}`),u=[];return a.persona_enabled&&u.push("{{persona.full_summary}}"),a.memory_enabled&&u.push("{{memory_vault}}"),a.writing_style_enabled&&u.push("{{writing_style}}"),a.brand_dna_enabled&&u.push("{{brand_dna}}"),a.rag_enabled&&u.push("{{rag_documents}}"),{fieldVars:r,chainVars:l,ctx:u}},[t,i,a]),d=r=>{const l=p.current;if(!l){o(s.prompt_template+r);return}const u=l.selectionStart??s.prompt_template.length,h=l.selectionEnd??u,m=s.prompt_template.slice(0,u)+r+s.prompt_template.slice(h);o(m),requestAnimationFrame(()=>{l.focus(),l.selectionStart=l.selectionEnd=u+r.length})};return e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(_,{className:"text-xs",children:"Prompt Template"}),e.jsxs(Ge,{children:[e.jsx(We,{asChild:!0,children:e.jsxs(b,{size:"sm",variant:"outline",className:"h-7 gap-1 text-xs",children:[e.jsx(Ks,{className:"h-3 w-3"})," Insert Variable"]})}),e.jsx(He,{className:"w-72 p-0",align:"end",children:e.jsxs("div",{className:"max-h-72 overflow-y-auto p-2 space-y-3",children:[e.jsx(ze,{title:"Wizard fields",tokens:f.fieldVars,onPick:d}),e.jsx(ze,{title:"Previous chain outputs",tokens:f.chainVars,onPick:d}),e.jsx(ze,{title:"Context",tokens:f.ctx,onPick:d})]})})]})]}),e.jsx($e,{ref:p,value:s.prompt_template,onChange:r=>o(r.target.value),placeholder:"Write your prompt. Use the Insert Variable button to reference fields and previous outputs.",className:"font-mono text-xs min-h-[200px]"})]})}function ze({title:s,tokens:t,onPick:a}){return e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] uppercase tracking-wider font-semibold text-muted-foreground px-1 mb-1",children:s}),t.length===0?e.jsx("p",{className:"text-xs text-muted-foreground italic px-1",children:"None available"}):e.jsx("div",{className:"space-y-0.5",children:t.map(i=>e.jsx("button",{type:"button",onClick:()=>a(i),className:"w-full text-left px-2 py-1 rounded text-xs font-mono hover:bg-muted text-foreground",children:i},i))})]})}function yt({label:s,onConfirm:t}){return e.jsxs(gs,{children:[e.jsx(js,{asChild:!0,children:e.jsx(b,{variant:"ghost",size:"sm",children:e.jsx(ve,{className:"h-4 w-4 text-destructive"})})}),e.jsxs(fs,{children:[e.jsxs(bs,{children:[e.jsx(vs,{children:s}),e.jsx(Ns,{children:"This action can't be undone."})]}),e.jsxs(_s,{children:[e.jsx(ys,{children:"Cancel"}),e.jsx(ws,{onClick:t,children:"Delete"})]})]})]})}const Fe=s=>{var t;return((t=Be.find(a=>a.id===s))==null?void 0:t.label)??s};function wt({basic:s,steps:t,chains:a,creditCost:i,saving:o,publishing:p,onSaveDraft:f,onPublish:d}){const[r,l]=g.useState({}),u=[{ok:s.name.trim().length>0,label:"App name is filled in"},{ok:s.description.trim().length>0,label:"Description is filled in"},{ok:!!s.suite_id,label:"A suite is selected"},{ok:t.length>0&&t.some(n=>n.title.trim()&&n.fields.some(x=>x.label.trim())),label:"At least one wizard step with at least one field"},{ok:a.length>0&&a.some(n=>n.prompt_template.trim().length>0),label:"At least one prompt chain with a prompt"},{ok:a.every(n=>n.output_variable_name.trim().length>0),label:"Every chain has an output variable name"}],h=u.every(n=>n.ok),m=[{on:s.persona_enabled,label:"Audience Persona"},{on:s.memory_enabled,label:"Memory Vault"},{on:s.writing_style_enabled,label:"Writing Style"},{on:s.brand_dna_enabled,label:"Brand DNA"},{on:s.rag_enabled,label:"RAG Documents"}];return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(q,{className:$(h?"border-emerald-500/40":"border-amber-500/40"),children:[e.jsx(J,{children:e.jsx(X,{className:"flex items-center gap-2 text-base",children:h?e.jsxs(e.Fragment,{children:[e.jsx(be,{className:"h-5 w-5 text-emerald-500"}),"Ready to publish"]}):e.jsxs(e.Fragment,{children:[e.jsx(Xs,{className:"h-5 w-5 text-amber-500"}),"Fix the items below before publishing"]})})}),e.jsx(B,{children:e.jsx("ul",{className:"space-y-2",children:u.map((n,x)=>e.jsxs("li",{className:"flex items-center gap-2 text-sm",children:[n.ok?e.jsx(be,{className:"h-4 w-4 text-emerald-500 shrink-0"}):e.jsx(qe,{className:"h-4 w-4 text-destructive shrink-0"}),e.jsx("span",{className:n.ok?"text-foreground":"text-muted-foreground",children:n.label})]},x))})})]}),e.jsxs(q,{children:[e.jsx(J,{children:e.jsx(X,{className:"text-base",children:"App Summary"})}),e.jsxs(B,{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4",children:[s.header_image_url?e.jsx("img",{src:s.header_image_url,alt:s.name||"App header",className:"h-24 w-40 rounded-md object-cover border border-border shrink-0"}):e.jsx("div",{className:"h-24 w-40 rounded-md bg-muted/50 border border-dashed border-border flex items-center justify-center text-muted-foreground shrink-0",children:e.jsx(Me,{className:"h-6 w-6"})}),e.jsxs("div",{className:"flex-1 space-y-1.5",children:[e.jsx("div",{className:"font-semibold text-lg",children:s.name||"Untitled app"}),e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-3",children:s.description||"No description"}),e.jsx("div",{className:"flex flex-wrap gap-2 pt-1",children:e.jsxs(w,{variant:"secondary",children:["Estimated: ",i," credits/run"]})})]})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4 pt-2",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-muted-foreground mb-1.5",children:"Text Models"}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:s.available_text_models.length===0?e.jsx("span",{className:"text-xs text-muted-foreground",children:"None selected"}):s.available_text_models.map(n=>e.jsx(w,{variant:"outline",children:Fe(n)},n))})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-muted-foreground mb-1.5",children:"Image Models"}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:s.available_image_models.length===0?e.jsx("span",{className:"text-xs text-muted-foreground",children:"None selected"}):s.available_image_models.map(n=>e.jsx(w,{variant:"outline",children:Fe(n)},n))})]})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-muted-foreground mb-1.5",children:"Memory & Context"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:m.map(n=>e.jsxs("div",{className:$("flex items-center gap-1.5 rounded-md border px-2 py-1 text-xs",n.on?"border-emerald-500/40 bg-emerald-500/5 text-foreground":"border-border text-muted-foreground"),children:[n.on?e.jsx(be,{className:"h-3.5 w-3.5 text-emerald-500"}):e.jsx(qe,{className:"h-3.5 w-3.5"}),n.label]},n.label))})]})]})]}),e.jsxs(q,{children:[e.jsx(J,{children:e.jsxs(X,{className:"text-base",children:["Wizard Steps (",t.length,")"]})}),e.jsxs(B,{className:"space-y-3",children:[t.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No steps configured."}),t.map((n,x)=>e.jsxs("div",{className:"rounded-md border border-border p-3 bg-card",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"font-medium text-sm",children:["Step ",x+1,": ",n.title||e.jsx("span",{className:"text-muted-foreground",children:"Untitled"})]}),e.jsxs(w,{variant:"secondary",className:"text-xs",children:[n.fields.length," field",n.fields.length===1?"":"s"]})]}),n.fields.length>0&&e.jsx("div",{className:"mt-2 flex flex-wrap gap-1.5",children:n.fields.map(N=>e.jsxs(w,{variant:"outline",className:"text-xs font-normal",children:[N.label||"Untitled"," ",e.jsxs("span",{className:"ml-1 text-muted-foreground",children:["· ",N.type]})]},N.id))})]},n.id))]})]}),e.jsxs(q,{children:[e.jsx(J,{children:e.jsxs(X,{className:"text-base",children:["Prompt Chains (",a.length,")"]})}),e.jsxs(B,{className:"space-y-3",children:[a.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No chains configured."}),a.map((n,x)=>e.jsx(Us,{open:!!r[n.id],onOpenChange:N=>l({...r,[n.id]:N}),children:e.jsxs("div",{className:"rounded-md border border-border bg-card",children:[e.jsxs(Ys,{className:"w-full flex items-center justify-between p-3 text-left",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[r[n.id]?e.jsx(Re,{className:"h-4 w-4 text-muted-foreground"}):e.jsx(Qs,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("span",{className:"font-medium text-sm",children:[x+1,". ",n.label]}),e.jsx(w,{variant:"outline",className:"text-xs",children:`{{${n.output_variable_name}}}`}),e.jsx(w,{variant:"secondary",className:"text-xs",children:n.output_type}),n.is_hidden&&e.jsx(w,{variant:"outline",className:"text-xs",children:"hidden"})]}),e.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground",children:[e.jsx("span",{children:Fe(n.ai_model)}),e.jsx("span",{children:"·"}
2),e.jsxs("span",{children:[n.max_tokens.toLocaleString()," tok"]}),e.jsx("span",{children:"·"}),e.jsxs("span",{children:["temp ",n.temperature.toFixed(2)]})]})]}),e.jsx(Js,{children:e.jsx("div",{className:"border-t border-border p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap font-mono text-xs bg-muted/40 rounded-md p-3 max-h-72 overflow-auto",children:n.prompt_template||"(empty)"})})})]})},n.id))]})]}),e.jsxs("div",{className:"sticky bottom-0 -mx-1 px-1 py-3 bg-background/95 backdrop-blur border-t border-border flex flex-col sm:flex-row sm:justify-end gap-2",children:[e.jsxs(b,{variant:"outline",onClick:f,disabled:o||p,className:"gap-2",children:[o?e.jsx(ee,{className:"h-4 w-4 animate-spin"}):e.jsx(Ss,{className:"h-4 w-4"}),"Save as Draft"]}),e.jsxs(b,{onClick:d,disabled:!h||o||p,className:"gap-2 bg-amber-500 hover:bg-amber-500/90 text-amber-50",children:[p?e.jsx(ee,{className:"h-4 w-4 animate-spin"}):e.jsx(ks,{className:"h-4 w-4"}),"Publish App"]})]})]})}function Ct({visible:s,onToggle:t,activeTab:a,basic:i,steps:o,chains:p,creditCost:f}){const[d,r]=g.useState("desktop"),[l,u]=g.useState(0),h=st(),m=o.filter(S=>S.title.trim()),n=m[l],x=e.jsxs("header",{className:"flex items-center justify-between border-b border-border px-4 py-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(De,{className:"h-4 w-4 text-primary"}),e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Preview"})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(b,{variant:d==="desktop"?"default":"ghost",size:"sm",className:"h-7 px-2",onClick:()=>r("desktop"),title:"Desktop",children:e.jsx(tt,{className:"h-3.5 w-3.5"})}),e.jsx(b,{variant:d==="mobile"?"default":"ghost",size:"sm",className:"h-7 px-2",onClick:()=>r("mobile"),title:"Mobile",children:e.jsx(at,{className:"h-3.5 w-3.5"})}),e.jsx("button",{onClick:t,className:"ml-1 text-muted-foreground hover:text-foreground transition-colors",title:"Hide preview",children:e.jsx(rt,{className:"h-4 w-4"})})]})]}),N=e.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:e.jsxs("div",{className:$("mx-auto transition-all",d==="mobile"?"max-w-[320px]":"max-w-full"),children:[e.jsxs("div",{className:"rounded-xl border border-border bg-secondary/20 overflow-hidden mb-4",children:[i.header_image_url?e.jsx("img",{src:i.header_image_url,alt:i.name||"App header",className:"w-full h-32 object-cover"}):e.jsx("div",{className:"w-full h-32 bg-muted/40 flex items-center justify-center",children:e.jsx(Me,{className:"h-8 w-8 text-muted-foreground/40"})}),e.jsxs("div",{className:"p-3",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsx("h3",{className:"text-base font-bold text-foreground line-clamp-1",children:i.name||"Untitled App"}),e.jsxs(w,{variant:"secondary",className:"text-[10px] shrink-0",children:[f," cr"]})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1 line-clamp-2",children:i.description||"App description will appear here."})]})]}),a==="chains"?e.jsx(It,{chains:p}):a==="basic"?e.jsx(St,{basic:i}):e.jsx(kt,{steps:m,currentStep:n,stepIdx:l,onStep:u})]})});return h?e.jsx(Zs,{open:s,onOpenChange:S=>!S&&t(),children:e.jsxs(et,{side:"bottom",className:"h-[85vh] p-0 flex flex-col",children:[x,N]})}):s?e.jsxs("aside",{className:"hidden lg:flex flex-col h-[calc(100vh-4rem)] w-[420px] border-l border-border bg-card sticky top-16",children:[x,N]}):e.jsx("button",{onClick:t,className:"fixed right-4 top-1/2 -translate-y-1/2 z-30 rounded-l-lg border border-border bg-card p-2 shadow-lg hover:bg-secondary/50 transition-colors hidden lg:block",title:"Show preview",children:e.jsx(De,{className:"h-4 w-4 text-muted-foreground"})})}function St({basic:s}){const a=[["Persona",s.persona_enabled],["Memory",s.memory_enabled],["Writing Style",s.writing_style_enabled],["Brand DNA",s.brand_dna_enabled],["RAG",s.rag_enabled]].filter(([,i])=>i);return e.jsxs("div",{className:"space-y-3 text-xs",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground mb-1.5",children:"Text models"}),e.jsx("div",{className:"flex flex-wrap gap-1",children:s.available_text_models.length===0?e.jsx("span",{className:"text-muted-foreground italic",children:"None selected"}):s.available_text_models.map(i=>e.jsx(w,{variant:"outline",className:"text-[10px]",children:i},i))})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground mb-1.5",children:"Image models"}),e.jsx("div",{className:"flex flex-wrap gap-1",children:s.available_image_models.length===0?e.jsx("span",{className:"text-muted-foreground italic",children:"None selected"}):s.available_image_models.map(i=>e.jsx(w,{variant:"outline",className:"text-[10px]",children:i},i))})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground mb-1.5",children:"Context integrations"}),a.length===0?e.jsx("span",{className:"text-muted-foreground italic",children:"None enabled"}):e.jsx("div",{className:"flex flex-wrap gap-1",children:a.map(([i])=>e.jsx(w,{className:"text-[10px]",children:i},i))})]})]})}
2function kt({steps:s,currentStep:t,stepIdx:a,onStep:i}){return s.length===0||!t?e.jsx("div",{className:"text-center text-xs text-muted-foreground py-8 border border-dashed border-border rounded-lg",children:"Add a wizard step to see a preview."}):e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsxs("span",{className:"text-muted-foreground",children:["Step ",a+1," of ",s.length]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx(b,{variant:"ghost",size:"sm",disabled:a===0,className:"h-7 text-xs",onClick:()=>i(a-1),children:"Prev"}),e.jsx(b,{variant:"ghost",size:"sm",disabled:a>=s.length-1,className:"h-7 text-xs",onClick:()=>i(a+1),children:"Next"})]})]}),e.jsxs("div",{className:"rounded-xl border border-border bg-background p-4 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-semibold text-foreground",children:t.title||"Untitled step"}),t.description&&e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:t.description})]}),t.fields.map(o=>e.jsxs("div",{className:"space-y-1",children:[e.jsxs(_,{className:"text-xs",children:[o.label||"Untitled field",o.required&&e.jsx("span",{className:"text-destructive ml-0.5",children:"*"})]}),e.jsx(At,{field:o}),o.help_text&&e.jsx("p",{className:"text-[10px] text-muted-foreground",children:o.help_text})]},o.id)),e.jsx(b,{size:"sm",className:"w-full mt-2 text-xs",children:a===s.length-1?"Generate":"Continue"})]})]})}function At({field:s}){const t=s.placeholder||`Enter ${s.label||"value"}...`;switch(s.type){case"textarea":case"rich_text":return e.jsx($e,{placeholder:t,className:"text-xs min-h-[60px]"});case"number":return e.jsx(k,{type:"number",placeholder:t,className:"h-8 text-xs"});case"toggle":return e.jsx(Te,{});case"color":return e.jsx(k,{type:"color",className:"h-8 w-16 p-0"});case"date":return e.jsx(k,{type:"date",className:"h-8 text-xs"});case"file":return e.jsx(k,{type:"file",className:"h-8 text-xs file:text-xs"});case"slider":return e.jsx(us,{min:s.slider_min??0,max:s.slider_max??100,step:s.slider_step??1,defaultValue:[s.slider_default??s.slider_min??0]});case"dropdown":case"multi_select":return e.jsxs(se,{children:[e.jsx(te,{className:"h-8 text-xs",children:e.jsx(ae,{placeholder:t})}),e.jsxs(re,{children:[(s.options??[]).map((a,i)=>e.jsx(Q,{value:a.title||`opt-${i}`,children:a.title||"Option"},i)),(s.options??[]).length===0&&e.jsx("div",{className:"px-2 py-1.5 text-xs text-muted-foreground italic",children:"No options"})]})]});case"radio":return e.jsxs("div",{className:"space-y-1",children:[(s.options??[]).map((a,i)=>e.jsxs("label",{className:"flex items-center gap-2 text-xs",children:[e.jsx("input",{type:"radio",name:s.id,className:"h-3 w-3"}),a.title||"Option"]},i)),(s.options??[]).length===0&&e.jsx("p",{className:"text-xs text-muted-foreground italic",children:"No options"})]});case"image_cards":return e.jsxs("div",{className:"grid grid-cols-2 gap-1.5",children:[(s.options??[]).map((a,i)=>e.jsxs("div",{className:"aspect-square rounded-md border border-border bg-muted/40 p-1.5 text-[10px] text-center flex flex-col items-center justify-center",children:[a.thumbnail_url?e.jsx("img",{src:a.thumbnail_url,alt:a.title,className:"h-8 w-8 object-cover rounded"}):e.jsx(Me,{className:"h-5 w-5 text-muted-foreground/40"}),e.jsx("span",{className:"mt-1 truncate w-full",children:a.title||"Option"})]},i)),(s.options??[]).length===0&&e.jsx("p",{className:"col-span-2 text-xs text-muted-foreground italic",children:"No options"})]});default:return e.jsx(k,{placeholder:t,className:"h-8 text-xs"})}}function It({chains:s}){return s.length===0?e.jsx("div",{className:"text-center text-xs text-muted-foreground py-8 border border-dashed border-border rounded-lg",children:"Add a prompt chain to see the pipeline."}):e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-[10px] uppercase tracking-wider text-muted-foreground font-semibold",children:"Chain pipeline"}),e.jsx("div",{className:"space-y-1.5",children:s.map((t,a)=>e.jsxs("div",{children:[e.jsxs("div",{className:$("rounded-lg border bg-background px-3 py-2",t.is_hidden?"border-dashed border-muted":"border-border"),children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("span",{className:"text-xs font-semibold text-foreground truncate",children:t.label}),e.jsx(w,{variant:"outline",className:"text-[10px] shrink-0",children:t.output_type})]}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5 font-mono truncate",children:t.output_variable_name?`{{${t.output_variable_name}}}`:"(no output var)"}),t.is_hidden&&e.jsx("p",{className:"text-[10px] text-muted-foreground italic mt-0.5",children:"Hidden from final output"})]}),a<s.length-1&&e.jsx("div",{className:"flex justify-center py-0.5",children:e.jsx(lt,{className:"h-3 w-3 text-muted-foreground rotate-90"})})]},t.id))})]})}const xs="simplified_builder_tour_seen_v1",Ve=[{icon:it,title:"Basic Info",body:"Set your app's name, models, and connect your content sources."},{icon:Cs,title:"Wizard Steps",body:"Build the form your users will fill out before generation."},{icon:Ke,title:"Prompt Chains",body:"Write the AI prompts that turn user inputs into outputs."},{icon:De,title:"Live Preview",body:"See your app exactly as users will experience it — updates as you edit."}];function Pt(){const[s,t]=g.useState(!1),[a,i]=g.useState(0);g.useEffect(()=>{typeof window>"u"||localStorage.getItem(xs)||t(!0)},[]);const o=()=>{try{localStorage.setItem(xs,"1")}catch{}t(!1)};if(!s)return null;const p=Ve[a],f=p.icon,d=a===Ve.length-1;return e.jsxs("div",{className:"fixed bottom-4 right-4 z-50 w-[320px] rounded-xl border border-border bg-card shadow-xl p-4 animate-in fade-in slide-in-from-bottom-4",children:[e.jsx("button",{onClick:o,className:"absolute top-2 right-2 text-muted-foreground hover:text-foreground","aria-label":"Dismiss tour",children:e.jsx(qe,{className:"h-4 w-4"})}),e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"rounded-md bg-primary/10 p-2 text-primary shrink-0",children:e.jsx(f,{className:"h-5 w-5"})}),e.jsxs("div",{className:"min-w-0 pr-4",children:[e.jsx("p",{className:"text-sm font-semibold text-foreground",children:p.title}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5 leading-relaxed",children:p.body})]})]}),e.jsxs("div",{className:"mt-3 flex items-center justify-between",children:[e.jsx("div",{className:"flex gap-1",children:Ve.map((r,l)=>e.jsx("span",{className:`h-1.5 w-1.5 rounded-full ${l===a?"bg-primary":"bg-muted"}`},l))}),e.jsxs("div",{className:"flex gap-1.5",children:[e.jsx(b,{size:"sm",variant:"ghost",className:"h-7 px-2 text-xs",onClick:o,children:"Skip"}),e.jsx(b,{size:"sm",className:"h-7 px-3 text-xs",onClick:()=>d?o():i(a+1),children:d?"Got it":"Next"})]})]})]})}const Dt=s=>{let t=0;for(const a of s){const i=Be.find(o=>o.id===a.ai_model);t+=(i==null?void 0:i.creditMultiplier)??1}return Math.max(1,t)},Tt=(s,t,a)=>{const i=!!(s.name.trim()&&s.description.trim()&&s.suite_id),o=!!(s.name.trim()||s.description.trim()||s.suite_id),p=t.length>0&&t.every(l=>l.title.trim()&&l.fields.every(u=>u.label.trim())),f=t.length>0,d=a.length>0&&a.every(l=>l.prompt_template.trim()&&l.output_variable_name.trim()),r=a.length>0;return{basic:i?"ok":o?"partial":"empty",steps:p?"ok":f?"partial":"empty",chains:d?"ok":r?"partial":"empty",review:i&&p&&d?"ok":"empty"}},Ie=({status:s})=>s==="empty"?null:e.jsx("span",{className:$("ml-1.5 inline-block h-2 w-2 rounded-full",s==="ok"?"bg-emerald-500":"bg-amber-500")}),Ia=()=>{var Ce;const{id:s}=Rs(),t=Es(),{user:a}=ps(),i=!!s&&s!=="new",[o,p]=g.useState(i?s:void 0),[f,d]=g.useState("basic"),[r,l]=g.useState(dt()),[u,h]=g.useState([Is()]),[m,n]=g.useState([Ps(1)]),[x,N]=g.useState(null),[S,pe]=g.useState(null),[G,le]=g.useState(null),[W,ie]=g.useState([]),[j,Ne]=g.useState([]),[ue,Z]=g.useState(i),[E,H]=g.useState(!1),[F,_e]=g.useState(!1),[O,he]=g.useState(!0),[ye,ne]=g.useState(null),de=g.useRef(!1),c=g.useRef(!0),L=g.useRef(null),z=g.useMemo(()=>Dt(m),[m]),K=g.useMemo(()=>Tt(r,u,m),[r,u,m]);g.useEffect(()=>{if(c.current){c.current=!1;return}de.current=!0},[r,u,m,x,S,G,W,j]),g.useEffect(()=>{if(!i||!s||!a||L.current===s)return;L.current=s;let A=!1;return(async()=>{Z(!0);const[P,M,ce,Se]=await Promise.all([I.from("apps").select("*").eq("id",s).maybeSingle(),I.from("app_steps").select("*").eq("app_id",s).order("step_number"),I.from("prompt_chains").select("*").eq("app_id",s).order("chain_order"),I.from("memory_app_links").select("category_id").eq("app_id",s)]);if(A)return;if(P.error||!P.data){T.error("Could not load app"),t("/app-builder");return}const y=P.data,R=y.available_models??{};l({name:y.name??"",description:y.description??"",header_image_url:y.header_image_url??"",suite_id:y.suite_id??"",available_text_models:R.text??[],available_image_models:R.image??[],persona_enabled:!!y.persona_enabled,memory_enabled:!!y.memory_enabled,writing_style_enabled:!!y.writing_style_enabled,brand_dna_enabled:!!y.brand_dna_enabled,rag_enabled:!!y.rag_enabled}),h((M.data??[]).map(v=>({id:v.id,title:v.title??"",description:v.description??"",expanded:!1,fields:Array.isArray(v.fields)?v.fields.map(C=>{const D=(Array.isArray(C.dropdown_options)?C.dropdown_options:Array.isArray(C.options)?C.options:[]).map(V=>V&&typeof V=="object"?{title:typeof V.title=="string"?V.title:"",prompt:typeof V.prompt=="string"?V.prompt:"",thumbnail_url:typeof V.thumbnail_url=="string"?V.thumbnail_url:""}:{title:String(V??""),prompt:"",thumbnail_url:""}),Oe=C.type==="dropdown"&&C.dropdown_display==="image_cards"?"image_cards":C.type??"text";return{id:C.id??crypto.randomUUID(),name:C.name??"",label:C.label??"",type:Oe,required:!!C.required,placeholder:C.placeholder??"",help_text:C.help_text??"",options:D,slider_min:C.slider_min,slider_max:C.slider_max,slider_step:C.slider_step,slider_default:C.slider_default}}):[]}))),n((ce.data??[]).map((v,C)=>({id:v.id,label:v.label||`Chain ${C+1}`,output_variable_name:v.output_variable_name??`step_${C+1}_output`,output_type:v.output_type??"text",is_hidden:!!v.is_hidden,ai_model:v.ai_model??"gemini",max_tokens:v.max_tokens??4e3,temperature:v.temperature??.7,prompt_template:v.prompt_template??"",expanded:!1}))),ie((Se.data??[]).map(v=>v.category_id)),p(s),Z(!1),c.current=!0,de.current=!1})(),()=>{A=!0}},[s,a,i,t]);const oe=g.useCallback(async A=>
2{if(!a)return T.error("Not signed in"),null;if(!r.name.trim())return T.error("App name is required"),null;if(!r.suite_id)return T.error("Please select a suite"),null;const P={name:r.name.trim(),description:r.description.trim(),suite_id:r.suite_id,credit_cost:z,builder_mode:"simplified",status:A?"published":"draft",available_models:{text:r.available_text_models,image:r.available_image_models},header_image_url:r.header_image_url||null,persona_enabled:r.persona_enabled,memory_enabled:r.memory_enabled,writing_style_enabled:r.writing_style_enabled,brand_dna_enabled:r.brand_dna_enabled,rag_enabled:r.rag_enabled};let M=o;if(M){const{error:y}=await I.from("apps").update(P).eq("id",M);if(y)return os(y)?T.error("App Name used, try again."):T.error(`Failed to save app: ${y.message}`),null}else{const{data:y,error:R}=await I.from("apps").insert({...P,creator_id:a.id}).select("id").single();if(R||!y)return os(R)?T.error("App Name used, try again."):T.error(`Failed to create app: ${(R==null?void 0:R.message)??""}`),null;M=y.id,p(M)}await I.from("app_steps").delete().eq("app_id",M);const ce=u.filter(y=>y.title.trim());if(ce.length>0){const y=ce.map((v,C)=>{var Ee;return{app_id:M,step_number:C+1,title:v.title.trim(),description:((Ee=v.description)==null?void 0:Ee.trim())||null,fields:v.fields.filter(D=>D.label.trim()).map(D=>{const Oe=["dropdown","multi_select","radio","image_cards"],V=D.type==="image_cards",ds=V?"dropdown":D.type,Le=(D.options??[]).map(Y=>Y&&typeof Y=="object"?{title:String(Y.title??""),prompt:String(Y.prompt??""),thumbnail_url:String(Y.thumbnail_url??"")}:{title:String(Y??""),prompt:"",thumbnail_url:""}),Ds=Le.map(Y=>Y.title),Ts=Oe.includes(D.type)&&Le.length>0;return{name:D.name,label:D.label,type:ds,required:D.required,placeholder:D.placeholder||"",help_text:D.help_text||"",...Ts?{options:Ds,dropdown_options:Le}:{},...ds==="dropdown"?{dropdown_display:V?"image_cards":"dropdown"}:{},...D.type==="slider"?{slider_min:D.slider_min??0,slider_max:D.slider_max??100,slider_step:D.slider_step??1,slider_default:D.slider_default??0}:{}}})}}),{error:R}=await I.from("app_steps").insert(y);if(R)return T.error(`Failed to save steps: ${R.message}`),null}await I.from("prompt_chains").delete().eq("app_id",M);const Se=m.filter(y=>y.prompt_template.trim());if(Se.length>0){const y=Se.map((v,C)=>({app_id:M,chain_order:C+1,label:v.label,prompt_template:v.prompt_template,ai_model:v.ai_model,output_type:v.output_type,output_variable_name:v.output_variable_name,max_tokens:v.max_tokens,temperature:v.temperature,is_hidden:v.is_hidden})),{error:R}=await I.from("prompt_chains").insert(y);if(R)return T.error(`Failed to save chains: ${R.message}`),null}return await I.from("memory_app_links").delete().eq("app_id",M),r.memory_enabled&&W.length>0&&await I.from("memory_app_links").insert(W.map((y,R)=>({app_id:M,category_id:y,sort_order:R,injection_mode:"inline",is_required:!1}))),await I.from("app_suite_assignments").delete().eq("app_id",M),r.suite_id&&await I.from("app_suite_assignments").insert({app_id:M,suite_id:r.suite_id}),ne(new Date),de.current=!1,M},[a,r,z,o,u,m,W]),U=g.useCallback(async(A=!1)=>{if(E||F)return;H(!0);const P=await oe(!1);H(!1),P&&(A||T.success("Draft saved"),o||t(`/app-builder/simplified/${P}`,{replace:!0}))},[o,t,oe,F,E]),we=g.useCallback(async()=>{if(F||E)return;_e(!0);const A=await oe(!0);_e(!1),A&&(T.success("App published!",{action:{label:"View",onClick:()=>t(`/apps/${A}`)}}),o||t(`/app-builder/simplified/${A}`,{replace:!0}))},[o,t,oe,F,E]);return g.useEffect(()=>{const A=setInterval(()=>{de.current&&!E&&!F&&r.name.trim()&&r.suite_id&&U(!0).then(()=>{T("Auto-saved",{duration:1500})})},3e4);return()=>clearInterval(A)},[U,E,F,r.name,r.suite_id]),g.useEffect(()=>{const A=P=>{(P.metaKey||P.ctrlKey)&&(P.shiftKey&&P.key.toLowerCase()==="p"?(P.preventDefault(),he(ce=>!ce)):!P.shiftKey&&P.key.toLowerCase()==="s"&&(P.preventDefault(),U(!1)))};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[U]),ue?e.jsx("div",{className:"flex h-[60vh] items-center justify-center",children:e.jsx(ee,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"flex",children:[e.jsxs("div",{className:"flex-1 min-w-0 mx-auto max-w-5xl px-1 space-y-5 pb-12",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx(b,{asChild:!0,variant:"ghost",size:"sm",className:"gap-1.5 -ml-2 shrink-0",children:e.jsxs(Os,{to:"/app-builder",children:[e.jsx(Ls,{className:"h-4 w-4"})," Back"]})}),e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(nt,{className:"h-5 w-5 text-primary shrink-0"}),e.jsx("h1",{className:"text-xl font-semibold truncate",children:((Ce=r.name)==null?void 0:Ce.trim())||"Untitled App"}),e.jsx(w,{variant:"outline",className:"hidden sm:inline-flex",children:"Simplified"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[o?"Editing app":"New app"," · Estimated ",z," credit",z===1?"":"s","/run"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2 ml-auto",children:[ye&&e.jsxs("span",{className:"text-xs text-muted-foreground hidden sm:inline",children:["Saved ",ye.toLocaleTimeString()]}),e.jsx(qs,{mode:"simplified",appId:o}),e.jsxs(b,{onClick:()=>he(A=>!A),variant:"ghost",size:"sm",className:"gap-1.5 lg:hidden",title:"Toggle preview (⌘⇧P)",children:[e.jsx(De,{className:"h-4 w-4"})," Preview"]}),e.jsxs(b,{onClick:()=>U(!1),disabled:E||F,variant:"outline",size:"sm",className:"gap-2",title:"Save draft (⌘S)",children:[E?e.jsx(ee,{className:"h-4 w-4 animate-spin"}):e.jsx(Ss,{className:"h-4 w-4"}),"Save Draft"]}),e.jsxs(b,{onClick:we,disabled:E||F,size:"sm",className:"gap-2",title:"Save & publish",children:[F?e.jsx(ee,{className:"h-4 w-4 animate-spin"}):e.jsx(ks,{className:"h-4 w-4"}),"Save & Publish"]})]})]}),e.jsxs(zs,{value:f,onValueChange:A=>d(A),children:[e.jsxs(Fs,{className:"grid w-full grid-cols-4",children:[e.jsxs(ke,{value:"basic",children:["Basic Info ",e.jsx(Ie,{status:K.basic})]}),e.jsxs(ke,{value:"steps",children:["Wizard Steps ",e.jsx(Ie,{status:K.steps})]}),e.jsxs(ke,{value:"chains",children:["Prompt Chains ",e.jsx(Ie,{status:K.chains})]}),e.jsxs(ke,{value:"review",children:["Review ",e.jsx(Ie,{status:K.review})]})]}),e.jsx(Ae,{value:"basic",className:"mt-5",children:e.jsx(ot,{basic:r,setBasic:l,creditCost:z,selectedPersonaId:x,setSelectedPersonaId:N,selectedBrandId:S,setSelectedBrandId:pe,selectedWritingStyleId:G,setSelectedWritingStyleId:le,selectedMemoryDocIds:W,setSelectedMemoryDocIds:ie,selectedRagDocIds:j,setSelectedRagDocIds:Ne,appId:o??null})}),e.jsx(Ae,{value:"steps",className:"mt-5",children:e.jsx(xt,{steps:u,setSteps:h})}),e.jsx(Ae,{value:"chains",className:"mt-5",children:e.jsx(vt,{chains:m,setChains:n,steps:u,basic:r})}),e.jsx(Ae,{value:"review",className:"mt-5",children:e.jsx(wt,{basic:r,steps:u,chains:m,creditCost:z,saving:E,publishing:F,onSaveDraft:()=>U(!1),onPublish:we})})]})]}),e.jsx(Ct,{visible:O,onToggle:()=>he(A=>!A),activeTab:f,basic:r,steps:u,chains:m,creditCost:z}),e.jsx(Pt,{})]})};export{Ia 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.