PageSourceSearch

https://crmduce.lovable.app/assets/FormBuilder-CGYoPTyy.js

js crmduce.lovable.app collected 2026-10-03 03:21:46 UTC 69,557 bytes, 103 lines download raw bytes

1import{j as e,u as es}from"./data-DN-eP4ae.js";import{r as f,k as zs,e as Vs,h as Gs,L as Hs}from"./react-BUOzgyp0.js";import{bd as ds,J as Ks,bu as v,av as S,aT as B,bF as ss,B as y,ay as De,A as Qs,a2 as ae,n as P,cx as ms,C as ge,a3 as us,ax as fe,s as Pe,aA as xs,aB as hs,aC as ps,aD as gs,j as Re,aE as bs,bU as Ys,$ as ce,ds as Js,Z as Xs,bD as fs,bA as Le,Y as Zs,c as O,bC as G,dE as et,K as st,V as tt,bK as at,a$ as nt,aV as rt,a4 as ot,bw as it}from"./index-6GUxm5Tl.js";import{A as ts}from"./AiCopyField-D1bDYLZJ.js";import{S as U,a as q,b as z,c as V,d as M}from"./select-C1j_15i9.js";import{T as vs,a as js,b as J,c as X}from"./tabs-C7nTENN3.js";import{h as lt,d as ct}from"./useForms-BIrUR2bK.js";import{g as ys,a as dt,f as ve,i as mt,S as ut,n as xt,O as ht,C as pt,A as Me,u as gt,F as bt,b as as,D as Ns,c as ft,r as vt,d as jt,e as yt,h as Nt,j as wt,t as kt,k as Ct,p as St,l as ns,m as _t,o as Tt,q as rs}from"./usePublicCatalog-D2AxmdpI.js";import{R as It,a as Ft}from"./radio-group-CORZjgwn.js";import{P as At,s as Et}from"./phone-input-N3YvTTa3.js";import{b as os}from"./browser-JP79f-a9.js";import{Q as Dt}from"./qr-code-BBUT7F9m.js";import{D as Pt}from"./download-D8rVLK2a.js";import{P as Rt}from"./printer-BesHXDy7.js";import{g as Lt}from"./url-BxF7c6Us.js";import{L as ws,C as je}from"./link-65tYANWF.js";import{T as $e}from"./trash-2-DEbXoeSC.js";import{L as ks}from"./layers-3zKAEbPs.js";import{S as Mt}from"./slider-CM1c_-pG.js";import{I as Ee}from"./ImageUploadField-B8bFwMAY.js";import{w as $t}from"./form-fields-CvZ2Waut.js";import{u as Ot}from"./useFields-Igu6f0H0.js";import{E as Wt}from"./eye-BMa0xke4.js";import{P as Bt}from"./play-I9FKzQDa.js";import{S as Ut}from"./share-2-CZ4zcaRo.js";import{S as qt}from"./save-Bg7aMJuF.js";import{G as is}from"./grip-vertical-CiuWx_Va.js";import"./documents-Bju1cS7G.js";import"./charts-BjVT6r80.js";import"./useReviewContext-Z5ny8VkI.js";import"./wand-sparkles-5e-vVx-4.js";import"./star-BwrGBDd5.js";import"./image-plus-DpxDknDd.js";function zt({formName:n,formDesc:N,fields:_,sections:k,successMessage:l}){const[j,t]=f.useState({}),[F,p]=f.useState(0),A=f.useMemo(()=>k&&k.length>0?k:ys(null,_),[k,_]),a=f.useMemo(()=>dt(A,_,j),[A,_,j]);if(_.length===0)return e.jsx("div",{className:"flex items-center justify-center h-full text-muted-foreground text-sm",children:"Add fields to see a live preview"});const x=a.length>1,C=Math.min(F,Math.max(a.length-1,0)),i=a[C],g=i?ve(_,i,A).filter(c=>mt(c,j)):[],b=C>=a.length-1,m=(c,D)=>t(E=>({...E,[c]:D}));return e.jsxs("div",{className:"max-w-md mx-auto",children:[e.jsxs("div",{className:"mb-6 space-y-1",children:[e.jsx("h2",{className:"text-xl font-bold",children:n||"Untitled Form"}),N&&e.jsx("p",{className:"text-sm text-muted-foreground whitespace-pre-line break-words",children:N})]}),x&&e.jsxs("div",{className:"mb-5 space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-xs text-muted-foreground",children:[e.jsxs("span",{children:["Step ",C+1," of ",a.length]}),e.jsxs("button",{className:"inline-flex items-center gap-1 hover:text-foreground transition-colors",onClick:()=>{t({}),p(0)},children:[e.jsx(ds,{className:"h-3 w-3"})," Reset preview"]})]}),e.jsx(Ks,{value:(C+1)/a.length*100,className:"h-1.5"})]}),i&&(i.title||i.description)&&e.jsxs("div",{className:"mb-4 space-y-0.5",children:[i.title&&e.jsx("h3",{className:"text-base font-semibold",children:i.title}),i.description&&e.jsx("p",{className:"text-xs text-muted-foreground whitespace-pre-line",children:i.description})]}),e.jsxs("div",{className:"space-y-4",children:[g.map(c=>{var H;const D=((H=c.options)==null?void 0:H.split(",").map(u=>u.trim()).filter(Boolean))||[],E=j[c.id]||"";return e.jsxs("div",{className:"space-y-1.5",children:[c.type!=="hidden"&&e.jsxs(v,{className:"text-sm",children:[c.label,c.required&&e.jsx("span",{className:"text-destructive ml-1",children:"*"})]}),(c.type==="text"||c.type==="signature")&&e.jsx(S,{placeholder:c.placeholder||(c.type==="signature"?"Signature captured on the live form":""),value:E,onChange:u=>m(c.id,u.target.value)}),c.type==="email"&&e.jsx(S,{type:"email",placeholder:c.placeholder||"[email protected]",value:E,onChange:u=>m(c.id,u.target.value)}
1),(c.type==="phone"||c.type==="tel")&&e.jsx(S,{type:"tel",inputMode:"numeric",maxLength:At,placeholder:c.placeholder||"08000000000",value:E,onChange:u=>m(c.id,Et(u.target.value))}),c.type==="number"&&e.jsx(S,{type:"number",placeholder:c.placeholder||"0",value:E,onChange:u=>m(c.id,u.target.value)}),(c.type==="date"||c.type==="time")&&e.jsx(S,{type:c.type,value:E,onChange:u=>m(c.id,u.target.value)}),(c.type==="rating"||c.type==="recommendation")&&e.jsx(ut,{kind:c.type,value:E,onChange:u=>m(c.id,u)}),c.type==="textarea"&&e.jsx(B,{placeholder:c.placeholder||"",rows:3,value:E,onChange:u=>m(c.id,u.target.value)}),(c.type==="dropdown"||c.type==="select")&&e.jsxs(U,{value:E,onValueChange:u=>m(c.id,u),children:[e.jsx(q,{children:e.jsx(z,{placeholder:c.placeholder||"Select an option"})}),e.jsx(V,{children:D.map(u=>e.jsx(M,{value:u,children:u},u))})]}),c.type==="checkbox"&&e.jsx("div",{className:"space-y-2",children:D.length>0?D.map(u=>{const Z=E.split(";").filter(Boolean);return e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ss,{checked:Z.includes(u),onCheckedChange:$=>{const ne=$?[...Z,u]:Z.filter(ee=>ee!==u);m(c.id,ne.join(";"))}}),e.jsx("span",{className:"text-sm",children:u})]},u)}):e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ss,{checked:E==="true",onCheckedChange:u=>m(c.id,u?"true":"false")}),e.jsx("span",{className:"text-sm",children:c.label})]})}),c.type==="radio"&&e.jsx(It,{value:E,onValueChange:u=>m(c.id,u),children:D.map(u=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ft,{value:u,id:`${c.id}-${u}`}),e.jsx(v,{htmlFor:`${c.id}-${u}`,className:"text-sm font-normal",children:u})]},u))}),c.type==="file"&&e.jsx(S,{type:"file",disabled:!0,className:"text-sm"}),c.type==="catalog"&&e.jsxs("div",{className:"rounded-lg border border-dashed p-3 text-xs text-muted-foreground",children:["Products and services picker - visitors choose items",c.catalogQuantity===!1?"":" and quantity"," and see the order total."]}),c.type==="hidden"&&e.jsxs("p",{className:"text-xs text-muted-foreground italic",children:["Hidden field: ",c.label]})]},c.id)}),e.jsxs("div",{className:"flex items-center gap-2 pt-1",children:[x&&C>0&&e.jsxs(y,{type:"button",variant:"outline",className:"flex-1",onClick:()=>p(C-1),children:[e.jsx(De,{className:"h-4 w-4 mr-1"})," Back"]}),x&&!b?e.jsxs(y,{type:"button",className:"flex-1",onClick:()=>p(C+1),children:["Next ",e.jsx(Qs,{className:"h-4 w-4 ml-1"})]}):e.jsx(y,{className:"flex-1",disabled:!0,children:"Submit"})]}),e.jsxs("p",{className:"text-[11px] text-muted-foreground text-center",children:['Preview only - answers here are not saved. Success message: "',l,'"']})]})]})}function Cs({url:n,formName:N,formDescription:_,compact:k}){const[l,j]=f.useState(""),t=f.useRef({}),F=!n;f.useEffect(()=>{let i=!1;if(!n){j("");return}return os.toDataURL(n,{width:320,margin:1,errorCorrectionLevel:"M"}).then(g=>{i||(t.current[320]=g,j(g))}).catch(()=>j("")),()=>{i=!0}},[n]);const p=f.useMemo(()=>(N||"form").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"")||"form",[N]),A=async i=>{if(t.current[i])return t.current[i];const g=await os.toDataURL(n,{width:i,margin:2,errorCorrectionLevel:"M"});return t.current[i]=g,g},a=async()=>{try{const i=await A(1024),g=document.createElement("a");g.href=i,g.download=`${p}-qr.png`,g.click(),P.success("QR code downloaded")}catch{P.error("Could not generate the QR code")}},x=async()=>{try{const i=await A(1024),g=await(await fetch(i)).blob();await navigator.clipboard.write([new ClipboardItem({"image/png":g})]),P.success("QR image copied")}catch{await navigator.clipboard.writeText(n),P.success("Form link copied instead")}},C=async()=>{try{const i=await A(900),g=window.open("","_blank","width=800,height=1000");if(!g){P.error("Allow pop-ups to print the poster");return}const b=m=>m.replace(/[&<>"]/g,c=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"})[c]);g.document.write(`<!doctype html><html><head><meta charset="utf-8"><title>${b(N)} - QR poster</title>
2<style>
3  body{font-family:system-ui,-apple-system,Segoe UI,sans-serif;margin:0;padding:48px;text-align:center;color:#111}
4  h1{font-size:34px;margin:0 0 8px;line-height:1.2}
5  p.desc{font-size:16px;color:#555;margin:0 auto 28px;max-width:520px}
6  img{width:420px;height:420px}
7  p.hint{font-size:18px;margin-top:24px;font-weight:600}
8  code{display:block;margin-top:10px;font-size:13px;color:#666;word-break:break-all}
9  @media print{body{padding:24px}}
10</style></head><body>
11  <h1>${b(N)}</h1>
12  ${_?`<p class="desc">${b(_)}</p>`:""}
13  <img src="${i}" alt="QR code" />
14  <p class="hint">Scan to open this form</p>
15  <code>${b(n)}</code>
16  <script>window.onload=function(){window.focus();window.print();}<\/script>
17</body></html>`),g.document.close()}catch{P.error("Could not build the poster")}};return e.jsxs("div",{className:"space-y-3",children:[!k&&e.jsxs(v,{className:"text-xs font-semibold uppercase text-muted-foreground flex items-center gap-1.5",children:[e.jsx(Dt,{className:"h-3.5 w-3.5"})," QR code"]}),e.jsxs("div",{className:"flex flex-col sm:flex-row items-center gap-4 roun
17ded-lg border bg-muted/30 p-4",children:[e.jsx("div",{className:"rounded-lg bg-white p-2 shrink-0",children:l?e.jsx("img",{src:l,alt:`QR code for ${N}`,className:"h-36 w-36"}):e.jsx("div",{className:"h-36 w-36 animate-pulse rounded bg-muted"})}),e.jsxs("div",{className:"flex-1 space-y-2 text-center sm:text-left",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Print it on a flyer, table tent or shop counter - anyone can scan it to open your form."}),e.jsxs("div",{className:"flex flex-wrap justify-center sm:justify-start gap-2",children:[e.jsxs(y,{variant:"outline",size:"sm",onClick:a,disabled:F,children:[e.jsx(Pt,{className:"h-3.5 w-3.5 mr-1"})," Download PNG"]}),e.jsxs(y,{variant:"outline",size:"sm",onClick:x,disabled:F,children:[e.jsx(ae,{className:"h-3.5 w-3.5 mr-1"})," Copy image"]}),e.jsxs(y,{variant:"outline",size:"sm",onClick:C,disabled:F,children:[e.jsx(Rt,{className:"h-3.5 w-3.5 mr-1"})," Print poster"]})]})]})]})]})}const Vt="https://znmnwaprjqiwjbtoqpej.supabase.co";function Gt({formSlug:n,formId:N,formStatus:_,orgId:k,formName:l,formDescription:j}){const[t,F]=f.useState(null),p=`${Vt}/functions/v1/form-submit`,A=ms(),a=k?`?org=${k}`:"",x=Lt({kind:"form",reference:n,params:{org:k}}).url,C=`${A}/form/${n}${a}`,i=`<!-- DuceCRM Form Embed -->
18<div id="ducecrm-form-${n}"></div>
19<script>
20(function(){
21  var d=document,f=d.createElement("iframe");
22  f.src="${C}";
23  f.style.cssText="width:100%;min-height:700px;border:0;border-radius:12px;";
24  f.loading="lazy";
25  f.title="DuceCRM Form";
26  d.getElementById("ducecrm-form-${n}").appendChild(f);
27})();
28<\/script>`,g=`<!-- DuceCRM Form Popup - opens in-page, keeps visitors on your site -->
29<button id="ducecrm-form-btn-${n}" style="padding:12px 24px;background:#2563eb;color:#fff;border:none;border-radius:6px;font-size:16px;cursor:pointer;">
30  Open Form
31</button>
32<script>
33(function(){
34  var url = "${C}";
35  var btn = document.getElementById("ducecrm-form-btn-${n}");
36  if(!btn) return;
37  btn.addEventListener("click", function(){
38    var overlay = document.createElement("div");
39    overlay.style.cssText = "position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:2147483647;display:flex;align-items:center;justify-content:center;padding:16px;";
40    var card = document.createElement("div");
41    card.style.cssText = "position:relative;width:100%;max-width:560px;height:100%;max-height:780px;background:#fff;border-radius:12px;overflow:hidden;box-shadow:0 20px 60px rgba(0,0,0,.3);";
42    var close = document.createElement("button");
43    close.setAttribute("aria-label","Close");
44    close.innerHTML = "&times;";
45    close.style.cssText = "position:absolute;top:8px;right:8px;width:32px;height:32px;border:0;border-radius:50%;background:rgba(0,0,0,.6);color:#fff;font-size:20px;line-height:1;cursor:pointer;z-index:1;";
46    var iframe = document.createElement("iframe");
47    iframe.src = url;
48    iframe.title = "DuceCRM Form";
49    iframe.style.cssText = "width:100%;height:100%;border:0;";
50    function closeModal(){ document.body.style.overflow=""; overlay.remove(); document.removeEventListener("keydown", onKey); }
51    function onKey(e){ if(e.key === "Escape") closeModal(); }
52    close.addEventListener("click", closeModal);
53    overlay.addEventListener("click", function(e){ if(e.target === overlay) closeModal(); });
54    document.addEventListener("keydown", onKey);
55    card.appendChild(close); card.appendChild(iframe); overlay.appendChild(card);
56    document.body.style.overflow = "hidden";
57    document.body.appendChild(overlay);
58  });
59})();
60<\/script>`,b=`// POST to this endpoint from your website:
61// ${p}
62
63fetch("${p}", {
64  method: "POST",
65  headers: {
66    "Content-Type": "application/json",
67    "apikey": "YOUR_PUBLIC_API_KEY"
68  },
69  body: JSON.stringify({
70    form_id: "${N}",
71    slug: "${n}",
72    org_id: "${k??""}",
73    // Any unique string per submission - protects against double sends
74    idempotency_key: crypto.randomUUID(),
75    data: {
76      // Use field IDs as keys
77      "field_id_1": "value1",
78      "field_id_2": "value2"
79    },
80    metadata: {
81      utm_source: "website",
82      utm_medium: "organic",
83      utm_campaign: "launch"
84    }
85  })
86})
87  .then(function (r) { return r.json(); })
88  .then(function (out) {
89    if (!out.success) throw new Error(out.error || "Submission failed");
90    console.log("Submitted", out.submission_id);
91  });`,m=(c,D)=>{navigator.clipboard.writeText(c),F(D),P.success(`${D} copied!`),setTimeout(()=>F(null),2e3)};return _!=="active"&&_!=="published"?e.jsxs("div",{className:"rounded-lg border border-warning/30 bg-warning/5 p-4 text-center",children:[e.jsx("p",{className:"text-sm font-medium text-warning",children:"Form must be active to generate embed code"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:'Publish your form first by changing its status to "active"'})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Direct Link"}),e.jsxs("div",{className:"flex items-center gap-2 rounded-lg border bg-muted/50 p-2",children:[e.jsx(ws,{className:"h-4 w-4 text-muted-foreground shrink-0"}),e.jsx("code",{className:"flex-1 text-xs truncate",children:x}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-7 w-7 shrink-0",onClick:()=>m(x,"Link"),children:t==="Link"?e.jsx(ge,{className:"h-3.5 w-3.5 text-primary"}):e.jsx(ae,{className:"h-3.5 w-3.5"})}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-7 w-7 shrink-0",onClick:()=>window.open(x,"_blank"),children:e.jsx(us,{className:"h-3.5 w-3.5"})})]})]}),e.jsx(Cs,{url:x,formName:l||"Form",formDescription:j}),e.jsxs(vs,{defaultValue:"html",children:[e.jsxs(js,{className:"w-full",children:[e.jsxs(J,{value:"html",className:"flex-1 text-xs",children:[e.jsx(je,{className:"h-3 w-3 mr-1"})," HTML Embed"]}),e.jsxs(J,{value:"popup",className:"flex-1 text-xs",children:[e.jsx(je,{className:"h-3 w-3 mr-1"})," Popup"]}),e.jsx(J,{value:"api",className:"flex-1 text-xs",children:"API Endpoint"})]}),e.jsxs(X,{value:"popup",className:"mt-2 space-y-2",children:[e.jsx("div",{className:"rounded-lg bg-muted p-3 font-mono text-[11px] overflow-x-auto max-h-64 overflow-y-auto whitespace-pre-wrap",children:g}),e.jsxs(y,{variant:"outline",size:"sm",onClick:()=>m(g,"Popup embed"),children:[t==="Popup embed"?e.jsx(ge,{className:"h-3.5 w-3.5 mr-1 text-primary"}):e.jsx(ae,{className:"h-3.5 w-3.5 mr-1"}),"Copy Popup"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Clicking the button opens the form in an on-page modal - visitors stay on your website."})]}),e.jsxs(X,{value:"html",className:"mt-2",children:[e.jsx("div",{className:"rounded-lg bg-muted p-3 font-mono text-[11px] overflow-x-auto max-h-64 overflow-y-auto whitespace-pre-wrap",children:i}),e.jsxs(y,{variant:"outline",size:"sm",className:"mt-2",onClick:()=>m(i,"HTML embed"),children:[t==="HTML embed"?e.jsx(ge,{className:"h-3.5 w-3.5 mr-1 text-primary"}):e.jsx(ae,{className:"h-3.5 w-3.5 mr-1"}),"Copy HTML"]})]}),e.jsxs(X,{value:"api",className:"mt-2",children:[e.jsx("div",{className:"rounded-lg bg-muted p-3 font-mono text-[11px] overflow-x-auto max-h-64 overflow-y-auto whitespace-pre-wrap",children:b}),e.jsxs(y,{variant:"outline",size:"sm",className:"mt-2",onClick:()=>m(b,"API code"),children:[t==="API code"?e.jsx(ge,{className:"h-3.5 w-3.5 mr-1 text-primary"}):e.jsx(ae,{className:"h-3.5 w-3.5 mr-1"}),"Copy API Code"]})]})]})]})}function Ht(n){return n!=null&&n.options?n.options.split(",").map(N=>
91N.trim()).filter(Boolean):[]}function ls({subject:n,condition:N,sourceFields:_,onChange:k}){const l=xt(N),j=!!l,t=x=>{k(x.rules.length===0?null:x)},F=()=>{const x=_[0];x&&k({action:"show",match:"all",rules:[{fieldId:x.id,operator:"equals",value:""}]})},p=(x,C)=>{l&&t({...l,rules:l.rules.map((i,g)=>g===x?{...i,...C}:i)})},A=x=>{l&&t({...l,rules:l.rules.filter((C,i)=>i!==x)})},a=()=>{if(!l)return;const x=_[0];x&&t({...l,rules:[...l.rules,{fieldId:x.id,operator:"equals",value:""}]})};return _.length===0?e.jsx("div",{className:"rounded-lg border border-dashed p-2.5",children:e.jsxs("p",{className:"text-[11px] text-muted-foreground",children:["Add another field first to build a rule for ",n,"."]})}):e.jsxs("div",{className:"rounded-lg border p-2.5 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(v,{className:"text-xs",children:"Conditional logic"}),j?e.jsx(y,{variant:"ghost",size:"sm",className:"h-6 px-2 text-[11px]",onClick:()=>k(null),children:"Always show"}):e.jsxs(y,{variant:"outline",size:"sm",className:"h-6 px-2 text-[11px]",onClick:F,children:[e.jsx(fe,{className:"h-3 w-3 mr-1"})," Add rule"]})]}),!j&&e.jsxs("p",{className:"text-[11px] text-muted-foreground",children:[n.charAt(0).toUpperCase()+n.slice(1)," is always shown."]}),j&&l&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsxs(U,{value:l.action,onValueChange:x=>t({...l,action:x}),children:[e.jsx(q,{className:"h-8 text-xs w-[84px]",children:e.jsx(z,{})}),e.jsxs(V,{children:[e.jsx(M,{value:"show",className:"text-xs",children:"Show"}),e.jsx(M,{value:"hide",className:"text-xs",children:"Hide"})]})]}),e.jsxs("span",{className:"text-[11px] text-muted-foreground shrink-0",children:[n," when"]}),e.jsxs(U,{value:l.match,onValueChange:x=>t({...l,match:x}),children:[e.jsx(q,{className:"h-8 text-xs w-[68px]",children:e.jsx(z,{})}),e.jsxs(V,{children:[e.jsx(M,{value:"all",className:"text-xs",children:"all"}),e.jsx(M,{value:"any",className:"text-xs",children:"any"})]})]})]}),l.rules.map((x,C)=>{const i=_.find(m=>m.id===x.fieldId),g=Ht(i),b=!ht.includes(x.operator);return e.jsxs("div",{className:"rounded-md border bg-muted/30 p-2 space-y-1.5",children:[e.jsxs("div",{className:"flex items-start gap-1.5",children:[e.jsxs(U,{value:x.fieldId,onValueChange:m=>p(C,{fieldId:m,value:""}),children:[e.jsx(q,{className:"h-8 text-xs flex-1",children:e.jsx(z,{})}),e.jsx(V,{children:_.map(m=>e.jsx(M,{value:m.id,className:"text-xs",children:m.label},m.id))})]}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-8 w-8 shrink-0",onClick:()=>A(C),children:e.jsx($e,{className:"h-3 w-3 text-muted-foreground"})})]}),e.jsxs(U,{value:x.operator,onValueChange:m=>p(C,{operator:m}),children:[e.jsx(q,{className:"h-8 text-xs",children:e.jsx(z,{})}),e.jsx(V,{children:pt.map(m=>e.jsx(M,{value:m.key,className:"text-xs",children:m.label},m.key))})]}),b&&(g.length>0?e.jsxs(U,{value:x.value||"",onValueChange:m=>p(C,{value:m}),children:[e.jsx(q,{className:"h-8 text-xs",children:e.jsx(z,{placeholder:"Choose an answer"})}),e.jsx(V,{children:g.map(m=>e.jsx(M,{value:m,className:"text-xs",children:m},m))})]}):e.jsx(S,{value:x.value||"",onChange:m=>p(C,{value:m.target.value}),placeholder:"this answer",className:"h-8 text-xs"}))]},C)}),e.jsxs(y,{variant:"ghost",size:"sm",className:"h-7 px-2 text-[11px]",onClick:a,children:[e.jsx(fe,{className:"h-3 w-3 mr-1"})," Add another rule"]})]})]})}const Kt={rate_limited:"Too many AI requests right now. Try again in a moment.",credits_exhausted:"AI credits are exhausted. Add credits to keep generating forms.",no_org:"No organisation found for your account.",brief_required:"Describe the form you want first.",no_valid_form:"The AI could not turn that into a form. Add a bit more detail and retry."};function Qt(){const[n,N]=f.useState(!1);return{generate:async(k,l)=>{N(!0);try{const{data:j,error:t}=await Pe.functions.invoke("form-ai-generate",{body:{brief:k,existing:l}}),F=j==null?void 0:j.error;if(t||F){const p=Kt[F]||((t==null?void 0:t.message)??"Could not generate the form. Please try again.");return P.error("AI generation failed",{description:p}),null}return j==null?void 0:j.form}catch(j){return P.error("AI generation failed",{cause:j}),null}finally{N(!1)}},isGenerating:n}}const Yt=["Bridal measurement and order form for a fashion label, with fabric choice and delivery date","Event catering enquiry - guest count, menu style, budget range and event date","Property viewing request with budget, preferred area and financing status"];function Jt({open:n,onOpenChange:N,existing:_,onApply:k}){const{generate:l,isGenerating:j}=Qt(),[t,F]=f.useState(""),[p,A]=f.useState(null),a=async()=>{const i=await l(t.trim(),_);i&&A(i)},x=i=>{i||(A(null),F("")),N(i)},C=Object.keys((p==null?void 0:p.automations)||{});return e.jsx(xs,{open:n,onOpenChange:x,children:e.jsxs(hs,{size:"md",className:" flex flex-col max-h-[85vh]",children:[e.jsxs(ps,{children:[e.jsxs(gs,{className:"flex items-center gap-2",children:[e.jsx(Re,{className:"h-4 w-4 text-primary"})," Build this form with AI"]}),e.jsx(bs,{children:"Describe what you need to collect. You will review the steps, questions and logic before anything is applied."})]}),e.jsx(Ys,{className:"flex-1 -mx-6 px-6",children:e.jsxs("div",{className:"space-y-4 pb-1",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(v,{htmlFor:"ai-brief",children:"What should this form do?"}),e.jsx(B,{id:"ai-brief",value:t,onChange:i=>F(i.target.value),rows:4,placeholder:"e.g. Intake form for my tailoring business - capture the client's contact details, measurements, fabric choice and pickup date."}),e.jsx("div",{className:"flex flex-wrap gap-1.5 pt-1",children:Yt.map(i=>e.jsx("button",{type:"button",onClick:()=>F(i),className:"text-[11px] rounded-full border px-2 py-1 text-muted-foreground hover:bg-muted transition-colors text-left",children:i.split(" - ")[0]},i))})]}),p&&e.jsxs("div",{className:"rounded-lg border bg-muted/30 p-3 space-y-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-semibold",children:p.name}),p.description&&e.jsx("p",{className:"text-xs text-muted-foreground",children:p.description})]}),e.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[e.jsxs(ce,{variant:"secondary",className:"gap-1",children:[e.jsx(ks,{className:"h-3 w-3"})," ",p.section_count," step",p.section_count===1?"":"s"]}),e.jsxs(ce,{variant:"secondary",className:"gap-1",children:[e.jsx(Js,{className:"h-3 w-3"})," ",p.field_count," question",p.field_count===1?"":"s"]}),C.length>0&&e.jsxs(ce,{variant:"secondary",className:"gap-1",children:[e.jsx(Xs,{className:"h-3 w-3"})," ",C.length," automation",C.length===1?"":"s"]})]}),e.jsx("div",{className:"space-y-2",children:p.sections.map((i,g)=>{const b=ve(p.fields,i,p.sections);return e.jsxs("div",{className:"rounded-md border bg-card p-2.5",children:[e.jsxs("p",{className:"text-xs font-semibold",children:[e.jsxs("span",{className:"text-muted-foreground mr-1",children:["Step ",g+1,":"]}),i.title,i.condition&&e.jsx(ce,{variant:"outline",className:"ml-1.5 text-[10px]",children:"conditional"})]}),i.description&&e.jsx("p",{className:"text-[11px] text-muted-foreground",children:i.description}),e.jsx("ul",{className:"mt-1.5 space-y-0.5",children:b.map(m=>e.jsxs("li",{className:"text-[11px] text-muted-foreground flex items-center gap-1.5",children:[e.jsx("span",{className:"truncate",children:m.label}),e.jsx("span",{className:"opacity-60",children:m.type}),m.required&&e.jsx("span",{className:"text-destructive",children:"*"}),m.condition&&e.jsx("span",{className:"opacity-60",children:"- conditional"})]},m.id))})]},i.id)})}),C.length>0&&e.jsxs("p",{className:"text-[11px] text-muted-foreground",children:["Suggested automations: ",C.map(i=>{var g;return((g=Me[i])==null?void 0:g.label)||i}
91).join(", ")]})]})]})}),e.jsxs(fs,{className:"gap-2 sm:gap-2",children:[e.jsx(y,{variant:"outline",onClick:()=>x(!1),disabled:j,children:"Cancel"}),p?e.jsxs(e.Fragment,{children:[e.jsxs(y,{variant:"outline",onClick:a,disabled:j||!t.trim(),children:[j?e.jsx(Le,{className:"h-4 w-4 mr-1 animate-spin"}):e.jsx(Zs,{className:"h-4 w-4 mr-1"}),"Regenerate"]}),e.jsx(y,{onClick:()=>{k(p),x(!1)},children:"Apply to builder"})]}):e.jsxs(y,{onClick:a,disabled:j||!t.trim(),children:[j?e.jsx(Le,{className:"h-4 w-4 mr-1 animate-spin"}):e.jsx(Re,{className:"h-4 w-4 mr-1"}),"Generate"]})]})]})})}const Xt=["#ffffff","#f8fafc","#f1f5f9","#fef3c7","#ecfdf5","#eff6ff","#f5f3ff","#111827"],Zt=["#2563eb","#0ea5e9","#059669","#d97706","#db2777","#7c3aed","#111827"];function cs({colors:n,value:N,onPick:_}){return e.jsxs("div",{className:"flex flex-wrap items-center gap-1.5",children:[n.map(k=>e.jsx("button",{type:"button","aria-label":`Use ${k}`,onClick:()=>_(k),className:O("h-6 w-6 rounded-full border transition-transform hover:scale-110",(N==null?void 0:N.toLowerCase())===k.toLowerCase()&&"ring-2 ring-primary ring-offset-1"),style:{backgroundColor:k}},k)),e.jsx("button",{type:"button",onClick:()=>_(null),className:"rounded-full border px-2 py-0.5 text-[10px] hover:bg-muted",children:"Default"})]})}function ea({style:n,onChange:N,orgId:_,formId:k}){var p,A;const l=(a,x)=>N({...n,[a]:{...n[a],...x}}),j=`public/${_||"shared"}/forms`,t=((p=n.typography)==null?void 0:p.font)||"inherit",F=((A=n.typography)==null?void 0:A.scale)||"normal";return gt(n),e.jsxs("div",{className:"space-y-5",children:[e.jsxs("section",{className:"space-y-2",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Typeface"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:`Match the website where this form is embedded. "Match website" uses the host page's own font.`}),e.jsx("div",{className:"grid grid-cols-2 gap-1.5 sm:grid-cols-3",children:bt.map(a=>e.jsxs("button",{type:"button",onClick:()=>l("typography",{font:a}),className:O("rounded-lg border px-2.5 py-2 text-left text-xs transition-colors",t===a?"border-primary bg-primary/5":"hover:bg-muted"),style:{fontFamily:as[a].stack},children:[e.jsx("span",{className:"block font-medium",children:as[a].label}),e.jsx("span",{className:"block truncate text-[10px] text-muted-foreground",children:"Aa Bb 123"})]},a))}),e.jsx("div",{className:"flex items-center gap-1.5 pt-1",children:["compact","normal","large"].map(a=>e.jsx("button",{type:"button",onClick:()=>l("typography",{scale:a}),className:O("rounded-full border px-2.5 py-0.5 text-[10px] capitalize transition-colors",F===a?"bg-primary text-primary-foreground":"hover:bg-muted"),children:a},a))})]}),e.jsxs("section",{className:"space-y-2 border-t pt-4",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Page background"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"color",value:n.background.color||"#ffffff",onChange:a=>l("background",{color:a.target.value}),className:"h-8 w-10 cursor-pointer rounded border bg-transparent p-0.5","aria-label":"Background colour"}),e.jsx(S,{value:n.background.color||"",onChange:a=>l("background",{color:a.target.value||null}),placeholder:"#ffffff",className:"h-8 text-xs"})]}),e.jsx(cs,{colors:Xt,value:n.background.color,onPick:a=>l("background",{color:a})}),e.jsx(Ee,{label:"Background image (optional)",value:n.background.image||null,onChange:a=>l("background",{image:a}),folder:j,fileName:`${k}-bg`,hint:"PNG, JPG or WEBP - max 5MB"}),n.background.image&&e.jsxs("div",{className:"space-y-2 rounded-lg border p-2.5",children:[e.jsx("div",{className:"flex items-center gap-1.5",children:["cover","contain","tile"].map(a=>e.jsx("button",{type:"button",onClick:()=>l("background",{fit:a}),className:O("rounded-full border px-2.5 py-0.5 text-[10px] capitalize transition-colors",n.background.fit===a?"bg-primary text-primary-foreground":"hover:bg-muted"),children:a},a))}),e.jsxs("div",{children:[e.jsxs(v,{className:"text-[11px]",children:["Dim overlay - ",n.background.overlay||0,"%"]}),e.jsx(Mt,{value:[n.background.overlay||0],onValueChange:([a])=>l("background",{overlay:a}),min:0,max:90,step:5,className:"mt-2"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-1",children:"Darkens busy photos so the form stays readable."})]})]})]}),e.jsxs("section",{className:"space-y-2 border-t pt-4",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Form card"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"color",value:n.card.color||"#ffffff",onChange:a=>l("card",{color:a.target.value}),className:"h-8 w-10 cursor-pointer rounded border bg-transparent p-0.5","aria-label":"Card colour"}),e.jsx(S,{value:n.card.color||"",onChange:a=>l("card",{color:a.target.value||null}),placeholder:"Card colour",className:"h-8 text-xs"})]}),e.jsx("div",{className:"flex items-center gap-1.5",children:["soft","rounded","square"].map(a=>e.jsx("button",{type:"button",onClick:()=>l("card",{radius:a}),className:O("rounded-full border px-2.5 py-0.5 text-[10px] capitalize transition-colors",n.card.radius===a?"bg-primary text-primary-foreground":"hover:bg-muted"),children:a},a))}),e.jsxs("div",{className:"pt-1",children:[e.jsx(v,{className:"text-[11px]",children:"Accent colour (buttons, progress)"}),e.jsx("div",{className:"mt-1.5",children:e.jsx(cs,{colors:Zt,value:n.card.accent,onPick:a=>l("card",{accent:a})})})]})]}),e.jsxs("section",{className:"space-y-2 border-t pt-4",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Top banner"}),e.jsx(G,{checked:!!n.top_banner.enabled,onCheckedChange:a=>l("top_banner",{enabled:a})})]}),n.top_banner.enabled&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(Ee,{label:"Banner image",value:n.top_banner.image||null,onChange:a=>l("top_banner",{image:a}),folder:j,fileName:`${k}-top-banner`,hint:"Wide image works best - max 5MB"}),e.jsx(S,{value:n.top_banner.heading||"",onChange:a=>l("top_banner",{heading:a.target.value}),placeholder:"Heading (optional)",className:"h-8 text-xs"}),e.jsx(B,{value:n.top_banner.subheading||"",onChange:a=>l("top_banner",{subheading:a.target.value}),placeholder:"Supporting text (optional)",rows:2,className:"text-xs"})]})]}),e.jsxs("section",{className:"space-y-2 border-t pt-4",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Bottom banner (CTA)"}),e.jsx(G,{checked:!!n.bottom_banner.enabled,onCheckedChange:a=>l("bottom_banner",{enabled:a})})]}),n.bottom_banner.enabled&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(Ee,{label:"Banner image (optional)",value:n.bottom_banner.image||null,onChange:a=>l("bottom_banner",{image:a}),folder:j,fileName:`${k}-bottom-banner`,hint:"Wide image works best - max 5MB"}),e.jsx(S,{value:n.bottom_banner.heading||"",onChange:a=>l("bottom_banner",{heading:a.target.value}),placeholder:"Heading, e.g. Need help choosing?",className:"h-8 text-xs"}),e.jsx(B,{value:n.bottom_banner.subheading||"",onChange:a=>l("bottom_banner",{subheading:a.target.value}),placeholder:"Supporting text (optional)",rows:2,className:"text-xs"}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsx(S,{value:n.bottom_banner.buttonLabel||"",onChange:a=>l("bottom_banner",{buttonLabel:a.target.value}),placeholder:"Button label",className:"h-8 text-xs"}),e.jsx(S,{value:n.bottom_banner.buttonUrl||"",onChange:a=>l("bottom_banner",{buttonUrl:a.target.value}),placeholder:"https://...",className:"h-8 text-xs"})]}),e.jsxs("div",{className:"flex items-start justify-between gap-3 rounded-lg border p-2.5",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx(v,{className:"text-xs",children:"Show after submitting"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5",children:"Keeps the call to action visible on the thank-you screen."})]}
91),e.jsx(G,{checked:n.bottom_banner.showAfterSubmit!==!1,onCheckedChange:a=>l("bottom_banner",{showAfterSubmit:a})})]})]})]}),e.jsxs(y,{variant:"outline",size:"sm",className:"w-full",onClick:()=>N(structuredClone(Ns)),children:[e.jsx(ds,{className:"h-3.5 w-3.5 mr-1"})," Reset appearance"]})]})}const sa=[{key:"contact.entered",kind:"event",group:"entry",label:"A new customer enters DuceCRM",startsWhen:"a new customer arrives",entry:!0},{key:"contact.new_channel",kind:"event",group:"entry",label:"Customer reaches us on a new channel",startsWhen:"a customer reaches us somewhere new",entry:!0},{key:"contact.new_enquiry",kind:"event",group:"entry",label:"Existing customer starts a new enquiry",startsWhen:"an existing customer starts a new enquiry",entry:!0},{key:"contact.created",kind:"event",group:"customer",label:"Customer created",startsWhen:"a customer is added"},{key:"contact.lifecycle_changed",kind:"event",group:"customer",label:"Lifecycle changes",startsWhen:"a customer's stage changes"},{key:"contact.tag_added",kind:"event",group:"customer",label:"Tag added",startsWhen:"a tag is added to a customer"},{key:"form.submitted",kind:"event",group:"customer",label:"Form submitted",startsWhen:"someone submits a form"},{key:"deal.created",kind:"event",group:"deal",label:"Deal created",startsWhen:"a deal is created"},{key:"deal.stage_changed",kind:"event",group:"deal",label:"Stage changed",startsWhen:"a deal moves stage"},{key:"deal.proposal_sent",kind:"event",group:"deal",label:"Proposal sent",startsWhen:"a proposal is sent"},{key:"deal.won",kind:"event",group:"deal",label:"Deal won",startsWhen:"a deal is won"},{key:"deal.lost",kind:"event",group:"deal",label:"Deal lost",startsWhen:"a deal is lost"},{key:"quote.sent",kind:"event",group:"commercial",label:"Offer sent",startsWhen:"an offer is sent"},{key:"quote.accepted",kind:"event",group:"commercial",label:"Offer accepted",startsWhen:"an offer is accepted"},{key:"invoice.created",kind:"event",group:"commercial",label:"Invoice created",startsWhen:"an invoice is created"},{key:"invoice.approved",kind:"event",group:"commercial",label:"Invoice approved",startsWhen:"an invoice is approved"},{key:"invoice.sent",kind:"event",group:"commercial",label:"Invoice sent",startsWhen:"an invoice is sent to the customer"},{key:"invoice.viewed",kind:"event",group:"commercial",label:"Invoice viewed",startsWhen:"the customer opens the invoice page"},{key:"invoice.not_viewed",kind:"event",group:"commercial",label:"Invoice not opened yet",startsWhen:"an invoice is still unopened after a few days"},{key:"invoice.pay_link_clicked",kind:"event",group:"commercial",label:"Pay link clicked",startsWhen:"the customer clicks Pay now"},{key:"invoice.due",kind:"event",group:"commercial",label:"Invoice due",startsWhen:"an invoice becomes due"},{key:"invoice.overdue",kind:"event",group:"commercial",label:"Invoice overdue",startsWhen:"an invoice becomes overdue"},{key:"payment.received",kind:"event",group:"commercial",label:"Payment received",startsWhen:"a payment is received"},{key:"payment.partial",kind:"event",group:"commercial",label:"Part payment received",startsWhen:"part of the money arrives"},{key:"payment.failed",kind:"event",group:"commercial",label:"Payment failed",startsWhen:"a payment fails"},{key:"receipt.generated",kind:"event",group:"commercial",label:"Receipt created",startsWhen:"a receipt is created for a payment"},{key:"receipt.viewed",kind:"event",group:"commercial",label:"Receipt viewed",startsWhen:"the customer opens their receipt"},{key:"message.sent",kind:"event",group:"communication",label:"Message sent",startsWhen:"a message is sent"},{key:"message.replied",kind:"event",group:"communication",label:"Customer replied",startsWhen:"a customer replies"},{key:"link.clicked",kind:"event",group:"communication",label:"Tracked link clicked",startsWhen:"a customer clicks a tracked link"},{key:"message.no_reply",kind:"event",group:"communication",label:"No reply after a period",startsWhen:"there is still no reply"},{key:"booking.created",kind:"event",group:"bookings",label:"Booking created",startsWhen:"a booking is made"},{key:"booking.approaching",kind:"event",group:"bookings",label:"Booking approaching",startsWhen:"a booking is coming up"},{key:"booking.completed",kind:"event",group:"bookings",label:"Booking completed",startsWhen:"a booking is completed"},{key:"booking.missed",kind:"event",group:"bookings",label:"Booking missed",startsWhen:"a booking is missed"},{key:"date.birthday",kind:"date",group:"dates",label:"Birthday",startsWhen:"it is the customer's birthday"},{key:"date.customer_anniversary",kind:"date",group:"dates",label:"Customer anniversary",startsWhen:"a customer reaches an anniversary"},{key:"date.contract_renewal",kind:"date",group:"dates",label:"Contract renewal",startsWhen:"a contract is due for renewal",offsetDays:-30},{key:"date.subscription_renewal",kind:"date",group:"dates",label:"Subscription renewal",startsWhen:"a subscription renews",offsetDays:-7},{key:"date.invoice_due",kind:"date",group:"dates",label:"Invoice due date",startsWhen:"an invoice is nearly due",offsetDays:-3},{key:"date.custom",kind:"date",group:"dates",label:"Custom date",startsWhen:"a date you choose arrives"}];function ta(n){return n?sa.find(N=>N.key===n)??null:null}function aa(n){var N;return!!((N=ta(n))!=null&&N.entry)}const be=[{key:"text",label:"Short Text"},{key:"textarea",label:"Long Text"},{key:"email",label:"Email"},{key:"phone",label:"Phone"},{key:"number",label:"Number"},{key:"date",label:"Date"},{key:"time",label:"Time"},{key:"dropdown",label:"Dropdown"},{key:"checkbox",label:"Checkbox"},{key:"radio",label:"Radio"},{key:"rating",label:"Rating (1-5)"},{key:"recommendation",label:"Recommend score (0-10)"},{key:"referral_source",label:"How did you hear about us?"},{key:"file",label:"File Upload"},{key:"signature",label:"Signature"},{key:"catalog",label:"Products & Services"},{key:"hidden",label:"Hidden Field"}],na={text:"text",long_text:"textarea",email:"email",phone:"phone",number:"number",money:"number",date:"date",url:"text",choice:"dropdown",multi_choice:"checkbox",yes_no:"radio"},$a=()=>{var Ze;const{id:n}=zs(),N=Vs(),{goBack:_}=et(),[k]=Gs(),l=st(),{fields:j}=Ot("contacts","forms"),{data:t,isLoading:F}=lt(n),p=ct(),{data:A}
91=es({queryKey:["form-builder-team",l],queryFn:async()=>{if(!l)return[];const{data:s,error:r}=await Pe.from("users").select("id, full_name, email").eq("org_id",l).order("full_name");if(r)throw r;return s||[]},enabled:!!l}),{data:a=[]}=ft(l),{data:x=[]}=es({queryKey:["entry-journeys",l],queryFn:async()=>{const{data:s,error:r}=await Pe.from("campaigns").select("id, name, trigger_key, status").eq("org_id",l).eq("is_archived",!1).order("name");if(r)throw r;return(s||[]).filter(o=>aa(o.trigger_key))},enabled:!!l}),C=t&&l&&t.org_id!==l,[i,g]=f.useState([]),[b,m]=f.useState([]),[c,D]=f.useState(null),[E,H]=f.useState(null),[u,Z]=f.useState(""),[$,ne]=f.useState(""),[ee,de]=f.useState("Thank you for your submission!"),[ye,Oe]=f.useState(""),[me,We]=f.useState(!0),[Ne,Be]=f.useState(7),[we,Ue]=f.useState(!0),[ke,ue]=f.useState({}),[Ce,qe]=f.useState(""),[L,ze]=f.useState(()=>structuredClone(Ns)),[Se,Ss]=f.useState(!1),_s=k.get("view")==="embed"?"embed":"build",[re,K]=f.useState(_s),[_e,xe]=f.useState(null),[Ts,oe]=f.useState(null),[Ve,Te]=f.useState(null),[Is,ie]=f.useState(!1),[Ge,Ie]=f.useState("idle"),[Fs,se]=f.useState(k.get("tab")==="appearance"?"appearance":"form"),[As,He]=f.useState(!1),Es=s=>{var r;Z(s.name||u),ne(s.description||$),s.success_message&&de(s.success_message),m(s.sections),g(s.fields),ue(o=>({...o,...s.automations})),D(null),H(((r=s.sections[0])==null?void 0:r.id)??null),se("form"),K("build"),P.success("Form generated",{description:"Review the steps and questions, then save."})};if(t&&!Se){Z(t.name),ne(t.description||"");const s=t.settings||{};de(s.success_message||"Thank you for your submission!"),Oe(s.redirect_url||""),We(s.allow_respondent_edit!==!1),Be(Number(s.respondent_edit_days??7)||7),Ue(s.notify_team_email!==!1),ue(s.automations||{}),qe(t.entry_journey_id||""),ze(vt(t.style));const r=$t(Array.isArray(t.fields)?t.fields:[]);g(r),m(ys(s,r)),Ss(!0)}const Ds=(s,r)=>{ue(o=>({...o,[s]:{...o[s]||{},enabled:r}}))},Q=(s,r,o)=>{ue(w=>({...w,[s]:{...w[s]||{},[r]:o}}))},h=i.find(s=>s.id===c),W=b.find(s=>s.id===E),he=b.length>1,Ps=s=>{const r=b.map(d=>d.title),o=ns.find(d=>!r.includes(d)),w={id:crypto.randomUUID(),title:o||`Section ${b.length+1}`,description:""};m(d=>[...d,w]),H(w.id),D(null),se("section"),K("build")},te=(s,r)=>{m(o=>o.map(w=>w.id===s?{...w,...r}:w))},Rs=s=>{if(b.length<=1){P.success("Keep at least one section",{description:"A form needs one section to hold its fields."});return}const r=b.findIndex(d=>d.id===s),o=b[r-1]||b[r+1],w=ve(i,b[r],b);g(d=>d.map(I=>w.some(T=>T.id===I.id)?{...I,sectionId:o.id}:I)),m(d=>d.filter(I=>I.id!==s)),E===s&&H(null),w.length>0&&P.success("Section removed",{description:`${w.length} field(s) moved to "${o.title||"the first section"}".`})},Ls=(s,r)=>{!s||!r||s===r||m(o=>{const w=o.findIndex(Y=>Y.id===s),d=o.findIndex(Y=>Y.id===r);if(w===-1||d===-1)return o;const I=[...o],[T]=I.splice(w,1);return I.splice(d,0,T),I})},Ms=s=>{var d;const r=be.find(I=>I.key===s),o=E||(h?h.sectionId:null)||((d=b[b.length-1])==null?void 0:d.id)||rs,w={id:crypto.randomUUID(),type:s,label:(r==null?void 0:r.label)||"Field",required:!1,sectionId:o};g([...i,w]),D(w.id),se("field"),K("build")},$s=s=>{var w;const r=E||(h?h.sectionId:null)||((w=b[b.length-1])==null?void 0:w.id)||rs,o={id:crypto.randomUUID(),type:na[s.type]||"text",label:s.label,placeholder:s.placeholder,required:s.required,options:s.options.length>0?s.options.join(`
92`):void 0,sectionId:r,crmField:s.key};g([...i,o]),D(o.id),se("field"),K("build")},R=(s,r)=>{g(i.map(o=>o.id===s?{...o,...r}:o))},Os=s=>{g(i.filter(r=>r.id!==s)),c===s&&D(null)},Ws=(s,r)=>{!s||!r||s===r||g(o=>{var Y;const w=o.findIndex(Ae=>Ae.id===s),d=o.findIndex(Ae=>Ae.id===r);if(w===-1||d===-1)return o;const I=[...o],[T]=I.splice(w,1);return I.splice(d,0,{...T,sectionId:o[d].sectionId??((Y=b[0])==null?void 0:Y.id)}),I})},Bs=(s,r)=>{g(o=>{const w=o.findIndex(T=>T.id===s);if(w===-1)return o;const d=[...o],[I]=d.splice(w,1);return[...d,{...I,sectionId:r}]})},Ke=s=>({id:n,org_id:t==null?void 0:t.org_id,name:u,description:$||null,fields:i,settings:{success_message:ee,redirect_url:ye||null,allow_respondent_edit:me,respondent_edit_days:Ne,notify_team_email:we,automations:ke,sections:b},style:L,entry_journey_id:Ce||null,...s?{status:s}:{}}),Qe=async s=>{if(n)try{if(await p.mutateAsync(Ke(s)),s==="active"){P.success("Form published"),ie(!0);return}P.success("Form saved"),N("/forms",{replace:!0})}catch(r){P.error("That did not save",{description:it(r)})}},Fe=f.useRef(""),Ye=f.useRef(!0),pe=JSON.stringify({formName:u,formDesc:$,fields:i,sections:b,successMessage:ee,redirectUrl:ye,automations:ke,allowRespondentEdit:me,respondentEditDays:Ne,notifyTeamEmail:we,formStyle:L,entryJourneyId:Ce});f.useEffect(()=>{if(!Se||!n||C)return;if(Ye.current){Ye.current=!1,Fe.current=pe;return}if(Fe.current===pe)return;const s=setTimeout(async()=>{try{Ie("saving"),await p.mutateAsync(Ke()),Fe.current=pe,Ie("saved")}catch{Ie("idle")}},1500);return()=>clearTimeout(s)},[pe,Se,n,C]);const Us=ms(),qs=t!=null&&t.org_id?`?org=${t.org_id}`:"",le=t!=null&&t.slug?`${Us}/form/${t.slug}${qs}`:"",Je=t!=null&&t.slug?`<!-- DuceCRM Form Embed -->
93<div id="ducecrm-form-${t.slug}"></div>
94<script>
95(function(){
96  var d=document,f=d.createElement("iframe");
97  f.src="${le}";
98  f.style.cssText="width:100%;min-height:700px;border:0;border-radius:12px;";
99  f.loading="lazy";
100  f.title="DuceCRM Form";
101  d.getElementById("ducecrm-form-${t.slug}").appendChild(f);
102})();
103<\/script>`:"",Xe=(s,r)=>{navigator.clipboard.writeText(s),P.success(`${r} copied`)};return F?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"Loading..."}):C?e.jsxs("div",{className:"flex flex-col items-center justify-center py-20 space-y-4",children:[e.jsx(tt,{className:"h-12 w-12 text-destructive"}),e.jsx("h2",{className:"text-lg font-semibold",children:"This form belongs to another organisation"}),e.jsx("p",{className:"text-muted-foreground text-center max-w-md",children:"You cannot edit this form because it was created under a different organisation. Switch to the correct organisation or go back."}),e.jsxs(y,{variant:"outline",onClick:()=>N("/forms"),children:[e.jsx(De,{className:"h-4 w-4 mr-2"})," Back to Forms"]})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(y,{variant:"ghost",size:"icon",onClick:_,children:e.jsx(De,{className:"h-4 w-4"})}),e.jsx("div",{children:e.jsx(S,{value:u,onChange:s=>Z(s.target.value),className:"text-lg font-bold border-none p-0 h-auto 
103shadow-none focus-visible:ring-0",placeholder:"Form name"})})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs("div",{className:"flex items-center rounded-lg border bg-muted/50 p-0.5",children:[e.jsxs("button",{onClick:()=>K("build"),className:O("px-3 py-1.5 rounded-md text-xs font-medium transition-colors",re==="build"?"bg-background shadow-sm":"hover:bg-muted"),children:[e.jsx(at,{className:"h-3 w-3 inline mr-1"}),"Build"]}),e.jsxs("button",{onClick:()=>K("preview"),className:O("px-3 py-1.5 rounded-md text-xs font-medium transition-colors",re==="preview"?"bg-background shadow-sm":"hover:bg-muted"),children:[e.jsx(Wt,{className:"h-3 w-3 inline mr-1"}),"Preview"]}),e.jsxs("button",{onClick:()=>K("embed"),className:O("px-3 py-1.5 rounded-md text-xs font-medium transition-colors",re==="embed"?"bg-background shadow-sm":"hover:bg-muted"),children:[e.jsx(je,{className:"h-3 w-3 inline mr-1"}),"Embed"]})]}),e.jsx("span",{className:"text-[11px] text-muted-foreground min-w-[52px]",children:Ge==="saving"?"Saving...":Ge==="saved"?"Saved":""}),e.jsx(ce,{variant:(t==null?void 0:t.status)==="active"?"success":(t==null?void 0:t.status)==="paused"?"warning":"secondary",children:(t==null?void 0:t.status)||"draft"}),e.jsxs(y,{variant:"outline",onClick:()=>He(!0),children:[e.jsx(Re,{className:"h-4 w-4 mr-1"})," AI build"]}),(t==null?void 0:t.status)==="draft"?e.jsxs(y,{variant:"outline",onClick:()=>Qe("active"),disabled:p.isPending,children:[e.jsx(Bt,{className:"h-4 w-4 mr-1"})," Publish"]}):(t==null?void 0:t.status)==="active"?e.jsxs(y,{variant:"outline",onClick:()=>ie(!0),children:[e.jsx(Ut,{className:"h-4 w-4 mr-1"})," Share"]}):null,e.jsxs(y,{onClick:()=>Qe(),disabled:p.isPending,children:[p.isPending?e.jsx(Le,{className:"h-4 w-4 mr-1 animate-spin"}):e.jsx(qt,{className:"h-4 w-4 mr-1"})," Save"]})]})]}),re==="preview"&&e.jsxs("div",{className:"relative min-h-[calc(100vh-180px)] rounded-lg border bg-background p-8",style:St(L),children:[L.background.image&&(L.background.overlay||0)>0&&e.jsx("div",{className:"absolute inset-0 rounded-lg pointer-events-none",style:{backgroundColor:`rgba(0,0,0,${(L.background.overlay||0)/100})`}}),e.jsxs("div",{className:"relative max-w-lg mx-auto space-y-4",style:{...Ct(L),...kt(L)},children:[e.jsx(jt,{style:L}),e.jsx("div",{className:O("bg-card border shadow-sm p-6",Nt[L.card.radius||"rounded"]),style:yt(L),children:e.jsx(zt,{formName:u,formDesc:$,fields:i,sections:b,successMessage:ee})}),e.jsx(wt,{style:L})]})]}),re==="embed"&&e.jsx("div",{className:"min-h-[calc(100vh-180px)] rounded-lg border bg-card p-6",children:e.jsxs("div",{className:"max-w-2xl mx-auto",children:[e.jsx("h3",{className:"text-lg font-semibold mb-1",children:"Share & Embed"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:"Get a shareable link, a QR code, or embed this form on your website"}),e.jsx(Gt,{formSlug:(t==null?void 0:t.slug)||"",formId:n||"",formStatus:(t==null?void 0:t.status)||"draft",orgId:t==null?void 0:t.org_id,formName:u,formDescription:$})]})}),re==="build"&&e.jsxs("div",{className:"flex flex-col lg:flex-row gap-4 min-h-[calc(100vh-180px)]",children:[e.jsxs("div",{className:"lg:w-56 shrink-0 rounded-lg border bg-card p-3 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-xs font-semibold uppercase text-muted-foreground mb-2 lg:block hidden",children:"Add Fields"}),e.jsx("div",{className:"flex lg:flex-col gap-1 overflow-x-auto lg:overflow-x-visible pb-2 lg:pb-0",children:be.map(s=>e.jsxs("button",{onClick:()=>Ms(s.key),className:"whitespace-nowrap lg:whitespace-normal w-auto lg:w-full text-left flex items-center gap-2 px-2 py-1.5 rounded text-xs hover:bg-muted transition-colors",children:[e.jsx(fe,{className:"h-3 w-3 text-muted-foreground shrink-0"}),s.label]},s.key))})]}),e.jsxs("div",{className:"border-t pt-3 space-y-1.5",children:[e.jsx("h3",{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Collecting a review?"}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Customer Care owns reviews, so answers, sentiment and follow-up stay in one place."}),e.jsx(y,{variant:"outline",size:"sm",className:"w-full",asChild:!0,children:e.jsx(Hs,{to:"/customer-care?tab=reviews",children:"Use Reviews instead"})})]}),j.length>0&&e.jsxs("div",{className:"border-t pt-3",children:[e.jsx("h3",{className:"text-xs font-semibold uppercase text-muted-foreground mb-2",children:"From your CRM"}),e.jsx("p",{className:"text-[11px] text-muted-foreground mb-2",children:"Ask for information you already collect, so answers land in the right place."}),e.jsx("div",{className:"flex lg:flex-col gap-1 overflow-x-auto lg:overflow-x-visible pb-2 lg:pb-0",children:j.map(s=>e.jsxs("button",{onClick:()=>$s(s),className:"whitespace-nowrap lg:whitespace-normal w-auto lg:w-full text-left flex items-center gap-2 px-2 py-1.5 rounded text-xs hover:bg-muted transition-colors",children:[e.jsx(fe,{className:"h-3 w-3 text-muted-foreground shrink-0"}),s.label]},s.key))})]}),e.jsxs("div",{className:"border-t pt-3",children:[e.jsx("h3",{className:"text-xs font-semibold uppercase text-muted-foreground mb-2",children:"Pages"}),e.jsxs(y,{variant:"outline",size:"sm",className:"w-full text-xs h-8",onClick:()=>Ps(),children:[e.jsx(ks,{className:"h-3 w-3 mr-1"})," Add section"]}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-1.5",children:"Each section becomes its own step with Next and Previous buttons."})]})]}),e.jsx("div",{className:"flex-1 rounded-lg border bg-muted/30 p-4",children:i.length===0&&b.length<=1?e.jsx("div",{className:"flex items-center justify-center h-full text-muted-foreground text-sm",children:"Click a field type to add it to your form"}):e.jsx("div",{className:"space-y-3 max-w-lg mx-auto",children:b.map((s,r)=>{const o=ve(i,s,b),w=!!s.collapsed;return e.jsxs("div",{className:O("rounded-lg border bg-card/60 transition-colors",E===s.id&&"border-primary ring-1 ring-primary/30"),onDragOver:d=>d.preventDefault(),onDrop:d=>{d.preventDefault();const I=d.dataTransfer.getData("text/plain");if(Ve){Ls(Ve,s.id),Te(null);return}I&&!o.some(T=>T.id===I)&&Bs(I,s.id),xe(null),oe(null)},children:[e.jsxs("div",{draggable:he,onDragStart:d=>{Te(s.id),d.dataTransfer.effectAllowed="move",d.dataTransfer.setData("text/plain","")},onDragEnd:()=>Te(null),onClick:()=>{H(s.id),D(null),se("section")},className:"flex items-center gap-2 px-3 py-2 border-b cursor-pointer",children:[he&&e.jsx(is,{className:"h-4 w-4 text-muted-foreground shrink-0 cursor-grab"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-1 min-w-0",children:[he&&e.jsxs("span",{className:"text-sm font-semibold text-muted-foreground shrink-0",children:["Step ",r+1,":"]}),e.jsx(S,{value:s.title,onChange:d=>te(s.id,{title:d.target.value}),onClick:d=>d.stopPropagation(),placeholder:"Untitled section",className:"text-sm font-semibold border-none bg-transparent p-0 h-auto 
103shadow-none focus-visible:ring-0 focus-visible:bg-background focus-visible:px-1 focus-visible:rounded"})]}),e.jsx(S,{value:s.description||"",onChange:d=>te(s.id,{description:d.target.value}),onClick:d=>d.stopPropagation(),placeholder:"Add a subtitle (optional)",className:"text-xs text-muted-foreground border-none bg-transparent p-0 h-auto shadow-none focus-visible:ring-0 focus-visible:bg-background focus-visible:px-1 focus-visible:rounded"}),e.jsxs("p",{className:"text-caption",children:[o.length," field",o.length===1?"":"s",s.condition?" - conditional":""]})]}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onClick:d=>{d.stopPropagation(),te(s.id,{collapsed:!w})},children:w?e.jsx(nt,{className:"h-3.5 w-3.5"}):e.jsx(rt,{className:"h-3.5 w-3.5"})}),b.length>1&&e.jsx(y,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onClick:d=>{d.stopPropagation(),Rs(s.id)},children:e.jsx($e,{className:"h-3 w-3 text-muted-foreground"})})]}),!w&&e.jsxs("div",{className:"p-2 space-y-2",children:[o.length===0&&e.jsx("p",{className:"text-xs text-muted-foreground text-center py-4",children:"Drag a field here, or select this section and add one."}),o.map(d=>{var I;return e.jsxs("div",{draggable:!0,onDragStart:T=>{xe(d.id),T.dataTransfer.effectAllowed="move",T.dataTransfer.setData("text/plain",d.id)},onDragEnd:()=>{xe(null),oe(null)},onDragOver:T=>{T.preventDefault(),T.dataTransfer.dropEffect="move"},onDragEnter:T=>{T.preventDefault(),_e&&_e!==d.id&&oe(d.id)},onDragLeave:()=>{oe(T=>T===d.id?null:T)},onDrop:T=>{T.preventDefault(),T.stopPropagation();const Y=T.dataTransfer.getData("text/plain");Ws(Y,d.id),xe(null),oe(null)},onClick:()=>{D(d.id),H(null),se("field")},className:O("flex items-center gap-2 rounded-lg border bg-card p-3 cursor-pointer transition-colors",c===d.id&&"border-primary ring-1 ring-primary/30",Ts===d.id&&_e!==d.id&&"border-primary"),children:[e.jsx(is,{className:"h-4 w-4 text-muted-foreground shrink-0 cursor-grab"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("p",{className:"text-sm font-medium",children:[d.label,d.required&&e.jsx("span",{className:"text-destructive ml-1",children:"*"})]}),e.jsxs("p",{className:"text-caption",children:[(I=be.find(T=>T.key===d.type))==null?void 0:I.label,d.condition?" • conditional":""]})]}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onClick:T=>{T.stopPropagation(),Os(d.id)},children:e.jsx($e,{className:"h-3 w-3 text-muted-foreground"})})]},d.id)})]})]},s.id)})})}),e.jsx("div",{className:"lg:w-72 shrink-0 rounded-lg border bg-card p-3 overflow-y-auto",children:e.jsxs(vs,{value:Fs,onValueChange:s=>se(s),children:[e.jsxs(js,{className:"w-full flex-wrap h-auto",children:[e.jsx(J,{value:"form",className:"flex-1 text-xs",children:"Form"}),e.jsx(J,{value:"section",className:"flex-1 text-xs",disabled:!W,children:"Section"}),e.jsx(J,{value:"field",className:"flex-1 text-xs",disabled:!h,children:"Field"}),e.jsx(J,{value:"automations",className:"flex-1 text-xs",children:"Actions"}),e.jsx(J,{value:"appearance",className:"flex-1 text-xs",children:"Design"})]}),e.jsxs(X,{value:"appearance",className:"mt-3",children:[e.jsx("p",{className:"text-[11px] text-muted-foreground mb-3",children:"Brand the public form page. Use the Preview tab to see it live."}),e.jsx(ea,{style:L,onChange:ze,orgId:(t==null?void 0:t.org_id)||l,formId:n||"form"})]}),e.jsx(X,{value:"section",className:"space-y-3 mt-3",children:W&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Section title"}),e.jsx(S,{value:W.title,onChange:s=>te(W.id,{title:s.target.value}),placeholder:"e.g. Personal Information",className:"text-xs"})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Section description"}),e.jsx(B,{value:W.description||"",onChange:s=>te(W.id,{description:s.target.value}),rows:2,className:"text-xs",placeholder:"Shown under the section title"})]}),e.jsx("div",{className:"flex flex-wrap gap-1",children:ns.map(s=>e.jsx("button",{onClick:()=>te(W.id,{title:s}),className:"rounded-full border px-2 py-0.5 text-[10px] hover:bg-muted transition-colors",children:s},s))}),e.jsx(ls,{subject:"this section",condition:W.condition,sourceFields:i.filter(s=>{var r;return(s.sectionId??((r=b[0])==null?void 0:r.id))!==W.id}),onChange:s=>te(W.id,{condition:s})}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Sections that fail the
103ir rules are skipped automatically when the visitor taps Next."})]})}),e.jsxs(X,{value:"form",className:"space-y-3 mt-3",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(v,{className:"text-xs",children:"Description"}),e.jsx(ts,{fieldKind:"form_description",value:$,onChange:ne,record:{form_name:u},recordType:"forms",recordId:n??null,module:"forms"})]}),e.jsx(B,{value:$,onChange:s=>ne(s.target.value),rows:2,className:"text-xs"})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(v,{className:"text-xs",children:"Success Message"}),e.jsx(ts,{fieldKind:"form_success_message",value:ee,onChange:de,record:{form_name:u,form_description:$},recordType:"forms",recordId:n??null,module:"forms"})]}),e.jsx(B,{value:ee,onChange:s=>de(s.target.value),rows:2,className:"text-xs"})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Redirect URL (optional)"}),e.jsx(S,{value:ye,onChange:s=>Oe(s.target.value),placeholder:"https://yoursite.com/thank-you",className:"text-xs"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-1",children:"Leave empty to show the success message instead."})]}),e.jsxs("div",{className:"flex items-start justify-between gap-3 rounded-lg border p-2.5",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx(v,{className:"text-xs",children:"Let respondents edit their answers"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5",children:"Shows a receipt after submitting and emails a private link so they can fix mistakes."})]}),e.jsx(G,{checked:me,onCheckedChange:We})]}),me&&e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Editing window (days)"}),e.jsx(S,{type:"number",min:1,max:90,value:Ne,onChange:s=>Be(Math.max(1,Math.min(90,Number(s.target.value)||1))),className:"text-xs"})]}),e.jsxs("div",{className:"flex items-start justify-between gap-3 rounded-lg border p-2.5",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx(v,{className:"text-xs",children:"Email the team on every response"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5",children:"In-app notifications are always sent to your organisation members."})]}),e.jsx(G,{checked:we,onCheckedChange:Ue})]})]}),e.jsxs(X,{value:"automations",className:"space-y-3 mt-3",children:[e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Choose what happens automatically after each submission."}),e.jsxs("div",{className:"rounded-lg border p-2.5 space-y-2",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:"Start a journey"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"New people get the welcome sequence. Someone you already know keeps their history and is never welcomed twice."})]}),e.jsxs(U,{value:Ce||"none",onValueChange:s=>qe(s==="none"?"":s),children:[e.jsx(q,{className:"text-xs h-8",children:e.jsx(z,{placeholder:"No journey"})}),e.jsxs(V,{children:[e.jsx(M,{value:"none",className:"text-xs",children:"No journey"}),x.map(s=>e.jsx(M,{value:s.id,className:"text-xs",children:s.name||"Untitled journey"},s.id))]})]}),x.length===0&&e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"No new customer journeys yet. Create one in Campaigns to welcome people who submit this form."})]}),_t.map(s=>{const r=ke[s]||{};return e.jsxs("div",{className:"rounded-lg border p-2.5 space-y-2",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-xs font-medium",children:Me[s].label}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:Me[s].description})]}),e.jsx(G,{checked:!!r.enabled,onCheckedChange:o=>Ds(s,o)})]}),r.enabled&&s==="create_task"&&e.jsx(S,{value:r.title||"",onChange:o=>Q(s,"title",o.target.value),placeholder:"Task title e.g. Follow up on form",className:"text-xs h-8"}),r.enabled&&s==="add_tags"&&e.jsx(S,{value:r.tags||"",onChange:o=>Q(s,"tags",o.target.value),placeholder:"Comma-separated tags",className:"text-xs h-8"}),r.enabled&&s==="create_deal"&&e.jsx(S,{value:r.value_field||"",onChange:o=>Q(s,"value_field",o.target.value),placeholder:"Field id holding deal value (optional)",className:"text-xs h-8"}),r.enabled&&s==="create_invoice"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(S,{type:"number",min:1,max:90,value:r.due_days??7,onChange:o=>Q(s,"due_days",o.target.value),placeholder:"Payment due in days",className:"text-xs h-8"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Needs a Products & Services field. The customer gets a private invoice link and can upload proof of payment. Receipts go out only after your team confirms the payment."})]}),r.enabled&&s==="assign_owner"&&e.jsxs(U,{value:r.user_id||"",onValueChange:o=>Q(s,"user_id",o),children:[e.jsx(q,{className:"text-xs h-8",children:e.jsx(z,{placeholder:"Choose a team member"})}),e.jsx(V,{children:(A||[]).map(o=>e.jsx(M,{value:o.id,className:"text-xs",children:o.full_name||o.email||"Team member"},o.id))})]}),r.enabled&&s==="send_email"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(S,{value:r.subject||"",onChange:o=>Q(s,"subject",o.target.value),placeholder:"Email subject (optional)",className:"text-xs h-8"}),e.jsx(B,{value:r.body||"",onChange:o=>Q(s,"body",o.target.value),placeholder:"Email message (defaults to your success message)",rows:2,className:"text-xs"})]}),r.enabled&&s==="send_whatsapp"&&e.jsx(B,{value:r.message||"",onChange:o=>Q(s,"message",o.target.value),placeholder:"WhatsApp message (defaults to your success message)",rows:2,className:"text-xs"})]},s)})]}),e.jsx(X,{value:"field",className:"space-y-3 mt-3",children:h&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Label"}),e.jsx(S,{value:h.label,onChange:s=>R(h.id,{label:s.target.value}),className:"text-xs"})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Placeholder"}),e.jsx(S,{value:h.placeholder||"",onChange:s=>R(h.id,{placeholder:s.target.value}),className:"text-xs"})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(v,{className:"text-xs",children:"Required"}),e.jsx(G,{checked:h.required,onCheckedChange:s=>R(h.id,{required:s})})]}),he&&e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Section"}),e.jsxs(U,{value:h.sectionId||((Ze=b[0])==null?void 0:Ze.id),onValueChange:s=>R(h.id,{sectionId:s}),children:[e.jsx(q,{className:"text-xs h-8",children:e.jsx(z,{})}),e.jsx(V,{children:b.map((s,r)=>e.jsxs(M,{value:s.id,className:"text-xs",children:[r+1,". ",s.title||"Untitled section"]},s.id))})]})]}),(h.type==="dropdown"||h.type==="radio"||h.type==="checkbox")&&e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Options (comma-separated)"}),e.jsx(B,{value:h.options||"",onChange:s=>R(h.id,{options:s.target.value}),rows:3,className:"text-xs",placeholder:"Option 1, Option 2, Option 3"})]}),h.type==="catalog"&&e.jsxs("div",{className:"space-y-2 rounded-lg border p-2.5",children:[e.jsx("p",{className:"text-[11px] text-muted-foreground",children:'Visitors pick from your catalog. Turn on "Create Draft Invoice" under Actions to bill the order automatically.'}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(v,{className:"text-xs",children:"Allow multiple items"}),e.jsx(G,{checked:h.catalogMultiple!==!1,onCheckedChange:s=>R(h.id,{catalogMultiple:s})})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(v,{className:"text-xs",children:"Allow quantity"}),e.jsx(G,{checked:h.catalogQuantity!==!1,onCheckedChange:s=>R(h.id,{catalogQuantity:s})})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Items shown"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mb-1",children:"Nothing selected = show every active catalog item."}),e.jsxs("div",{className:"max-h-44 overflow-y-auto space-y-1 rounded-md border p-1.5",children:[a.length===0&&e.jsx("p",{className:"text-[10px] text-muted-foreground p-1",children:"No active catalog items 
103yet. Add them under Catalog."}),a.map(s=>{const r=(h.catalogItemIds||[]).includes(s.id);return e.jsxs("button",{type:"button",onClick:()=>{const o=h.catalogItemIds||[];R(h.id,{catalogItemIds:r?o.filter(w=>w!==s.id):[...o,s.id]})},className:O("w-full text-left rounded px-2 py-1 text-[11px] transition-colors",r?"bg-primary/10 text-foreground":"hover:bg-muted"),children:[e.jsx("span",{className:"font-medium",children:s.name}),s.price?e.jsxs("span",{className:"text-muted-foreground",children:[" ","- ",Tt(Number(s.price),s.currency_code)]}):null]},s.id)})]})]})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Field Type"}),e.jsxs(U,{value:h.type,onValueChange:s=>R(h.id,{type:s}),children:[e.jsx(q,{className:"text-xs",children:e.jsx(z,{})}),e.jsx(V,{children:be.map(s=>e.jsx(M,{value:s.key,children:s.label},s.key))})]})]}),h.type==="number"?e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Min"}),e.jsx(S,{type:"number",value:h.min??"",onChange:s=>R(h.id,{min:s.target.value===""?void 0:Number(s.target.value)}),className:"text-xs h-8"})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Max"}),e.jsx(S,{type:"number",value:h.max??"",onChange:s=>R(h.id,{max:s.target.value===""?void 0:Number(s.target.value)}),className:"text-xs h-8"})]})]}):h.type==="text"||h.type==="textarea"?e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Min length"}),e.jsx(S,{type:"number",value:h.minLength??"",onChange:s=>R(h.id,{minLength:s.target.value===""?void 0:Number(s.target.value)}),className:"text-xs h-8"})]}),e.jsxs("div",{children:[e.jsx(v,{className:"text-xs",children:"Max length"}),e.jsx(S,{type:"number",value:h.maxLength??"",onChange:s=>R(h.id,{maxLength:s.target.value===""?void 0:Number(s.target.value)}),className:"text-xs h-8"})]})]}):null,e.jsx(ls,{subject:"this field",condition:h.condition,sourceFields:i.filter(s=>s.id!==h.id),onChange:s=>R(h.id,{condition:s})})]})})]})})]}),e.jsx(xs,{open:Is,onOpenChange:ie,children:e.jsxs(hs,{size:"md",className:"",children:[e.jsxs(ps,{children:[e.jsxs(gs,{className:"flex items-center gap-2",children:[e.jsx(ot,{className:"h-5 w-5 text-success"}),"Form is live - share it now"]}),e.jsx(bs,{children:"Copy the link to send directly, or grab the embed code to put it on your website."})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"Direct link"}),e.jsxs("div",{className:"flex items-center gap-2 rounded-lg border bg-muted/50 p-2",children:[e.jsx(ws,{className:"h-4 w-4 text-muted-foreground shrink-0"}),e.jsx("code",{className:"flex-1 text-xs truncate",children:le}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-7 w-7 shrink-0",onClick:()=>Xe(le,"Link"),children:e.jsx(ae,{className:"h-3.5 w-3.5"})}),e.jsx(y,{variant:"ghost",size:"icon",className:"h-7 w-7 shrink-0",onClick:()=>window.open(le,"_blank"),children:e.jsx(us,{className:"h-3.5 w-3.5"})})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Send this link by email, SMS, or social - anyone with the link can fill it in."})]}),e.jsx(Cs,{url:le,formName:u||"Form",formDescription:$}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(v,{className:"text-xs font-semibold uppercase text-muted-foreground",children:"HTML embed"}),e.jsx("div",{className:"rounded-lg bg-muted p-3 font-mono text-[11px] max-h-40 overflow-auto whitespace-pre-wrap",children:Je}),e.jsxs(y,{variant:"outline",size:"sm",onClick:()=>Xe(Je,"HTML embed"),children:[e.jsx(ae,{className:"h-3.5 w-3.5 mr-1"})," Copy HTML"]}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Paste this snippet into your website HTML where you want the form to appear."})]})]}),e.jsxs(fs,{className:"gap-2 sm:gap-2",children:[e.jsxs(y,{variant:"outline",onClick:()=>{K("embed"),ie(!1)},children:[e.jsx(je,{className:"h-4 w-4 mr-1"})," Open Embed tab"]}),e.jsx(y,{onClick:()=>ie(!1),children:"Done"})]})]})}),e.jsx(Jt,{open:As,onOpenChange:He,existing:{name:u,description:$,sections:b,fields:i},onApply:Es})]})};export{$a 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.