1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3287],{23287:(e,t,r)=>{r.r(t),r.d(t,{ComputedEditor:()=>N});var n=r(54568),s=r(7620),l=r(53135),a=r(22322),u=r(44513);function o(e,t){return"string"==typeof e?+!!e.includes(t):Array.isArray(e)?e.reduce((e,r)=>e+o(r,t),0):e&&"object"==typeof e?Object.values(e).reduce((e,r)=>e+o(r,t),0):0}let c=(e,t,r)=>"".concat(e," ").concat(1===e?t:r);var i=r(68217),d=r(95409),m=r(17629);let p=new Set(["PENDING","RUNNING"]);var f=r(2658),x=r(83127),h=r(33444);function v(e){var t;let{text:r,schema:l,texts:a,scopeBlockId:u=null}=e,[o,c]=(0,s.useState)({}),i=(0,s.useMemo)(()=>{try{return(0,x.acg)((0,x.iBq)(r,l))}catch(e){return[]}},[r,l]),d=(0,s.useMemo)(()=>(function(e,t,r,n){try{let s=(0,x.iBq)(e,t);return{value:(0,x._3z)((0,x.qgA)(s),{schema:t,answers:r,scopeBlockId:n})}}catch(e){return{error:e instanceof x.tjM?e.message:(null==e?void 0:e.message)||"This formula cannot be evaluated."}}})(r,l,function(e,t){let r={};for(let[n,s]of Object.entries(t)){let t=e.questions[n],l=(null!=s?s:"").trim();if(!t||!l)continue;if((0,x.kDJ)(t.type)){r[n]={selected:l.split(",").map(e=>e.trim()).filter(Boolean)};continue}let a=Number(l);if(""!==l&&Number.isFinite(a)&&!["OPEN","AUTOCOMPLETE","SINGLE","DROPDOWN"].includes(t.type)){r[n]={value:a};continue}r[n]="OPEN"===t.type||"AUTOCOMPLETE"===t.type?{text:l}:{value:l}}return r}(l,o),u),[r,l,o,u]);return 0===i.length?null:(0,n.jsxs)("div",{className:"rounded-lg border border-dashed border-primary/30 bg-primary/5 p-3",children:[(0,n.jsx)("p",{className:"mb-2 text-xs font-medium",children:"Try it with sample answers"}),(0,n.jsx)("div",{className:"space-y-1.5",children:i.map(e=>{var t,r,s;let u=l.questions[e];if(!u)return null;let i=null!=(t=u.name)?t:e;return(0,n.jsxs)("label",{className:"flex items-center gap-2 text-xs",children:[(0,n.jsx)("span",{className:"w-12 shrink-0 font-mono text-[11px] font-semibold text-primary",children:null!=(r=u.name)?r:"?"}),(0,n.jsx)("span",{className:"min-w-0 flex-1 truncate text-muted-foreground",children:(null==a?void 0:a[e])||u.type}),(0,n.jsx)(h.p,{"aria-label":"Sample answer for ".concat(i),value:null!=(s=o[e])?s:"",onChange:t=>c(r=>({...r,[e]:t.target.value})),className:"h-7 w-28 text-xs",placeholder:"answer"})]},e)})}),(0,n.jsx)("p",{"data-testid":"formula-preview-result",className:"mt-2 font-mono text-sm",children:d.error?(0,n.jsx)("span",{className:"text-destructive",children:d.error}):(0,n.jsxs)(n.Fragment,{children:["= ",null==(t=d.value)?"blank":Array.isArray(t)?t.join(", "):"boolean"==typeof t?t?"true":"false":String(t)]})})]})}var y=r(95400),g=r(65968);let b=[{value:"number",label:"Number"},{value:"text",label:"Text"},{value:"boolean",label:"True / false"}];function N(e){var t,r,x;let{question:N,onChange:j,disabled:w}=e,{schema:k,texts:R,isLoopBlock:T}=(0,u.f)(),E=(0,a.V0)(e=>e.questions),C=(0,a.V0)(e=>e.survey),D=null==C?void 0:C.flow,S=null!=(t=N.computed)?t:{expression:"",outputType:"number"},A=null!=(r=N.blockId)?r:null,[F,I]=(0,s.useState)(()=>({text:(0,i.R)(S.expression,k)})),M=F.type&&"unknown"!==F.type&&F.type!==S.outputType?"This formula produces ".concat(F.type,"; change the output type or the formula."):null,q=(0,s.useMemo)(()=>(function(e){let t=[];return(e.conditions&&t.push(c(e.conditions,"condition","conditions")),e.setRules&&t.push(c(e.setRules,"flow rule","flow rules")),e.computed&&t.push(c(e.computed,"computed question","computed questions")),t.length)?"Used by ".concat(1===t.length?t[0]:t.slice(0,-1).join(", ")+" and "+t[t.length-1]):"Not used by any logic yet"})(function(e,t,r){var n,s,l;let a="{q:".concat(e,"}"),u=0,c=0;for(let e of t)u+=o([e.logic,e.displayLogic,e.entryLogic,e.consistencyChecks,e.options,null==(n=e.validation)?void 0:n.rows],a),c+=o(null==(s=e.computed)?void 0:s.expression,a);return{conditions:u,setRules:o(null!=(l=null==r?void 0:r.setRules)?l:[],a),computed:c}}(N.id,E,D)),[N.id,E,D]),O=e=>j({computed:{...S,...e}}),{run:L,start:P,starting:U}=function(e){let[t,r]=(0,s.useState)(null),[n,l]=(0,s.useState)(!1),{toast:u}=(0,m.d)(),o=(0,a.V0)(e=>e.isSaving),c=(0,s.useRef)(null),i=(0,s.useRef)(!1),f=(0,s.useRef)(!1),x=(0,s.useRef)(null),h=(0,s.useRef)(null),v=()=>{i.current=!0,f.current=!1,x.current=null,c.current&&clearTimeout(c.current),c.current=null},y=(0,s.useCallback)(async()=>
1{if(e)try{let n=await d.FH.get("/computed/surveys/".concat(e,"/runs/latest"));if(!i.current){var t;h.current=null!=(t=null==n?void 0:n.status)?t:null,r(n)}}catch(e){}},[e]),g=(0,s.useCallback)(()=>{if(f.current||!e||null!==x.current&&Date.now()>=x.current)return;null===x.current&&(x.current=Date.now()+6e5),f.current=!0,i.current=!1;let t=async()=>{if(i.current){f.current=!1;return}let n=null,s=!1;try{n=await d.FH.get("/computed/surveys/".concat(e,"/runs/latest"))}catch(e){s=!0}if(i.current){f.current=!1;return}n&&(h.current=n.status,r(n));let l=s?!!h.current&&p.has(h.current):!!n&&p.has(n.status);l&&null!==x.current&&Date.now()<x.current?c.current=setTimeout(t,2e3):(f.current=!1,c.current=null,l||(x.current=null))};t()},[e]);(0,s.useEffect)(()=>(i.current=!1,y(),v),[y]);let b=(0,s.useRef)(o);(0,s.useEffect)(()=>{b.current&&!o&&(x.current=null,g()),b.current=o},[o,g]),(0,s.useEffect)(()=>{t&&p.has(t.status)&&g()},[t,g]);let N=(0,s.useCallback)(async()=>{if(e){l(!0);try{let t=await d.FH.post("/computed/surveys/".concat(e,"/recompute"),{});h.current=t.status,r(e=>({...null!=e?e:{},id:t.id,status:t.status})),x.current=null,g()}catch(e){u((null==e?void 0:e.message)||"Could not start the recompute.","error")}finally{l(!1)}}},[e,g,u]);return{run:t,start:N,starting:n,refresh:y}}(null==C?void 0:C.id),G=(null==C?void 0:C.status)!=="DRAFT"||!!L,B=!!L&&("PENDING"===L.status||"RUNNING"===L.status);return(0,n.jsxs)("div",{className:"space-y-4",children:[(0,n.jsxs)("div",{className:"flex items-start gap-2 rounded-lg border border-dashed border-muted-foreground/30 bg-muted/20 p-3 text-[11px] text-muted-foreground",children:[(0,n.jsx)(l.A,{className:"h-3.5 w-3.5 shrink-0 mt-0.5","aria-hidden":!0}),(0,n.jsx)("p",{children:"Never shown to respondents â this value recalculates after every answer."})]}),(0,n.jsxs)("div",{className:"space-y-1",children:[(0,n.jsx)("label",{className:"text-sm font-medium",htmlFor:"computed-formula",children:"Formula"}),(0,n.jsx)(f.F,{id:"computed-formula",canonical:S.expression,schema:k,texts:R,scopeBlockId:A,inLoop:T(A),ownerQuestionId:N.id,disabled:w,onCommit:e=>{e&&O({expression:e})},onValidation:I}),M&&(0,n.jsx)("p",{className:"text-[11px] text-sv-2a-warn-ink",children:M})]}),(0,n.jsx)(v,{text:F.text,schema:k,texts:R,scopeBlockId:A}),(0,n.jsxs)("div",{className:"grid grid-cols-2 gap-3",children:[(0,n.jsxs)("div",{className:"space-y-1",children:[(0,n.jsx)("label",{className:"text-sm font-medium",htmlFor:"computed-output-type",children:"Output type"}),(0,n.jsx)(g.l,{id:"computed-output-type",className:"h-8 text-sm",value:S.outputType,disabled:w,onChange:e=>O({outputType:e.target.value}),children:b.map(e=>(0,n.jsx)("option",{value:e.value,children:e.label},e.value))})]}),"number"===S.outputType&&(0,n.jsxs)("div",{className:"space-y-1",children:[(0,n.jsx)("label",{className:"text-sm font-medium",htmlFor:"computed-decimals",children:"Decimals"}),(0,n.jsx)(h.p,{id:"computed-decimals",type:"number",min:0,max:10,className:"h-8 text-sm",value:null!=(x=S.decimals)?x:"",disabled:w,onChange:e=>O({decimals:""===e.target.value?void 0:Math.max(0,Math.min(10,Number(e.target.value)))})})]})]}),(0,n.jsx)("p",{className:"text-[11px] text-muted-foreground",children:q}),G&&(0,n.jsxs)("div",{className:"rounded-lg border p-3 space-y-2",children:[(0,n.jsxs)("div",{className:"flex items-center justify-between gap-2",children:[(0,n.jsx)("p",{className:"text-sm font-medium",children:"Stored values"}),(0,n.jsx)(y.$,{size:"sm",variant:"outline",onClick:()=>void P(),disabled:w||U||B,children:(null==L?void 0:L.status)==="FAILED"?"Try again":"Recompute now"})]}),(0,n.jsx)("p",{className:"text-[11px] text-muted-foreground","aria-live":"polite",children:function(e){var t,r;if(!e)return"Stored values are recomputed automatically when you change the formula.";let n=e=>(null!=e?e:0).toLocaleString("en-US");return"PENDING"===e.status?"Recompute queuedâ¦":"RUNNING"===e.status?"Recomputing ".concat(n(e.done)," / ").concat(n(e.total),"â¦"):"FAILED"===e.status?"Recompute failed: ".concat(null!=(t=e.error)?t:"unknown error"):"Updated ".concat(n(e.total)," responses ").concat(function(e){let t=Math.max(0,(Date.now()-new Date(e).getTime())/1e3);return t<60?"just now":t<3600?"".concat(Math.floor(t/60)," min ago"):t<86400?"".concat(Math.floor(t/3600)," h ago"):"".concat(Math.floor(t/86400)," d ago")}(null!=(r=e.finishedAt)?r:e.createdAt))}(L)}),(0,n.jsx)("p",{className:"text-[11px] text-muted-foreground",children:"Recompute updates stored values only. Skip logic, quotas and respondent status are not re-run."})]})]})}},65968:(e,t,r)=>{r.d(t,{l:()=>a});var n=r(54568),s=r(7620),l=r(25703);let a=s.forwardRef((e,t)=>
1{let{className:r,children:s,...a}=e;return(0,n.jsx)("select",{className:(0,l.cn)("flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",r),ref:t,...a,children:s})});a.displayName="Select"}}]);
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.