@sonata-innovations/fiber-fbre 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -6
- package/dist/fiber-fbre.cjs +1 -1
- package/dist/fiber-fbre.css +1 -1
- package/dist/fiber-fbre.js +1387 -1238
- package/dist/index.d.ts +4 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -92,6 +92,7 @@ Render a Flow object you already have in memory.
|
|
|
92
92
|
|------|------|----------|-------------|
|
|
93
93
|
| `flow` | `Flow` | Yes | Flow JSON object |
|
|
94
94
|
| `data` | `FlowData` | No | Pre-populated form data |
|
|
95
|
+
| `mode` | `FlowModeType` | No | Override form mode (`"standard"` \| `"conversational"`) |
|
|
95
96
|
| `screenIndex` | `number` | No | Initial screen index (default `0`) |
|
|
96
97
|
| `theme` | `ThemeConfig` | No | Override theme settings (merged over `flow.config.theme`) |
|
|
97
98
|
| `navigation` | `NavigationConfig` | No | Override navigation settings (merged over `flow.config.navigation`) |
|
|
@@ -112,6 +113,7 @@ Fetch a published flow from a Fiber API by ID.
|
|
|
112
113
|
| `apiEndpoint` | `string` | Yes | Base URL of the Fiber API |
|
|
113
114
|
| `apiKey` | `string` | No | API key for authentication |
|
|
114
115
|
| `data` | `FlowData` | No | Pre-populated form data |
|
|
116
|
+
| `mode` | `FlowModeType` | No | Override form mode (`"standard"` \| `"conversational"`) |
|
|
115
117
|
| `screenIndex` | `number` | No | Initial screen index (default `0`) |
|
|
116
118
|
| `theme` | `ThemeConfig` | No | Override theme settings |
|
|
117
119
|
| `navigation` | `NavigationConfig` | No | Override navigation settings |
|
|
@@ -131,6 +133,7 @@ Session-based rendering. The server evaluates conditions and validation; the cli
|
|
|
131
133
|
| `sessionEndpoint` | `string` | Yes | Session API base URL |
|
|
132
134
|
| `flowId` | `string` | Yes | ID of the flow to start a session for |
|
|
133
135
|
| `apiKey` | `string` | No | API key for authentication |
|
|
136
|
+
| `mode` | `FlowModeType` | No | Override form mode (`"standard"` \| `"conversational"`) |
|
|
134
137
|
| `theme` | `ThemeConfig` | No | Override theme settings |
|
|
135
138
|
| `context` | `Record<string, string \| boolean \| number>` | No | External context values for condition evaluation and calculations |
|
|
136
139
|
| `onFlowComplete` | `(data: FlowData) => void` | Yes | Called when the user completes the flow |
|
|
@@ -223,7 +226,7 @@ import type {
|
|
|
223
226
|
Flow
|
|
224
227
|
├── uuid: string
|
|
225
228
|
├── metadata: { name?, description?, ...}
|
|
226
|
-
├── config?: { theme?: { color?, darkMode?, style? }, navigation?: { transition?, allowInvalidTransition? }, controls?: { show?, layout?, showStepper? }, summary? }
|
|
229
|
+
├── config?: { mode?, theme?: { color?, darkMode?, style? }, navigation?: { transition?, allowInvalidTransition? }, controls?: { show?, layout?, showStepper? }, summary? }
|
|
227
230
|
└── screens: FlowScreen[]
|
|
228
231
|
├── uuid: string
|
|
229
232
|
├── label?: string
|
|
@@ -369,6 +372,38 @@ Text components and `detail` fields support inline markup:
|
|
|
369
372
|
- `[i]italic[/i]` → *italic*
|
|
370
373
|
- `[l href="url"]link[/l]` → [link](url)
|
|
371
374
|
|
|
375
|
+
## Conversational Mode
|
|
376
|
+
|
|
377
|
+
Set `mode: "conversational"` to transform FBRE into a one-question-per-screen experience:
|
|
378
|
+
|
|
379
|
+
```tsx
|
|
380
|
+
// Via JSX prop
|
|
381
|
+
<FBRE flow={myFlow} mode="conversational" onFlowComplete={handleComplete} />
|
|
382
|
+
|
|
383
|
+
// Via flow config
|
|
384
|
+
const flow = {
|
|
385
|
+
...myFlow,
|
|
386
|
+
config: { ...myFlow.config, mode: "conversational" }
|
|
387
|
+
};
|
|
388
|
+
<FBRE flow={flow} onFlowComplete={handleComplete} />
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
**What changes:**
|
|
392
|
+
|
|
393
|
+
- Content is vertically and horizontally centered
|
|
394
|
+
- Single-select components (`radio`, `yesNo`, `cardSelect`, `dropDown`) auto-advance ~500ms after selection
|
|
395
|
+
- Pressing Enter on `inputText` / `inputNumber` advances to the next screen
|
|
396
|
+
- Components animate in with fade + scale + stagger on screen transitions (respects `prefers-reduced-motion`)
|
|
397
|
+
- Option items, Yes/No buttons, card-select cards, and input fields are enlarged for easier tapping
|
|
398
|
+
- Each of the 6 existing styles has a tailored conversational presentation (e.g. clean = bordered cards, soft-outlined = pill-shaped options, defined-outlined = inverted selection)
|
|
399
|
+
|
|
400
|
+
**What doesn't change:**
|
|
401
|
+
|
|
402
|
+
- FlowData output is identical
|
|
403
|
+
- Conditions, validation, calculations all work the same
|
|
404
|
+
- Navigation buttons still work alongside auto-advance
|
|
405
|
+
- Works with all 6 transition types and dark mode
|
|
406
|
+
|
|
372
407
|
## Theming
|
|
373
408
|
|
|
374
409
|
FBRE uses CSS custom properties. Override on the container:
|
|
@@ -501,8 +536,4 @@ Each `<FBRE />` creates its own isolated Zustand store. Multiple instances can r
|
|
|
501
536
|
```tsx
|
|
502
537
|
<FBRE flow={flowA} onFlowComplete={handleA} />
|
|
503
538
|
<FBRE flow={flowB} onFlowComplete={handleB} />
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
## AI-Assisted Development
|
|
507
|
-
|
|
508
|
-
For AI integration reference, see [AI Integration Guide](../documentation/ai-integration-guide.md) and [llms.txt](./llms.txt).
|
|
539
|
+
```
|
package/dist/fiber-fbre.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("react/jsx-runtime"),w=require("react"),G=require("zustand"),Je=require("@sonata-innovations/fiber-types"),U=require("@sonata-innovations/fiber-shared"),Ye=new Map,We=e=>Ye.get(e),Xe=new Set(["header","text","divider","callout","table"]),ue=e=>Xe.has(e),Ke=(e,s)=>{var o,t,n,r;if(s.includes(":")){const i=s.split(":"),a=e.components[i[0]];return(n=(t=(o=a==null?void 0:a.addedComponents)==null?void 0:o[parseInt(i[1])])==null?void 0:t[parseInt(i[2])])==null?void 0:n.value}return(r=e.components[s])==null?void 0:r.value},T=(e,s)=>{var t;const o=(t=e.properties)==null?void 0:t.validation;return!o||!o.rules||o.rules.length===0?[]:U.validateValue(e.value,o,s?n=>Ke(s,n):void 0)},Qe=e=>{var o;const s=(o=e.properties)==null?void 0:o.validation;return!s||!s.rules?!1:s.rules.some(t=>t.type==="required")},z=e=>{var o,t;const s={...e};return Array.isArray((o=s.properties)==null?void 0:o.options)&&(s.properties={...s.properties,options:Je.normalizeOptions(s.properties.options)}),e.type==="computed"?(s.value=null,s.valid=!0):!ue(e.type)&&e.type!=="group"&&e.type!=="repeater"&&(s.value=null,s.valid=!Qe(e)),(e.type==="checkbox"||e.type==="dropDownMulti")&&(s.value=[]),e.type==="radio"&&"options"in e.properties&&e.properties.options.length>0&&(s.value=e.properties.options[0].value),e.type==="toggleSwitch"&&(s.value=!1),e.type==="colorPicker"&&((t=e.properties)!=null&&t.defaultValue)&&(s.value=e.properties.defaultValue),e.type==="slider"&&(s.value="initValue"in e.properties?e.properties.initValue:"min"in e.properties?e.properties.min:0),s},oe=(e,s,o)=>{var t;for(const n of o.rules)if(n.type==="matchesField"&&((t=n.params)!=null&&t.field)){const r=n.params.field;e[r]||(e[r]=[]),e[r].push({target:s,config:o})}},X=(e,s)=>{const o=e.screens[s];return o?!o.components.some(n=>{var a;const r=e.components[n.uuid];if(!r)return!1;if(r.type==="group"||r.type==="repeater")return(a=r.addedComponents)==null?void 0:a.some(c=>c.some(u=>{const l=u.conditions&&"when"in u.conditions?u.conditions:void 0;return l&&U.isHiddenByCondition(l,e.conditionResults[u.uuid])?!1:!u.valid}));const i=r.conditions&&"when"in r.conditions?r.conditions:void 0;return U.isHiddenByCondition(i,e.conditionResults[r.uuid])?!1:r&&"valid"in r&&!r.valid}):!0};function Ze(e){const s={};for(const o of e){const t=U.extractFormulaReferences(o.formula);for(const n of t)s[n]||(s[n]=[]),s[n].includes(o.uuid)||s[n].push(o.uuid)}return s}function Ue(e){const s=new Map(e.map(i=>[i.uuid,i])),o=new Set(e.map(i=>i.uuid)),t=new Set,n=[];function r(i,a){if(t.has(i)||a.has(i))return;a.add(i);const c=s.get(i);if(!c)return;const u=U.extractFormulaReferences(c.formula);for(const l of u)o.has(l)&&r(l,a);a.delete(i),t.add(i),n.push(c)}for(const i of e)r(i.uuid,new Set);return n}function Ae(e,s){return{resolveValue:o=>{if(o in s)return s[o];const t=J(e,o);if(!t)return null;const n=t.value;if(n==null||n==="")return null;const r=Number(n);return isNaN(r)?null:r},resolveOptionMetadata:(o,t)=>{var l;const n=J(e,o);if(!n)return null;const r=n.value;if(!r)return null;const i=(l=n.properties)==null?void 0:l.options;if(!Array.isArray(i))return null;const a=i.find(d=>d.value===r||d.label===r);if(!(a!=null&&a.metadata))return null;const c=a.metadata[t];if(c==null)return null;const u=Number(c);return isNaN(u)?null:u},resolveRepeaterValues:o=>{for(const t of Object.keys(e.components)){const n=e.components[t];if(n.type!=="repeater"||!n.addedComponents||!n.components)continue;const r=n.components.findIndex(a=>a.uuid===o);if(r===-1)continue;const i=[];for(const a of n.addedComponents){const c=a[r];if(!c)continue;const u=c.value;if(u==null||u==="")continue;const l=Number(u);isNaN(l)||i.push(l)}return i.length>0?i:null}return null}}}function ye(e){const s=e.calculations??[];if(s.length===0)return{};const o=Ue(s),t={},n=Ae(e,t);for(const r of o)t[r.uuid]=U.evaluateFormula(r.formula,n);return t}function me(e,s){const o=e.calculationsByDependency,t=e.calculations??[];if(t.length===0||!o)return null;const n=new Set,r=[s];for(;r.length>0;){const l=r.shift(),d=o[l];if(d)for(const f of d)n.has(f)||(n.add(f),r.push(f))}if(n.size===0)return null;const i=Ue(t),a={...e.calculationResults},c=Ae(e,a);let u=!1;for(const l of i)if(n.has(l.uuid)){const d=U.evaluateFormula(l.formula,c);a[l.uuid]!==d&&(a[l.uuid]=d,u=!0)}return u?a:null}function en(e){const s={},o=t=>{var r;if(t.type!=="computed"||!((r=t.properties)!=null&&r.formula))return;const n=U.extractFormulaReferences(t.properties.formula);for(const i of n)s[i]||(s[i]=[]),s[i].includes(t.uuid)||s[i].push(t.uuid)};for(const t of Object.values(e))if(o(t),(t.type==="repeater"||t.type==="group")&&t.components)for(const n of t.components)o(n);return s}function Le(e,s,o,t){var c,u;const n=e.components[o],r=((c=n==null?void 0:n.components)==null?void 0:c.map(l=>l.uuid))??[],i=(u=n==null?void 0:n.addedComponents)==null?void 0:u[t],a=new Map;if(i)for(let l=0;l<i.length;l++){const d=r[l];d&&a.set(d,i[l])}return{resolveValue:l=>{if(l in s)return s[l];const d=a.get(l);if(d){const g=d.value;if(g==null||g==="")return null;const h=Number(g);return isNaN(h)?null:h}const f=J(e,l);if(!f)return null;const m=f.value;if(m==null||m==="")return null;const y=Number(m);return isNaN(y)?null:y},resolveOptionMetadata:(l,d)=>{var x;const m=a.get(l)??J(e,l);if(!m)return null;const y=m.value;if(!y)return null;const g=(x=m.properties)==null?void 0:x.options;if(!Array.isArray(g))return null;const h=g.find(b=>b.value===y||b.label===y);if(!(h!=null&&h.metadata))return null;const v=h.metadata[d];if(v==null)return null;const C=Number(v);return isNaN(C)?null:C},resolveRepeaterValues:l=>{for(const d of Object.keys(e.components)){const f=e.components[d];if(f.type!=="repeater"||!f.addedComponents||!f.components)continue;const m=f.components.findIndex(g=>g.uuid===l);if(m===-1)continue;const y=[];for(const g of f.addedComponents){const h=g[m];if(!h)continue;const v=h.value;if(v==null||v==="")continue;const C=Number(v);isNaN(C)||y.push(C)}return y.length>0?y:null}return null}}}function de(e,s){return{resolveValue:o=>{if(o in s)return s[o];const t=J(e,o);if(!t)return null;const n=t.value;if(n==null||n==="")return null;const r=Number(n);return isNaN(r)?null:r},resolveOptionMetadata:(o,t)=>{var l;const n=J(e,o);if(!n)return null;const r=n.value;if(!r)return null;const i=(l=n.properties)==null?void 0:l.options;if(!Array.isArray(i))return null;const a=i.find(d=>d.value===r||d.label===r);if(!(a!=null&&a.metadata))return null;const c=a.metadata[t];if(c==null)return null;const u=Number(c);return isNaN(u)?null:u},resolveRepeaterValues:o=>{for(const t of Object.keys(e.components)){const n=e.components[t];if(n.type!=="repeater"||!n.addedComponents||!n.components)continue;const r=n.components.findIndex(a=>a.uuid===o);if(r===-1)continue;const i=[];for(const a of n.addedComponents){const c=a[r];if(!c)continue;const u=c.value;if(u==null||u==="")continue;const l=Number(u);isNaN(l)||i.push(l)}return i.length>0?i:null}return null}}}function ge(e){var o,t;const s=e.calculationResults??{};for(const n of Object.values(e.components))if(n.type==="computed"&&((o=n.properties)!=null&&o.formula)){const r=de(e,s),i=U.evaluateFormula(n.properties.formula,r);n.value=i}for(const n of Object.values(e.components))if((n.type==="repeater"||n.type==="group")&&n.addedComponents&&n.components)for(let r=0;r<n.addedComponents.length;r++){const i=n.addedComponents[r];for(let a=0;a<i.length;a++){const c=i[a],u=n.components[a];if((u==null?void 0:u.type)==="computed"&&((t=u.properties)!=null&&t.formula)){const l=n.type==="repeater"?Le(e,s,n.uuid,r):de(e,s),d=U.evaluateFormula(u.properties.formula,l);c.value=d}}}}function nn(e,s){var i,a;const o=e.computedByDependency;if(!o)return!1;const t=o[s];if(!t||t.length===0)return!1;const n=e.calculationResults??{};let r=!1;for(const c of t){const u=e.components[c];if(u&&u.type==="computed"&&((i=u.properties)!=null&&i.formula)){const l=de(e,n),d=U.evaluateFormula(u.properties.formula,l);u.value!==d&&(e.components[c]={...u,value:d},r=!0);continue}for(const l of Object.values(e.components))if((l.type==="repeater"||l.type==="group")&&l.components&&l.addedComponents){const d=l.components.findIndex(m=>m.uuid===c);if(d===-1)continue;const f=l.components[d];if((f==null?void 0:f.type)!=="computed"||!((a=f.properties)!=null&&a.formula))continue;for(let m=0;m<l.addedComponents.length;m++){const y=l.addedComponents[m][d];if(!y)continue;const g=l.type==="repeater"?Le(e,n,l.uuid,m):de(e,n),h=U.evaluateFormula(f.properties.formula,g);y.value!==h&&(l.addedComponents[m][d]={...y,value:h},r=!0)}}}return r}const Pe=(e,s)=>{var o,t;if(s.includes(":")){const n=s.split(":"),r=e[n[0]];return(t=(o=r==null?void 0:r.addedComponents)==null?void 0:o[parseInt(n[1])])==null?void 0:t[parseInt(n[2])]}return e[s]},ae=(e,s)=>{for(const o of e.screenOrder)if(e.screens[o].components.some(n=>n.uuid===s))return o;return null},tn=(e,s)=>{if(!s.includes(":"))return null;const o=s.split(":");if(o.length!==3)return null;const t=o[0],n=parseInt(o[2]),r=e.components[t];if(!r||r.type!=="repeater"||!r.components)return null;const i=r.components[n];return(i==null?void 0:i.uuid)??null},ke=(e,s,o,t,n)=>{const r=s(),i=r.groupComponentMap[o]??o,a=rn(e,s,i),c={components:r.components,groupComponentMap:r.groupComponentMap},u=T(t,c);t.valid=u.length===0;const l={...r.validationErrors,[o]:u},d=r.validationsByDependency[o];if(d)for(const x of d){const b=J(r,x.target);if(b){const R=T(b,c);b.valid=R.length===0,l[x.target]=R}}const f=ae(r,n),m={components:{...r.components},validationErrors:l};if(a&&(m.conditionResults=a),f){const x={...r,...m};m.screenValidity={...r.screenValidity,[f]:X(x,f)}}const y={...r,...m},g=o.includes(":")?tn(y,o):null,h=[],v=x=>{if(nn(y,x)){const b=y.computedByDependency[x]??[];for(const R of b)h.includes(R)||(h.push(R),v(R))}};v(i),g&&g!==i&&v(g);let C=me(y,i);if(g&&g!==i){const x=C?{...y,calculationResults:C}:y,b=me(x,g);b&&(C=b)}for(const x of h){const b=C?{...y,calculationResults:C}:y,R=me(b,x);R&&(C=R)}C&&(m.calculationResults=C),e(m)},on=(e,s)=>({updateComponentValue:(o,t)=>{var i,a;const n=s();if(o.includes(":")){const c=o.split(":"),u=c[0],l=parseInt(c[1]),d=parseInt(c[2]),f=n.components[u];if(!((a=(i=f==null?void 0:f.addedComponents)==null?void 0:i[l])!=null&&a[d]))return;const m=f.addedComponents[l][d].value!==t;if(f.addedComponents[l][d]={...f.addedComponents[l][d],value:t},m){ke(e,s,o,f.addedComponents[l][d],u);return}e({components:{...n.components}});return}const r=n.components[o];if(r){if(r.type==="group"||r.type==="repeater"){e({components:{...n.components}});return}t!==r.value&&(r.value=t,ke(e,s,o,r,o))}},addGroupIteration:o=>{const t=s(),n=t.components[o];if(!n||n.type!=="group"||!n.components)return;n.addedComponents||(n.addedComponents=[]);const r={...t.conditionsByDependency},i=n.components.map((u,l)=>{const d=z(u);if(d.uuid=[o,n.addedComponents.length,l].join(":"),t.groupComponentMap[d.uuid]=u.uuid,d.conditions&&Object.keys(d.conditions).length>0){const m="when"in d.conditions?d.conditions:null;m&&(d.conditions=m,W(r,d.uuid,m,"component"))}const f=T(d);return d.valid=f.length===0,d});n.addedComponents.push(i);const a=ae(t,o),c={...t.screenValidity};return a&&(c[a]=X({...t},a)),e({components:{...t.components},groupComponentMap:{...t.groupComponentMap},conditionsByDependency:r,screenValidity:c}),n.addedComponents},addRepeaterIteration:o=>{const t=s(),n=t.components[o];if(!n||n.type!=="repeater"||!n.components)return;n.addedComponents||(n.addedComponents=[]);const r={...t.conditionsByDependency},i=n.components.map((d,f)=>{const m=z(d);if(m.uuid=[o,n.addedComponents.length,f].join(":"),m.conditions&&Object.keys(m.conditions).length>0){const g="when"in m.conditions?m.conditions:null;g&&(m.conditions=g,W(r,m.uuid,g,"component"))}const y=T(m);return m.valid=y.length===0,m});n.addedComponents.push(i);const a=ae(t,o),c={...t.screenValidity};a&&(c[a]=X({...t},a));const u={...t,components:{...t.components}};ge(u);const l=ye(u);return e({components:{...t.components},conditionsByDependency:r,screenValidity:c,calculationResults:l}),n.addedComponents},removeRepeaterIteration:(o,t)=>{var g;const n=s(),r=n.components[o];if(!r||r.type!=="repeater"||!r.addedComponents)return;const i=((g=r.properties)==null?void 0:g.minIterations)??1;if(r.addedComponents.length<=i)return;const a=r.addedComponents[t],c=new Set(a.map(h=>h.uuid));r.addedComponents.splice(t,1);const u={...n.conditionsByDependency},l={...n.validationErrors};for(const h of c)delete l[h],delete u[h];for(const h of Object.keys(u)){const v=u[h],C=v.filter(x=>!c.has(x.target));C.length===0?delete u[h]:C.length!==v.length&&(u[h]=C)}for(let h=t;h<r.addedComponents.length;h++)r.addedComponents[h].forEach((v,C)=>{const x=v.uuid,b=[o,h,C].join(":");if(x!==b){v.uuid=b,x in l&&(l[b]=l[x],delete l[x]),x in u&&(u[b]=u[x],delete u[x]);for(const R of Object.values(u))for(const _ of R)_.target===x&&(_.target=b)}});const d=ae(n,o),f={...n.screenValidity};d&&(f[d]=X({...n},d));const m={...n,components:{...n.components}};ge(m);const y=ye(m);e({components:{...n.components},conditionsByDependency:u,validationErrors:l,screenValidity:f,calculationResults:y})}}),J=(e,s)=>Pe(e.components,s),W=(e,s,o,t)=>{const n=new Set;for(const i of o.when.rules)n.add(i.source);const r={target:s,type:t,config:o};for(const i of n)e[i]||(e[i]=[]),e[i].push(r)},sn=(e,s)=>{var o,t;return s.includes(":")?(o=J(e,s))==null?void 0:o.value:(t=e.components[s])==null?void 0:t.value},Q=(e,s)=>U.evaluateConditionConfig(s,(o,t)=>(t==null?void 0:t.sourceType)==="context"?e.externalContext[t.source]:sn(e,o)),rn=(e,s,o)=>{const t=s(),n=t.conditionsByDependency[o];if(!n||n.length===0)return null;let r=!1;const i={...t.conditionResults};for(const a of n){const c=Q(t,a.config);i[a.target]!==c&&(i[a.target]=c,r=!0)}return r?i:null},cn=e=>{const s={},o=new Set;for(const t of Object.values(e.conditionsByDependency))for(const n of t)o.has(n.target)||(o.add(n.target),s[n.target]=Q(e,n.config));return s},ve=e=>{if(!e||ue(e.type))return null;const s="label"in e.properties?e.properties.label:"",o={uuid:e.uuid,type:e.type,value:e.value};return s&&(o.label=s),o},Ve=e=>{if(e.conditions&&"when"in e.conditions)return e.conditions},an=e=>{const s={uuid:e.flowUUID,metadata:e.metadata,screens:[]},o=t=>{const n=e.screens[t];if(!n)return!1;const r=n.conditions&&"when"in n.conditions?n.conditions:void 0;return U.isHiddenByCondition(r,e.conditionResults[t])};return s.screens=e.screenOrder.filter(t=>!o(t)).map(t=>{const n=e.screens[t],r={uuid:t,components:n.components.map(i=>{const a=e.components[i.uuid];if(!a)return null;const c=Ve(a);if(U.isHiddenByCondition(c,e.conditionResults[i.uuid]))return null;if((a.type==="group"||a.type==="repeater")&&a.addedComponents){const u=ve(a);return u?{...u,components:a.addedComponents.map(l=>l.filter(d=>{const f=Ve(d);return!U.isHiddenByCondition(f,e.conditionResults[d.uuid])}).map(d=>ve(d)).filter(d=>d!==null))}:null}return ve(a)}).filter(i=>i!==null)};return n.label&&(r.label=n.label),r}),s},pe=w.createContext(null);function P(e){const s=w.useContext(pe);if(!s)throw new Error("useFBREStore must be used within an FBRE provider");return G.useStore(s,e)}const be=()=>{const e=w.useContext(pe);if(!e)throw new Error("useFBREStoreApi must be used within an FBRE provider");return e},re=e=>!e||Object.keys(e).length===0?null:"when"in e?e:null,ln=[],un=()=>G.createStore((e,s)=>({flowUUID:"",metadata:{},config:{},externalContext:{},screenOrder:[],screens:{},screenValidity:{},components:{},groupComponentMap:{},templateComponentMap:{},conditionsByDependency:{},conditionResults:{},validationErrors:{},validationsByDependency:{},calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},loadFlow:(o,t,n)=>{var _;const r={},i=[],a={},c={},u={},l={},d={},f={},m={};o.screens.forEach(E=>{const O=re(E.conditions),F=O?{...E,conditions:O}:{...E};r[E.uuid]=F,i.push(E.uuid),O&&W(l,E.uuid,O,"screen"),E.components.forEach(S=>{var k;const D=re(S.conditions),B=z(S);if(D&&(B.conditions=D),a[S.uuid]=B,D&&W(l,S.uuid,D,"component"),(k=B.properties)!=null&&k.validation&&oe(d,S.uuid,B.properties.validation),(S.type==="group"||S.type==="repeater")&&S.components){const $=a[S.uuid];$.addedComponents||($.addedComponents=[]);const q=S.properties??{},j=S.type==="repeater"?q.initialData:void 0,N=S.type==="repeater"?q.minIterations:void 0,M=j?j.length:S.type==="repeater"&&N&&N>1?N:1;for(let V=0;V<M;V++){const K=S.components.map((Y,fe)=>{var te;const A=z(Y);A.uuid=[S.uuid,$.addedComponents.length,fe].join(":"),j&&j[V]&&Y.uuid in j[V]&&(A.value=j[V][Y.uuid]),S.type==="group"&&(c[A.uuid]=Y.uuid),V===0&&(u[Y.uuid]=A.uuid);const ee=re(A.conditions);ee&&(A.conditions=ee,W(l,A.uuid,ee,"component")),(te=A.properties)!=null&&te.validation&&oe(d,A.uuid,A.properties.validation);const ne=T(A);return A.valid=ne.length===0,ne.length>0&&(f[A.uuid]=ne),A});$.addedComponents.push(K)}}})}),t&&t.screens.forEach(E=>{const O=i.indexOf(E.uuid);O<0||E.components.forEach(F=>{const S=a[F.uuid];if(S)if((S.type==="group"||S.type==="repeater")&&F.components&&S.addedComponents){if(F.components.length>1){const D=S.addedComponents.length,B=F.components.length-D,H=o.screens[O].components.find(k=>k.uuid===S.uuid);for(let k=0;k<B;k++)if(H!=null&&H.components){const $=H.components.map((q,j)=>{var K;const N=z(q);N.uuid=[S.uuid,S.addedComponents.length,j].join(":"),S.type==="group"&&(c[N.uuid]=q.uuid),u[q.uuid]||(u[q.uuid]=N.uuid);const M=re(N.conditions);M&&(N.conditions=M,W(l,N.uuid,M,"component")),(K=N.properties)!=null&&K.validation&&oe(d,N.uuid,N.properties.validation);const V=T(N);return N.valid=V.length===0,V.length>0&&(f[N.uuid]=V),N});S.addedComponents.push($)}}F.components.forEach(D=>{D.forEach(B=>{for(const H of S.addedComponents){const k=H.find($=>$.uuid===B.uuid);if(k){k.value=B.value;const $=T(k);k.valid=$.length===0,f[k.uuid]=$;break}}})})}else{S.value=F.value;const D=T(S);S.valid=D.length===0,f[S.uuid]=D}})});for(const E of Object.keys(a)){const O=a[E];if(!(O.type==="group"||O.type==="repeater"||!((_=O.properties)!=null&&_.validation))&&!(E in f)){const F=T(O);O.valid=F.length===0,F.length>0&&(f[E]=F)}}const y={...s(),externalContext:n??{},screens:r,screenOrder:i,components:a,groupComponentMap:c,templateComponentMap:u,conditionsByDependency:l,conditionResults:{},validationErrors:f,validationsByDependency:d},g=cn(y);y.conditionResults=g,i.forEach(E=>{m[E]=X(y,E)});const h=en(a),v={...y,computedByDependency:h,calculationResults:{},screenValidity:m};ge(v);const C=o.calculations??[],x=Ze(C),b={...v,calculations:C},R=ye(b);e({flowUUID:o.uuid,metadata:o.metadata,config:o.config??{},externalContext:n??{},screens:r,screenOrder:i,components:a,groupComponentMap:c,templateComponentMap:u,conditionsByDependency:l,conditionResults:g,screenValidity:m,validationErrors:f,validationsByDependency:d,calculations:C,calculationResults:R,calculationsByDependency:x,computedByDependency:h})},updateContext:o=>{const t=s(),n=t.externalContext,r=[];for(const u of Object.keys(o))n[u]!==o[u]&&r.push(u);for(const u of Object.keys(n))!(u in o)&&!r.includes(u)&&r.push(u);if(r.length===0)return;const i={...t.conditionResults};let a=!1;const c=new Set;for(const u of r){const l=t.conditionsByDependency[u];if(!(!l||l.length===0))for(const d of l){if(c.has(d.target))continue;c.add(d.target);const f={...t,externalContext:o},m=Q(f,d.config);i[d.target]!==m&&(i[d.target]=m,a=!0)}}if(a){const u={...t,conditionResults:i},l={};for(const d of t.screenOrder)l[d]=X(u,d);e({externalContext:o,conditionResults:i,screenValidity:l})}else e({externalContext:o})},...on(e,s),getFlowData:()=>an(s()),getScreenByIndex:o=>{const t=s();return o<0||o>=t.screenOrder.length?null:t.screens[t.screenOrder[o]]},getScreenValidity:o=>{const t=s();return o<0||o>=t.screenOrder.length?!0:t.screenValidity[t.screenOrder[o]]??!0},getValidationErrors:o=>s().validationErrors[o]??ln,evaluateFlowValidation:()=>{const o=s(),t={};let n=!0;return o.screenOrder.forEach(r=>{const i=X(o,r);t[r]=i,i||(n=!1)}),e({screenValidity:t}),n},getMaxScreenCount:()=>s().screenOrder.length-1,getCalculationResult:o=>s().calculationResults[o]??null})),dn=({uuid:e})=>{var a;const s=P(c=>c.components[e]),o=P(c=>c.conditionResults[e]);if(!s)return null;const t=s.conditions&&"when"in s.conditions?s.conditions:void 0;if(U.isHiddenByCondition(t,o))return null;const n=We(s.type);if(!n)return console.warn(`FBRE: No renderer for component type "${s.type}"`),null;const r=(a=s.properties)==null?void 0:a.width,i=r&&r!=="full"?`fbre-component fbre-width-${r}`:"fbre-component";return p.jsx("div",{className:i,"data-uuid":e,children:p.jsx(n,{uuid:e})})},Ce=({screenIndex:e})=>{const s=P(o=>{const t=o.screenOrder[e];return t?o.screens[t]:null});return s?p.jsx("div",{className:"fbre-screen",children:s.components.map(o=>p.jsx(dn,{uuid:o.uuid},o.uuid))}):null},Oe=({size:e=14})=>p.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",children:p.jsx("polyline",{points:"4,12 9.5,17.5 20,7"})}),pn=({size:e=12})=>p.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M7 10l5 5 5-5z"})}),fn=({size:e=16})=>p.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"})}),mn=({size:e=16})=>p.jsx("svg",{width:e,height:e,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"})}),Me=({position:e,screenIndex:s,maxScreenIndex:o,allowInvalidTransition:t,screenValidity:n,summaryEnabled:r,summaryActive:i,atMaxScreen:a,nextButtonLabel:c,backButtonLabel:u,completionLoading:l,nextScreen:d,prevScreen:f,onFlowComplete:m})=>{const y=l??!1;if(e==="back"){const x=s===0&&!i||y;return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--secondary",disabled:x,onClick:f,children:[p.jsx(fn,{}),u||"Back"]})}const g=r?a&&i:a,h=r?a?i?"Done":"Review":"Next":a?"Done":"Next";if(o===0){const x=r?i?"Complete":"Review":"Complete",b=y||!n;return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--primary",disabled:b,onClick:()=>{r&&!i?d():m()},children:[c||x,y?p.jsx("span",{className:"fbre-btn__spinner"}):p.jsx(Oe,{size:16})]})}const v=y||!t&&!n||h==="Done"&&!n,C=()=>{g?m():d()};return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--primary",disabled:v,onClick:C,children:[c||h,y?p.jsx("span",{className:"fbre-btn__spinner"}):g?p.jsx(Oe,{size:16}):p.jsx(mn,{})]})},vn=({current:e,total:s})=>{if(s<=1)return null;const o=(e+1)/s*100;return s<=6?p.jsx("div",{className:"fbre-stepper fbre-stepper--dots",children:Array.from({length:s},(t,n)=>p.jsx("div",{className:`fbre-stepper__dot${n===e?" active":n<e?" completed":""}`},n))}):p.jsx("div",{className:"fbre-stepper fbre-stepper--bar",children:p.jsx("div",{className:"fbre-stepper__fill",style:{width:`${o}%`}})})},hn=({current:e,total:s})=>s<=1?null:p.jsxs("div",{className:"fbre-stepper-text",children:["Step ",e+1," of ",s]}),yn=e=>{const s=e.controlsLayout==="stacked",o=e.showStepper!==!1&&(e.stepperStyle??"default")==="default";return p.jsxs("div",{className:s?"fbre-controls fbre-controls--stacked":"fbre-controls",children:[e.screenIndex>0||e.summaryActive?p.jsx(Me,{position:"back",...e}):p.jsx("div",{}),o&&e.maxScreenIndex>0&&p.jsx(vn,{current:e.screenIndex,total:e.maxScreenIndex+1}),p.jsx(Me,{position:"next",...e})]})},$e=({component:e})=>{const{uuid:s,label:o,type:t,value:n,components:r}=e,i=()=>{if(n!=null)switch(t){case"fileUpload":return n.name;case"inputText":case"inputTextArea":case"inputNumber":case"dropDown":case"radio":case"rating":case"slider":return n.toString();case"computed":return typeof n=="number"?n.toString():"—";case"toggleSwitch":return n?"Yes":"No";case"checkbox":case"dropDownMulti":return n.length===0?"No options selected":n.join(", ")}return"No response given"};if(t==="signature"&&n){const u=typeof n=="string"&&n.startsWith("data:");return p.jsxs("div",{className:"fbre-summary__item",children:[p.jsx("div",{className:"fbre-summary__item-label",children:o}),p.jsx("div",{className:"fbre-summary__item-value",children:u?p.jsx("img",{src:n,alt:"Signature",style:{maxWidth:"100%",maxHeight:80}}):p.jsx("span",{style:{fontFamily:'"Brush Script MT", "Segoe Script", "Dancing Script", cursive',fontSize:24},children:n})})]})}const a=()=>r?r.map((u,l)=>p.jsx("div",{children:u.map(d=>p.jsx($e,{component:d},d.uuid))},`${s}-${l}`)):null,c=n==null||typeof n=="string"&&n.length===0||Array.isArray(n)&&n.length===0;return p.jsxs("div",{className:`fbre-summary__item${t==="group"||t==="repeater"?" fbre-summary__item--group":""}`,children:[p.jsx("div",{className:"fbre-summary__item-label",children:o}),t!=="group"&&t!=="repeater"&&p.jsx("div",{className:`fbre-summary__item-value${c?" fbre-summary__item-value--empty":""}`,children:i()}),(t==="group"||t==="repeater")&&a()]})},gn=/\$\{([^}]+)\}/g,Cn=(e,s)=>e.replace(gn,(o,t)=>{const n=s.resolveCalculation(t);if(n!==null)return n;const r=s.resolveComponentValue(t);return r!==null?r:""});function bn(){const e=P(n=>n.components),s=P(n=>n.templateComponentMap),o=P(n=>n.calculations),t=P(n=>n.calculationResults);return w.useMemo(()=>{const n=new Map(o.map(i=>[i.uuid,i])),r=i=>{var d,f;const a=e[i];if(a)return a;const c=s[i];if(!c)return;const u=c.split(":"),l=e[u[0]];return(f=(d=l==null?void 0:l.addedComponents)==null?void 0:d[parseInt(u[1])])==null?void 0:f[parseInt(u[2])]};return{resolveComponentValue:i=>{const a=r(i);if(!a)return null;const c=a.value;return c==null||c===""?null:Array.isArray(c)?c.join(", "):String(c)},resolveCalculation:i=>{const a=n.get(i);if(!a)return null;const c=t[i]??null;return U.formatCalculationResult(c,a.format,a.decimalPlaces,a.currencySymbol)}}},[e,s,o,t])}const xn=({screen:e,showToggle:s})=>{const o=bn(),[t,n]=w.useState(!0);return p.jsxs("div",{children:[p.jsxs("div",{className:"fbre-summary__screen-header",onClick:()=>s&&n(!t),children:[e.label&&p.jsx("span",{className:"fbre-summary__screen-label",children:Cn(e.label,o)}),s&&p.jsx("span",{className:`fbre-summary__screen-toggle${t?"":" collapsed"}`,children:p.jsx(pn,{size:20})})]}),p.jsx("div",{className:`fbre-summary__screen-body${t?"":" collapsed"}`,children:e.components.map(r=>p.jsx($e,{component:r},r.uuid))})]})},_e=()=>{const e=be(),s=P(n=>n.components),o=w.useMemo(()=>e.getState().getFlowData(),[e,s]),t=o.screens.length;return p.jsxs("div",{className:"fbre-summary",children:[p.jsx("h2",{className:"fbre-summary__title",children:"Review"}),p.jsx("div",{className:"fbre-summary__body",children:o.screens.map(n=>p.jsx(xn,{screen:n,showToggle:t>1},n.uuid))})]})},He=(e,s,o)=>s?!U.isHiddenByCondition(s,o[e]):!0,Fe=(e,s)=>{const o=e.screenOrder.length-1;let t=s+1;for(;t<=o;){const n=e.screenOrder[t],r=e.screens[n],i=r!=null&&r.conditions&&"when"in r.conditions?r.conditions:void 0;if(He(n,i,e.conditionResults))return t;t++}return null},Sn=(e,s)=>{let o=s-1;for(;o>=0;){const t=e.screenOrder[o],n=e.screens[t],r=n!=null&&n.conditions&&"when"in n.conditions?n.conditions:void 0;if(He(t,r,e.conditionResults))return o;o--}return 0},le=new Map,wn=(e,s)=>{le.has(e)||le.set(e,new Set),le.get(e).add(s)},jn=(e,s)=>{var o;(o=le.get(e))==null||o.delete(s)},qe=w.createContext(null);function Rn(){return w.useContext(qe)}class se extends Error{constructor(s,o,t,n){super(s),this.name="ApiError",this.status=o,this.code=t,this.validationErrors=n}}class Ge extends se{constructor(s,o){super(`Request timed out after ${o}ms: ${s}`,0,"TIMEOUT"),this.name="TimeoutError"}}const En=new Set([502,503,504]),ie=2,Nn=[500,1500];function In(e){return new Promise(s=>setTimeout(s,e))}async function Z(e,s,o={}){const n=`${e.apiEndpoint||""}${s}`,r=e.timeout??3e4,i={...o.body?{"Content-Type":"application/json"}:{},...o.headers};e.apiKey&&(i.Authorization=`Bearer ${e.apiKey}`);let a;for(let c=0;c<=ie;c++){c>0&&await In(Nn[c-1]);try{const u=new AbortController,l=setTimeout(()=>u.abort(),r);let d;try{d=await fetch(n,{...o,headers:i,signal:u.signal})}finally{clearTimeout(l)}if(!d.ok){const f=await d.json().catch(()=>({})),m=new se(f.error||`HTTP ${d.status}`,d.status,f.code,f.validationErrors);if(c<ie&&En.has(d.status)){a=m;continue}throw m}return d.json()}catch(u){if(u instanceof se)throw u;if(u instanceof DOMException&&u.name==="AbortError"){const l=new Ge(n,r);if(c<ie){a=l;continue}throw l}if(u instanceof TypeError&&c<ie){a=u;continue}throw u}}throw a}async function Dn(e,s){return Z(e,`/api/v1/public/flows/${s}`)}async function Bn(e,s,o){const t={flowId:s};return o&&Object.keys(o).length>0&&(t.context=o),Z(e,"/api/v1/public/sessions",{method:"POST",body:JSON.stringify(t)})}async function kn(e,s,o){return Z(e,`/api/v1/public/sessions/${s}/next`,{method:"POST",body:JSON.stringify({data:o})})}async function Vn(e,s,o){return Z(e,`/api/v1/public/sessions/${s}/prev`,{method:"POST",body:JSON.stringify({data:o})})}async function On(e,s,o){return Z(e,`/api/v1/public/sessions/${s}/complete`,{method:"POST",body:JSON.stringify({data:o})})}async function Mn(e,s){return Z(e,`/api/v1/public/sessions/${s}`)}const _n=({screenIndex:e,onFlowComplete:s,onScreenChange:o})=>{var te,xe,Se,we,je,Re,Ee,Ne,Ie,De,Be;const t=P(I=>I.config),n=P(I=>I.screenOrder.length),r=P(I=>Object.keys(I.components).length>0),i=be(),a=Math.max(0,Math.min(e,n-1)),[c,u]=w.useState(a),[l,d]=w.useState(!!(t!=null&&t.summary&&e>=n)),f=((te=t==null?void 0:t.navigation)==null?void 0:te.transition)??"none",m=f!=="none",[y,g]=w.useState(null),[h,v]=w.useState(!1),[C,x]=w.useState("forward"),[b,R]=w.useState(!1),_=w.useRef(null);w.useEffect(()=>{const I=Math.max(0,Math.min(e,n-1));u(I)},[e]);const E=P(I=>I.screenOrder[c]),O=P(I=>E?I.screenValidity[E]??!0:!0),F=P(I=>{var L;return E?(L=I.screens[E])==null?void 0:L.nextButtonLabel:void 0}),S=P(I=>{var L;return E?(L=I.screens[E])==null?void 0:L.backButtonLabel:void 0}),D=w.useCallback(I=>{o&&o(I,i.getState().getFlowData())},[o,i]),B=w.useCallback(I=>{g(c),v(l),x(I),R(!0),_.current&&(_.current.scrollTop=0)},[c,l]),H=w.useCallback(()=>{R(!1),g(null)},[]),k=w.useCallback(()=>{if(!b)if(l)m&&B("back"),d(!1),D(c);else{const I=i.getState(),L=Sn(I,c);L!==c&&(m&&B("back"),u(L),D(L))}},[b,l,c,m,B,D,i]),$=w.useCallback(()=>{if(b)return;const I=i.getState(),L=Fe(I,c);L!==null?(m&&B("forward"),u(L),D(L)):t!=null&&t.summary&&(m&&B("forward"),d(!0),D(c+1))},[b,c,t,m,B,D,i]),[q,j]=w.useState(!1),[N,M]=w.useState(null),V=w.useCallback(()=>{M(null);const I=s(i.getState().getFlowData());I&&typeof I.then=="function"&&(j(!0),I.then(()=>{j(!1)}).catch(L=>{j(!1),M(L instanceof Error?L.message:String(L))}))},[s,i]),K=P(I=>Fe(I,c)===null),Y=((xe=t==null?void 0:t.theme)==null?void 0:xe.darkMode)??!1,fe=((Se=t==null?void 0:t.controls)==null?void 0:Se.showStepper)!==!1&&((we=t==null?void 0:t.controls)==null?void 0:we.stepperStyle)==="text",A={};(je=t==null?void 0:t.theme)!=null&&je.color&&(A["--fbre-theme-color"]=t.theme.color);const ee=()=>h?p.jsx(_e,{}):y!==null?p.jsx(Ce,{screenIndex:y}):null,ne=b?"fbre-screen-wrapper fbre-screen-wrapper--transitioning":"fbre-screen-wrapper";return p.jsxs("div",{className:"fbre-container","data-style":((Re=t==null?void 0:t.theme)==null?void 0:Re.style)??"clean","data-mode":Y?"dark":"light","data-transition":m?f:void 0,style:Object.keys(A).length>0?A:void 0,children:[fe&&p.jsx(hn,{current:c,total:n}),p.jsxs("div",{className:ne,ref:_,children:[b&&p.jsx("div",{className:"fbre-screen--exiting","data-direction":C,onAnimationEnd:H,children:ee()}),p.jsxs("div",{className:b?"fbre-screen--entering":void 0,"data-direction":b?C:void 0,children:[!l&&p.jsx(Ce,{screenIndex:c}),l&&p.jsx(_e,{})]})]}),N&&p.jsx("div",{className:"fbre-completion-error",children:N}),((Ee=t==null?void 0:t.controls)==null?void 0:Ee.show)!==!1&&r&&p.jsx(yn,{screenIndex:c,maxScreenIndex:n-1,allowInvalidTransition:(Ne=t==null?void 0:t.navigation)==null?void 0:Ne.allowInvalidTransition,screenValidity:O,summaryEnabled:(t==null?void 0:t.summary)??!1,summaryActive:l,atMaxScreen:K,nextButtonLabel:F,backButtonLabel:S,completionLoading:q,showStepper:(Ie=t==null?void 0:t.controls)==null?void 0:Ie.showStepper,stepperStyle:(De=t==null?void 0:t.controls)==null?void 0:De.stepperStyle,controlsLayout:(Be=t==null?void 0:t.controls)==null?void 0:Be.layout,nextScreen:$,prevScreen:k,onFlowComplete:V})]})},Fn=[],Tn=()=>G.createStore((e,s)=>({flowUUID:"",metadata:{},config:{},externalContext:{},screenOrder:[],screens:{},screenValidity:{},components:{},groupComponentMap:{},templateComponentMap:{},conditionsByDependency:{},conditionResults:{},validationErrors:{},validationsByDependency:{},calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},sessionId:"",navigation:{canGoBack:!1,canGoForward:!1,screenNumber:1,totalScreens:1,progress:0,isLastScreen:!0},loading:!1,error:"",clearError:()=>{e({error:""})},loadFlow:()=>{},updateContext:o=>{e({externalContext:o})},updateComponentValue:(o,t)=>{var f,m;const n=s();if(o.includes(":")){const y=o.split(":"),g=y[0],h=parseInt(y[1]),v=parseInt(y[2]),C=n.components[g];if(!((m=(f=C==null?void 0:C.addedComponents)==null?void 0:f[h])!=null&&m[v]))return;C.addedComponents[h][v]={...C.addedComponents[h][v],value:t};const x=n.groupComponentMap[o]??o,b=n.conditionsByDependency[x];let R=n.conditionResults;if(b&&b.length>0){R={...n.conditionResults};for(const S of b)R[S.target]=Q({...n},S.config)}const _={components:n.components,groupComponentMap:n.groupComponentMap},E=T(C.addedComponents[h][v],_);C.addedComponents[h][v].valid=E.length===0;const O={...n.validationErrors,[o]:E},F=n.validationsByDependency[o];if(F)for(const S of F){const D=Te(n,S.target);if(D){const B=T(D,_);D.valid=B.length===0,O[S.target]=B}}e({components:{...n.components},conditionResults:R,validationErrors:O});return}const r=n.components[o];if(!r)return;if(r.type==="group"||r.type==="repeater"){e({components:{...n.components}});return}if(t===r.value)return;r.value=t;const i=n.conditionsByDependency[o];let a=n.conditionResults;if(i&&i.length>0){a={...n.conditionResults};for(const y of i)a[y.target]=Q({...n},y.config)}const c={components:n.components,groupComponentMap:n.groupComponentMap},u=T(r,c);r.valid=u.length===0;const l={...n.validationErrors,[o]:u},d=n.validationsByDependency[o];if(d)for(const y of d){const g=Te(n,y.target);if(g){const h=T(g,c);g.valid=h.length===0,l[y.target]=h}}e({components:{...n.components},conditionResults:a,validationErrors:l})},addGroupIteration:o=>{const t=s(),n=t.components[o];if(!n||n.type!=="group"||!n.components)return;n.addedComponents||(n.addedComponents=[]);const r=n.components.map((i,a)=>{const c=z(i);c.uuid=[o,n.addedComponents.length,a].join(":"),t.groupComponentMap[c.uuid]=i.uuid;const u=T(c);return c.valid=u.length===0,c});return n.addedComponents.push(r),e({components:{...t.components},groupComponentMap:{...t.groupComponentMap}}),n.addedComponents},addRepeaterIteration:o=>{const t=s(),n=t.components[o];if(!n||n.type!=="repeater"||!n.components)return;n.addedComponents||(n.addedComponents=[]);const r=n.components.map((i,a)=>{const c=z(i);c.uuid=[o,n.addedComponents.length,a].join(":");const u=T(c);return c.valid=u.length===0,c});return n.addedComponents.push(r),e({components:{...t.components}}),n.addedComponents},removeRepeaterIteration:(o,t)=>{const n=s(),r=n.components[o];if(!(!r||r.type!=="repeater"||!r.addedComponents||r.addedComponents.length<=1)){r.addedComponents.splice(t,1);for(let i=t;i<r.addedComponents.length;i++)r.addedComponents[i].forEach((a,c)=>{a.uuid=[o,i,c].join(":")});e({components:{...n.components}})}},getFlowData:()=>({uuid:"",metadata:{},screens:[]}),getScreenByIndex:o=>{const t=s();if(o!==0)return null;const n=t.screenOrder[0];return n?t.screens[n]:null},getScreenValidity:o=>{const t=s();if(o!==0)return!0;const n=t.screenOrder[0];return n?t.screenValidity[n]??!0:!0},getValidationErrors:o=>s().validationErrors[o]??Fn,evaluateFlowValidation:()=>{const o=s(),t=o.screenOrder[0];if(!t)return!0;const n=o.screens[t];if(!n)return!0;let r=!0;const i={components:o.components,groupComponentMap:o.groupComponentMap},a={...o.validationErrors};for(const u of n.components){const l=o.components[u.uuid];if(!l)continue;if((l.type==="group"||l.type==="repeater")&&l.addedComponents){for(const m of l.addedComponents)for(const y of m){const g=y.conditions&&"when"in y.conditions?y.conditions:void 0;if(g){const v=o.conditionResults[y.uuid]??!1;if(g.action==="show"&&!v||g.action==="hide"&&v)continue}const h=T(y,i);y.valid=h.length===0,a[y.uuid]=h,h.length>0&&(r=!1)}continue}const d=l.conditions&&"when"in l.conditions?l.conditions:void 0;if(d){const m=o.conditionResults[l.uuid]??!1;if(d.action==="show"&&!m||d.action==="hide"&&m)continue}const f=T(l,i);l.valid=f.length===0,a[l.uuid]=f,f.length>0&&(r=!1)}const c={...o.screenValidity,[t]:r};return e({components:{...o.components},validationErrors:a,screenValidity:c}),r},getMaxScreenCount:()=>0,getCalculationResult:o=>s().calculationResults[o]??null})),ce=(e,s)=>{var y,g;const o={},t={},n={},r={},i={},a={},c=s.screen.uuid;for(const h of s.screen.components){const v=z(h);if(o[v.uuid]=v,(v.type==="group"||v.type==="repeater")&&h.components){v.addedComponents||(v.addedComponents=[]);const C=h.components.map((x,b)=>{var _;const R=z(x);if(R.uuid=[v.uuid,0,b].join(":"),v.type==="group"&&(t[R.uuid]=x.uuid),n[x.uuid]=R.uuid,(_=R.properties)!=null&&_.validation){const E=R.properties.validation;E.rules&&E.rules.length>0&&oe(i,R.uuid,E)}return R});v.addedComponents.push(C)}if((y=v.properties)!=null&&y.validation){const C=v.properties.validation;C.rules&&C.rules.length>0&&oe(i,v.uuid,C)}}for(const{targetUUID:h,config:v}of s.conditions)W(r,h,v,h===c?"screen":"component");if(s.previousData)for(const h of s.previousData){const v=o[h.uuid];v&&(v.value=h.value)}const u={components:o};for(const h of Object.keys(o)){const v=o[h];if(v.type==="group"||v.type==="repeater"){if(v.addedComponents)for(const x of v.addedComponents)for(const b of x){const R=T(b,u);b.valid=R.length===0,R.length>0&&(a[b.uuid]=R)}continue}if(!((g=v.properties)!=null&&g.validation))continue;const C=T(v,u);v.valid=C.length===0,C.length>0&&(a[h]=C)}const l={},d={components:o},f=new Set;for(const h of Object.values(r))for(const v of h)f.has(v.target)||(f.add(v.target),l[v.target]=Q(d,v.config));let m=!0;for(const h of s.screen.components){const v=o[h.uuid];if(v)if((v.type==="group"||v.type==="repeater")&&v.addedComponents)for(const C of v.addedComponents){for(const x of C)if(!x.valid){m=!1;break}if(!m)break}else v.valid===!1&&(m=!1)}e.setState({screenOrder:[c],screens:{[c]:{uuid:c,label:s.screen.label,components:s.screen.components,nextButtonLabel:s.screen.nextButtonLabel,backButtonLabel:s.screen.backButtonLabel}},screenValidity:{[c]:m},components:o,groupComponentMap:t,templateComponentMap:n,conditionsByDependency:r,conditionResults:l,validationErrors:a,validationsByDependency:i,calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},sessionId:s.sessionId,navigation:s.navigation,loading:!1,error:"",externalContext:s.context??{},config:s.config??{}})},he=e=>{var r;const s=e.getState(),o=s.screenOrder[0];if(!o)return[];const t=s.screens[o];if(!t)return[];const n=[];for(const i of t.components){const a=s.components[i.uuid];if(!a||ue(a.type))continue;const c={uuid:a.uuid,type:a.type,value:a.value};(r=a.properties)!=null&&r.label&&(c.label=a.properties.label),(a.type==="group"||a.type==="repeater")&&a.addedComponents&&(c.components=a.addedComponents.map(u=>u.filter(l=>!ue(l.type)).map(l=>({uuid:l.uuid,type:l.type,value:l.value})))),n.push(c)}return n},Te=(e,s)=>Pe(e.components,s),Un=6e4,An=({sessionEndpoint:e,flowId:s,apiKey:o,theme:t,context:n,onFlowComplete:r,onScreenChange:i})=>{const a=w.useRef(null);a.current||(a.current=Tn());const c=a.current,u=G.useStore(c,j=>j.loading),l=G.useStore(c,j=>j.error),d=G.useStore(c,j=>j.navigation),f=G.useStore(c,j=>j.config),m=G.useStore(c,j=>j.screenOrder),y=G.useStore(c,j=>j.screens),[g,h]=w.useState(""),v=w.useRef(null),C=w.useRef(Date.now()),x=w.useMemo(()=>({apiEndpoint:e,apiKey:o}),[e,o]),b=w.useCallback(j=>{h(j),v.current&&clearTimeout(v.current),v.current=setTimeout(()=>h(""),8e3)},[]),R=w.useCallback(()=>{h(""),v.current&&(clearTimeout(v.current),v.current=null)},[]),_=w.useCallback(async()=>{c.setState({loading:!0,error:""});try{const j=await Bn(x,s,n);ce(c,j)}catch(j){c.setState({loading:!1,error:j.message})}},[s,e,o,n]);w.useEffect(()=>{_()},[_]),w.useEffect(()=>{const j=async()=>{if(document.visibilityState!=="visible")return;const N=c.getState();if(!(!N.sessionId||Date.now()-C.current<Un))try{const V=await Mn(x,N.sessionId);ce(c,V)}catch(V){V instanceof se&&V.status===404&&c.setState({loading:!1,error:"Session expired. Please start over.",screenOrder:[],screens:{}})}};return document.addEventListener("visibilitychange",j),()=>document.removeEventListener("visibilitychange",j)},[e,o]);const E=w.useCallback(async()=>{const j=c.getState();if(!(j.loading||!j.evaluateFlowValidation())){c.setState({loading:!0}),c.getState().clearError(),R();try{const M=he(c),V=await kn(x,j.sessionId,M);ce(c,V),C.current=Date.now(),i==null||i(V.navigation.screenNumber)}catch(M){c.setState({loading:!1}),b(M.message)}}},[x,c,i,b,R]),O=w.useCallback(async()=>{const j=c.getState();if(!j.loading){c.setState({loading:!0}),c.getState().clearError(),R();try{const N=he(c),M=await Vn(x,j.sessionId,N);ce(c,M),C.current=Date.now(),i==null||i(M.navigation.screenNumber)}catch(N){c.setState({loading:!1}),b(N.message)}}},[x,c,i,b,R]),F=w.useCallback(async()=>{const j=c.getState();if(!(j.loading||!j.evaluateFlowValidation())){c.setState({loading:!0}),c.getState().clearError(),R();try{const M=he(c),V=await On(x,j.sessionId,M);C.current=Date.now(),r(V)}catch(M){c.setState({loading:!1}),b(M.message)}}},[x,c,r,b,R]),S={...f==null?void 0:f.theme,...t},D=S.darkMode??!1,B={};S.color&&(B["--fbre-theme-color"]=S.color);const H=m[0],k=H?y[H]:void 0,$=k==null?void 0:k.nextButtonLabel,q=k==null?void 0:k.backButtonLabel;return u&&m.length===0?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-loading",children:[p.jsx("div",{className:"fbre-spinner"}),p.jsx("p",{children:"Loading form..."})]})}):l&&m.length===0?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-error",children:[p.jsx("p",{children:"Failed to load form"}),p.jsx("p",{className:"fbre-remote-error__detail",children:l}),p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--next",onClick:_,style:{marginTop:12},children:"Retry"})]})}):m.length===0?null:p.jsx(pe.Provider,{value:c,children:p.jsxs("div",{className:"fbre-container","data-style":S.style??"clean","data-mode":D?"dark":"light",style:Object.keys(B).length>0?B:void 0,children:[p.jsx("div",{className:"fbre-screen-wrapper",children:p.jsx(Ce,{screenIndex:0})}),p.jsx(Ln,{navigation:d,loading:u,onPrev:O,onNext:E,onComplete:F,nextButtonLabel:$,backButtonLabel:q}),p.jsxs("div",{className:`fbre-toast${g?" visible":""}`,children:[p.jsx("span",{children:g}),p.jsx("button",{type:"button",className:"fbre-toast__close",onClick:R,"aria-label":"Dismiss",children:"×"})]})]})})},Ln=({navigation:e,loading:s,onPrev:o,onNext:t,onComplete:n,nextButtonLabel:r,backButtonLabel:i})=>p.jsxs("div",{className:"fbre-controls",children:[p.jsx("div",{className:"fbre-controls__left",children:e.canGoBack&&p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--back",onClick:o,disabled:s,children:i||"Back"})}),p.jsx("div",{className:"fbre-controls__center",children:p.jsxs("span",{className:"fbre-controls__progress",children:[e.screenNumber," / ",e.totalScreens]})}),p.jsx("div",{className:"fbre-controls__right",children:e.isLastScreen?p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--complete",onClick:n,disabled:s,children:s?"Submitting...":r||"Complete"}):p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--next",onClick:t,disabled:s,children:s?"Loading...":r||"Next"})})]});function Pn(e,s,o,t){return!s&&!o&&!t?e:{...e,...s&&{theme:{...e.theme,...s}},...o&&{navigation:{...e.navigation,...o}},...t&&{controls:{...e.controls,...t}}}}const ze=({flow:e,data:s,theme:o,navigation:t,controls:n,screenIndex:r,context:i,storeRef:a,onFlowComplete:c,onScreenChange:u,onScreenValidationChange:l,apiCtx:d})=>{const f=w.useRef(null);f.current||(f.current=un(),f.current.getState().loadFlow(e,s,i)),a&&(a.current=f.current),w.useEffect(()=>{f.current&&f.current.getState().loadFlow(e,s,i)},[e.uuid]),w.useEffect(()=>{if(f.current){const g=f.current.getState(),h=Pn(g.config,o,t,n);h!==g.config&&f.current.setState({config:h})}},[o,t,n]);const m=i?JSON.stringify(i):"";w.useEffect(()=>{f.current&&i&&f.current.getState().updateContext(i)},[m]);const y=p.jsx(pe.Provider,{value:f.current,children:p.jsx(_n,{screenIndex:r??0,onFlowComplete:c,onScreenChange:u,onScreenValidationChange:l})});return d?p.jsx(qe.Provider,{value:d,children:y}):y},$n=({flowId:e,apiEndpoint:s,apiKey:o,data:t,theme:n,navigation:r,controls:i,screenIndex:a,context:c,storeRef:u,onFlowComplete:l,onScreenChange:d,onScreenValidationChange:f})=>{const[m,y]=w.useState(null),[g,h]=w.useState(""),[v,C]=w.useState(null);return w.useEffect(()=>{y(null),h("");const x={apiEndpoint:s,apiKey:o};Dn(x,e).then(b=>{y(b.data),C({config:x,flowId:b.id,flowVersionId:b.versionId,tenantId:b.tenantId})}).catch(b=>h(b.message))},[e,s,o]),g?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-error",children:[p.jsx("p",{children:"Failed to load form"}),p.jsx("p",{className:"fbre-remote-error__detail",children:g})]})}):!m||!v?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-loading",children:[p.jsx("div",{className:"fbre-spinner"}),p.jsx("p",{children:"Loading form..."})]})}):p.jsx(ze,{flow:m,data:t,theme:n,navigation:r,controls:i,screenIndex:a,context:c,storeRef:u,onFlowComplete:l,onScreenChange:d,onScreenValidationChange:f,apiCtx:v})},Hn=e=>"sessionEndpoint"in e?p.jsx(An,{...e}):"flowId"in e&&e.flowId?p.jsx($n,{...e}):p.jsx(ze,{...e});exports.ApiError=se;exports.FBRE=Hn;exports.TimeoutError=Ge;exports.addFBREEventListener=wn;exports.removeFBREEventListener=jn;exports.useFBREApi=Rn;exports.useFBREStore=P;exports.useFBREStoreApi=be;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("react/jsx-runtime"),x=require("react"),z=require("zustand"),on=require("@sonata-innovations/fiber-types"),M=require("@sonata-innovations/fiber-shared"),sn=new Map,rn=n=>sn.get(n),cn=new Set(["header","text","divider","callout","table"]),pe=n=>cn.has(n),an=(n,s)=>{var o,t,e,r;if(s.includes(":")){const c=s.split(":"),i=n.components[c[0]];return(e=(t=(o=i==null?void 0:i.addedComponents)==null?void 0:o[parseInt(c[1])])==null?void 0:t[parseInt(c[2])])==null?void 0:e.value}return(r=n.components[s])==null?void 0:r.value},_=(n,s)=>{var t;const o=(t=n.properties)==null?void 0:t.validation;return!o||!o.rules||o.rules.length===0?[]:M.validateValue(n.value,o,s?e=>an(s,e):void 0)},ln=n=>{var o;const s=(o=n.properties)==null?void 0:o.validation;return!s||!s.rules?!1:s.rules.some(t=>t.type==="required")},W=n=>{var o,t;const s={...n};return Array.isArray((o=s.properties)==null?void 0:o.options)&&(s.properties={...s.properties,options:on.normalizeOptions(s.properties.options)}),n.type==="computed"?(s.value=null,s.valid=!0):!pe(n.type)&&n.type!=="group"&&n.type!=="repeater"&&(s.value=null,s.valid=!ln(n)),(n.type==="checkbox"||n.type==="dropDownMulti")&&(s.value=[]),n.type==="radio"&&"options"in n.properties&&n.properties.options.length>0&&(s.value=n.properties.options[0].value),n.type==="toggleSwitch"&&(s.value=!1),n.type==="colorPicker"&&((t=n.properties)!=null&&t.defaultValue)&&(s.value=n.properties.defaultValue),n.type==="slider"&&(s.value="initValue"in n.properties?n.properties.initValue:"min"in n.properties?n.properties.min:0),s},se=(n,s,o)=>{var t;for(const e of o.rules)if(e.type==="matchesField"&&((t=e.params)!=null&&t.field)){const r=e.params.field;n[r]||(n[r]=[]),n[r].push({target:s,config:o})}},Z=(n,s)=>{const o=n.screens[s];return o?!o.components.some(e=>{var i;const r=n.components[e.uuid];if(!r)return!1;if(r.type==="group"||r.type==="repeater")return(i=r.addedComponents)==null?void 0:i.some(l=>l.some(a=>{const u=a.conditions&&"when"in a.conditions?a.conditions:void 0;return u&&M.isHiddenByCondition(u,n.conditionResults[a.uuid])?!1:!a.valid}));const c=r.conditions&&"when"in r.conditions?r.conditions:void 0;return M.isHiddenByCondition(c,n.conditionResults[r.uuid])?!1:r&&"valid"in r&&!r.valid}):!0};function un(n){const s={};for(const o of n){const t=M.extractFormulaReferences(o.formula);for(const e of t)s[e]||(s[e]=[]),s[e].includes(o.uuid)||s[e].push(o.uuid)}return s}function qe(n){const s=new Map(n.map(c=>[c.uuid,c])),o=new Set(n.map(c=>c.uuid)),t=new Set,e=[];function r(c,i){if(t.has(c)||i.has(c))return;i.add(c);const l=s.get(c);if(!l)return;const a=M.extractFormulaReferences(l.formula);for(const u of a)o.has(u)&&r(u,i);i.delete(c),t.add(c),e.push(l)}for(const c of n)r(c.uuid,new Set);return e}function He(n,s){return{resolveValue:o=>{if(o in s)return s[o];const t=K(n,o);if(!t)return null;const e=t.value;if(e==null||e==="")return null;const r=Number(e);return isNaN(r)?null:r},resolveOptionMetadata:(o,t)=>{var u;const e=K(n,o);if(!e)return null;const r=e.value;if(!r)return null;const c=(u=e.properties)==null?void 0:u.options;if(!Array.isArray(c))return null;const i=c.find(d=>d.value===r||d.label===r);if(!(i!=null&&i.metadata))return null;const l=i.metadata[t];if(l==null)return null;const a=Number(l);return isNaN(a)?null:a},resolveRepeaterValues:o=>{for(const t of Object.keys(n.components)){const e=n.components[t];if(e.type!=="repeater"||!e.addedComponents||!e.components)continue;const r=e.components.findIndex(i=>i.uuid===o);if(r===-1)continue;const c=[];for(const i of e.addedComponents){const l=i[r];if(!l)continue;const a=l.value;if(a==null||a==="")continue;const u=Number(a);isNaN(u)||c.push(u)}return c.length>0?c:null}return null}}}function be(n){const s=n.calculations??[];if(s.length===0)return{};const o=qe(s),t={},e=He(n,t);for(const r of o)t[r.uuid]=M.evaluateFormula(r.formula,e);return t}function ye(n,s){const o=n.calculationsByDependency,t=n.calculations??[];if(t.length===0||!o)return null;const e=new Set,r=[s];for(;r.length>0;){const u=r.shift(),d=o[u];if(d)for(const m of d)e.has(m)||(e.add(m),r.push(m))}if(e.size===0)return null;const c=qe(t),i={...n.calculationResults},l=He(n,i);let a=!1;for(const u of c)if(e.has(u.uuid)){const d=M.evaluateFormula(u.formula,l);i[u.uuid]!==d&&(i[u.uuid]=d,a=!0)}return a?i:null}function dn(n){const s={},o=t=>{var r;if(t.type!=="computed"||!((r=t.properties)!=null&&r.formula))return;const e=M.extractFormulaReferences(t.properties.formula);for(const c of e)s[c]||(s[c]=[]),s[c].includes(t.uuid)||s[c].push(t.uuid)};for(const t of Object.values(n))if(o(t),(t.type==="repeater"||t.type==="group")&&t.components)for(const e of t.components)o(e);return s}function Ye(n,s,o,t){var l,a;const e=n.components[o],r=((l=e==null?void 0:e.components)==null?void 0:l.map(u=>u.uuid))??[],c=(a=e==null?void 0:e.addedComponents)==null?void 0:a[t],i=new Map;if(c)for(let u=0;u<c.length;u++){const d=r[u];d&&i.set(d,c[u])}return{resolveValue:u=>{if(u in s)return s[u];const d=i.get(u);if(d){const g=d.value;if(g==null||g==="")return null;const h=Number(g);return isNaN(h)?null:h}const m=K(n,u);if(!m)return null;const f=m.value;if(f==null||f==="")return null;const y=Number(f);return isNaN(y)?null:y},resolveOptionMetadata:(u,d)=>{var b;const f=i.get(u)??K(n,u);if(!f)return null;const y=f.value;if(!y)return null;const g=(b=f.properties)==null?void 0:b.options;if(!Array.isArray(g))return null;const h=g.find(S=>S.value===y||S.label===y);if(!(h!=null&&h.metadata))return null;const v=h.metadata[d];if(v==null)return null;const C=Number(v);return isNaN(C)?null:C},resolveRepeaterValues:u=>{for(const d of Object.keys(n.components)){const m=n.components[d];if(m.type!=="repeater"||!m.addedComponents||!m.components)continue;const f=m.components.findIndex(g=>g.uuid===u);if(f===-1)continue;const y=[];for(const g of m.addedComponents){const h=g[f];if(!h)continue;const v=h.value;if(v==null||v==="")continue;const C=Number(v);isNaN(C)||y.push(C)}return y.length>0?y:null}return null}}}function fe(n,s){return{resolveValue:o=>{if(o in s)return s[o];const t=K(n,o);if(!t)return null;const e=t.value;if(e==null||e==="")return null;const r=Number(e);return isNaN(r)?null:r},resolveOptionMetadata:(o,t)=>{var u;const e=K(n,o);if(!e)return null;const r=e.value;if(!r)return null;const c=(u=e.properties)==null?void 0:u.options;if(!Array.isArray(c))return null;const i=c.find(d=>d.value===r||d.label===r);if(!(i!=null&&i.metadata))return null;const l=i.metadata[t];if(l==null)return null;const a=Number(l);return isNaN(a)?null:a},resolveRepeaterValues:o=>{for(const t of Object.keys(n.components)){const e=n.components[t];if(e.type!=="repeater"||!e.addedComponents||!e.components)continue;const r=e.components.findIndex(i=>i.uuid===o);if(r===-1)continue;const c=[];for(const i of e.addedComponents){const l=i[r];if(!l)continue;const a=l.value;if(a==null||a==="")continue;const u=Number(a);isNaN(u)||c.push(u)}return c.length>0?c:null}return null}}}function xe(n){var o,t;const s=n.calculationResults??{};for(const e of Object.values(n.components))if(e.type==="computed"&&((o=e.properties)!=null&&o.formula)){const r=fe(n,s),c=M.evaluateFormula(e.properties.formula,r);e.value=c}for(const e of Object.values(n.components))if((e.type==="repeater"||e.type==="group")&&e.addedComponents&&e.components)for(let r=0;r<e.addedComponents.length;r++){const c=e.addedComponents[r];for(let i=0;i<c.length;i++){const l=c[i],a=e.components[i];if((a==null?void 0:a.type)==="computed"&&((t=a.properties)!=null&&t.formula)){const u=e.type==="repeater"?Ye(n,s,e.uuid,r):fe(n,s),d=M.evaluateFormula(a.properties.formula,u);l.value=d}}}}function pn(n,s){var c,i;const o=n.computedByDependency;if(!o)return!1;const t=o[s];if(!t||t.length===0)return!1;const e=n.calculationResults??{};let r=!1;for(const l of t){const a=n.components[l];if(a&&a.type==="computed"&&((c=a.properties)!=null&&c.formula)){const u=fe(n,e),d=M.evaluateFormula(a.properties.formula,u);a.value!==d&&(n.components[l]={...a,value:d},r=!0);continue}for(const u of Object.values(n.components))if((u.type==="repeater"||u.type==="group")&&u.components&&u.addedComponents){const d=u.components.findIndex(f=>f.uuid===l);if(d===-1)continue;const m=u.components[d];if((m==null?void 0:m.type)!=="computed"||!((i=m.properties)!=null&&i.formula))continue;for(let f=0;f<u.addedComponents.length;f++){const y=u.addedComponents[f][d];if(!y)continue;const g=u.type==="repeater"?Ye(n,e,u.uuid,f):fe(n,e),h=M.evaluateFormula(m.properties.formula,g);y.value!==h&&(u.addedComponents[f][d]={...y,value:h},r=!0)}}}return r}const Ge=(n,s)=>{var o,t;if(s.includes(":")){const e=s.split(":"),r=n[e[0]];return(t=(o=r==null?void 0:r.addedComponents)==null?void 0:o[parseInt(e[1])])==null?void 0:t[parseInt(e[2])]}return n[s]},ue=(n,s)=>{for(const o of n.screenOrder)if(n.screens[o].components.some(e=>e.uuid===s))return o;return null},fn=(n,s)=>{if(!s.includes(":"))return null;const o=s.split(":");if(o.length!==3)return null;const t=o[0],e=parseInt(o[2]),r=n.components[t];if(!r||r.type!=="repeater"||!r.components)return null;const c=r.components[e];return(c==null?void 0:c.uuid)??null},_e=(n,s,o,t,e)=>{const r=s(),c=r.groupComponentMap[o]??o,i=hn(n,s,c),l={components:r.components,groupComponentMap:r.groupComponentMap},a=_(t,l);t.valid=a.length===0;const u={...r.validationErrors,[o]:a},d=r.validationsByDependency[o];if(d)for(const b of d){const S=K(r,b.target);if(S){const w=_(S,l);S.valid=w.length===0,u[b.target]=w}}const m=ue(r,e),f={components:{...r.components},validationErrors:u};if(i&&(f.conditionResults=i),m){const b={...r,...f};f.screenValidity={...r.screenValidity,[m]:Z(b,m)}}const y={...r,...f},g=o.includes(":")?fn(y,o):null,h=[],v=b=>{if(pn(y,b)){const S=y.computedByDependency[b]??[];for(const w of S)h.includes(w)||(h.push(w),v(w))}};v(c),g&&g!==c&&v(g);let C=ye(y,c);if(g&&g!==c){const b=C?{...y,calculationResults:C}:y,S=ye(b,g);S&&(C=S)}for(const b of h){const S=C?{...y,calculationResults:C}:y,w=ye(S,b);w&&(C=w)}C&&(f.calculationResults=C),n(f)},mn=(n,s)=>({updateComponentValue:(o,t)=>{var c,i;const e=s();if(o.includes(":")){const l=o.split(":"),a=l[0],u=parseInt(l[1]),d=parseInt(l[2]),m=e.components[a];if(!((i=(c=m==null?void 0:m.addedComponents)==null?void 0:c[u])!=null&&i[d]))return;const f=m.addedComponents[u][d].value!==t;if(m.addedComponents[u][d]={...m.addedComponents[u][d],value:t},f){_e(n,s,o,m.addedComponents[u][d],a);return}n({components:{...e.components}});return}const r=e.components[o];if(r){if(r.type==="group"||r.type==="repeater"){n({components:{...e.components}});return}if(t===r.value){n({selectionTick:s().selectionTick+1});return}r.value=t,_e(n,s,o,r,o)}},addGroupIteration:o=>{const t=s(),e=t.components[o];if(!e||e.type!=="group"||!e.components)return;e.addedComponents||(e.addedComponents=[]);const r={...t.conditionsByDependency},c=e.components.map((a,u)=>{const d=W(a);if(d.uuid=[o,e.addedComponents.length,u].join(":"),t.groupComponentMap[d.uuid]=a.uuid,d.conditions&&Object.keys(d.conditions).length>0){const f="when"in d.conditions?d.conditions:null;f&&(d.conditions=f,Q(r,d.uuid,f,"component"))}const m=_(d);return d.valid=m.length===0,d});e.addedComponents.push(c);const i=ue(t,o),l={...t.screenValidity};return i&&(l[i]=Z({...t},i)),n({components:{...t.components},groupComponentMap:{...t.groupComponentMap},conditionsByDependency:r,screenValidity:l}),e.addedComponents},addRepeaterIteration:o=>{const t=s(),e=t.components[o];if(!e||e.type!=="repeater"||!e.components)return;e.addedComponents||(e.addedComponents=[]);const r={...t.conditionsByDependency},c=e.components.map((d,m)=>{const f=W(d);if(f.uuid=[o,e.addedComponents.length,m].join(":"),f.conditions&&Object.keys(f.conditions).length>0){const g="when"in f.conditions?f.conditions:null;g&&(f.conditions=g,Q(r,f.uuid,g,"component"))}const y=_(f);return f.valid=y.length===0,f});e.addedComponents.push(c);const i=ue(t,o),l={...t.screenValidity};i&&(l[i]=Z({...t},i));const a={...t,components:{...t.components}};xe(a);const u=be(a);return n({components:{...t.components},conditionsByDependency:r,screenValidity:l,calculationResults:u}),e.addedComponents},removeRepeaterIteration:(o,t)=>{var g;const e=s(),r=e.components[o];if(!r||r.type!=="repeater"||!r.addedComponents)return;const c=((g=r.properties)==null?void 0:g.minIterations)??1;if(r.addedComponents.length<=c)return;const i=r.addedComponents[t],l=new Set(i.map(h=>h.uuid));r.addedComponents.splice(t,1);const a={...e.conditionsByDependency},u={...e.validationErrors};for(const h of l)delete u[h],delete a[h];for(const h of Object.keys(a)){const v=a[h],C=v.filter(b=>!l.has(b.target));C.length===0?delete a[h]:C.length!==v.length&&(a[h]=C)}for(let h=t;h<r.addedComponents.length;h++)r.addedComponents[h].forEach((v,C)=>{const b=v.uuid,S=[o,h,C].join(":");if(b!==S){v.uuid=S,b in u&&(u[S]=u[b],delete u[b]),b in a&&(a[S]=a[b],delete a[b]);for(const w of Object.values(a))for(const O of w)O.target===b&&(O.target=S)}});const d=ue(e,o),m={...e.screenValidity};d&&(m[d]=Z({...e},d));const f={...e,components:{...e.components}};xe(f);const y=be(f);n({components:{...e.components},conditionsByDependency:a,validationErrors:u,screenValidity:m,calculationResults:y})}}),K=(n,s)=>Ge(n.components,s),Q=(n,s,o,t)=>{const e=new Set;for(const c of o.when.rules)e.add(c.source);const r={target:s,type:t,config:o};for(const c of e)n[c]||(n[c]=[]),n[c].push(r)},vn=(n,s)=>{var o,t;return s.includes(":")?(o=K(n,s))==null?void 0:o.value:(t=n.components[s])==null?void 0:t.value},te=(n,s)=>M.evaluateConditionConfig(s,(o,t)=>(t==null?void 0:t.sourceType)==="context"?n.externalContext[t.source]:vn(n,o)),hn=(n,s,o)=>{const t=s(),e=t.conditionsByDependency[o];if(!e||e.length===0)return null;let r=!1;const c={...t.conditionResults};for(const i of e){const l=te(t,i.config);c[i.target]!==l&&(c[i.target]=l,r=!0)}return r?c:null},yn=n=>{const s={},o=new Set;for(const t of Object.values(n.conditionsByDependency))for(const e of t)o.has(e.target)||(o.add(e.target),s[e.target]=te(n,e.config));return s},ge=n=>{if(!n||pe(n.type))return null;const s="label"in n.properties?n.properties.label:"",o={uuid:n.uuid,type:n.type,value:n.value};return s&&(o.label=s),o},Me=n=>{if(n.conditions&&"when"in n.conditions)return n.conditions},gn=n=>{const s={uuid:n.flowUUID,metadata:n.metadata,screens:[]},o=t=>{const e=n.screens[t];if(!e)return!1;const r=e.conditions&&"when"in e.conditions?e.conditions:void 0;return M.isHiddenByCondition(r,n.conditionResults[t])};return s.screens=n.screenOrder.filter(t=>!o(t)).map(t=>{const e=n.screens[t],r={uuid:t,components:e.components.map(c=>{const i=n.components[c.uuid];if(!i)return null;const l=Me(i);if(M.isHiddenByCondition(l,n.conditionResults[c.uuid]))return null;if((i.type==="group"||i.type==="repeater")&&i.addedComponents){const a=ge(i);return a?{...a,components:i.addedComponents.map(u=>u.filter(d=>{const m=Me(d);return!M.isHiddenByCondition(m,n.conditionResults[d.uuid])}).map(d=>ge(d)).filter(d=>d!==null))}:null}return ge(i)}).filter(c=>c!==null)};return e.label&&(r.label=e.label),r}),s},ve=x.createContext(null);function L(n){const s=x.useContext(ve);if(!s)throw new Error("useFBREStore must be used within an FBRE provider");return z.useStore(s,n)}const ie=()=>{const n=x.useContext(ve);if(!n)throw new Error("useFBREStoreApi must be used within an FBRE provider");return n},ce=n=>!n||Object.keys(n).length===0?null:"when"in n?n:null,Cn=[],bn=()=>z.createStore((n,s)=>({flowUUID:"",metadata:{},config:{},externalContext:{},screenOrder:[],screens:{},screenValidity:{},components:{},groupComponentMap:{},templateComponentMap:{},conditionsByDependency:{},conditionResults:{},validationErrors:{},validationsByDependency:{},calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},selectionTick:0,loadFlow:(o,t,e)=>{var O;const r={},c=[],i={},l={},a={},u={},d={},m={},f={};o.screens.forEach(E=>{const T=ce(E.conditions),V=T?{...E,conditions:T}:{...E};r[E.uuid]=V,c.push(E.uuid),T&&Q(u,E.uuid,T,"screen"),E.components.forEach(j=>{var U;const I=ce(j.conditions),k=W(j);if(I&&(k.conditions=I),i[j.uuid]=k,I&&Q(u,j.uuid,I,"component"),(U=k.properties)!=null&&U.validation&&se(d,j.uuid,k.properties.validation),(j.type==="group"||j.type==="repeater")&&j.components){const A=i[j.uuid];A.addedComponents||(A.addedComponents=[]);const G=j.properties??{},P=j.type==="repeater"?G.initialData:void 0,B=j.type==="repeater"?G.minIterations:void 0,q=P?P.length:j.type==="repeater"&&B&&B>1?B:1;for(let H=0;H<q;H++){const X=j.components.map((R,Y)=>{var ne;const N=W(R);N.uuid=[j.uuid,A.addedComponents.length,Y].join(":"),P&&P[H]&&R.uuid in P[H]&&(N.value=P[H][R.uuid]),j.type==="group"&&(l[N.uuid]=R.uuid),H===0&&(a[R.uuid]=N.uuid);const $=ce(N.conditions);$&&(N.conditions=$,Q(u,N.uuid,$,"component")),(ne=N.properties)!=null&&ne.validation&&se(d,N.uuid,N.properties.validation);const ee=_(N);return N.valid=ee.length===0,ee.length>0&&(m[N.uuid]=ee),N});A.addedComponents.push(X)}}})}),t&&t.screens.forEach(E=>{const T=c.indexOf(E.uuid);T<0||E.components.forEach(V=>{const j=i[V.uuid];if(j)if((j.type==="group"||j.type==="repeater")&&V.components&&j.addedComponents){if(V.components.length>1){const I=j.addedComponents.length,k=V.components.length-I,J=o.screens[T].components.find(U=>U.uuid===j.uuid);for(let U=0;U<k;U++)if(J!=null&&J.components){const A=J.components.map((G,P)=>{var X;const B=W(G);B.uuid=[j.uuid,j.addedComponents.length,P].join(":"),j.type==="group"&&(l[B.uuid]=G.uuid),a[G.uuid]||(a[G.uuid]=B.uuid);const q=ce(B.conditions);q&&(B.conditions=q,Q(u,B.uuid,q,"component")),(X=B.properties)!=null&&X.validation&&se(d,B.uuid,B.properties.validation);const H=_(B);return B.valid=H.length===0,H.length>0&&(m[B.uuid]=H),B});j.addedComponents.push(A)}}V.components.forEach(I=>{I.forEach(k=>{for(const J of j.addedComponents){const U=J.find(A=>A.uuid===k.uuid);if(U){U.value=k.value;const A=_(U);U.valid=A.length===0,m[U.uuid]=A;break}}})})}else{j.value=V.value;const I=_(j);j.valid=I.length===0,m[j.uuid]=I}})});for(const E of Object.keys(i)){const T=i[E];if(!(T.type==="group"||T.type==="repeater"||!((O=T.properties)!=null&&O.validation))&&!(E in m)){const V=_(T);T.valid=V.length===0,V.length>0&&(m[E]=V)}}const y={...s(),externalContext:e??{},screens:r,screenOrder:c,components:i,groupComponentMap:l,templateComponentMap:a,conditionsByDependency:u,conditionResults:{},validationErrors:m,validationsByDependency:d},g=yn(y);y.conditionResults=g,c.forEach(E=>{f[E]=Z(y,E)});const h=dn(i),v={...y,computedByDependency:h,calculationResults:{},screenValidity:f};xe(v);const C=o.calculations??[],b=un(C),S={...v,calculations:C},w=be(S);n({flowUUID:o.uuid,metadata:o.metadata,config:o.config??{},externalContext:e??{},screens:r,screenOrder:c,components:i,groupComponentMap:l,templateComponentMap:a,conditionsByDependency:u,conditionResults:g,screenValidity:f,validationErrors:m,validationsByDependency:d,calculations:C,calculationResults:w,calculationsByDependency:b,computedByDependency:h})},updateContext:o=>{const t=s(),e=t.externalContext,r=[];for(const a of Object.keys(o))e[a]!==o[a]&&r.push(a);for(const a of Object.keys(e))!(a in o)&&!r.includes(a)&&r.push(a);if(r.length===0)return;const c={...t.conditionResults};let i=!1;const l=new Set;for(const a of r){const u=t.conditionsByDependency[a];if(!(!u||u.length===0))for(const d of u){if(l.has(d.target))continue;l.add(d.target);const m={...t,externalContext:o},f=te(m,d.config);c[d.target]!==f&&(c[d.target]=f,i=!0)}}if(i){const a={...t,conditionResults:c},u={};for(const d of t.screenOrder)u[d]=Z(a,d);n({externalContext:o,conditionResults:c,screenValidity:u})}else n({externalContext:o})},...mn(n,s),getFlowData:()=>gn(s()),getScreenByIndex:o=>{const t=s();return o<0||o>=t.screenOrder.length?null:t.screens[t.screenOrder[o]]},getScreenValidity:o=>{const t=s();return o<0||o>=t.screenOrder.length?!0:t.screenValidity[t.screenOrder[o]]??!0},getValidationErrors:o=>s().validationErrors[o]??Cn,evaluateFlowValidation:()=>{const o=s(),t={};let e=!0;return o.screenOrder.forEach(r=>{const c=Z(o,r);t[r]=c,c||(e=!1)}),n({screenValidity:t}),e},getMaxScreenCount:()=>s().screenOrder.length-1,getCalculationResult:o=>s().calculationResults[o]??null})),xn=({uuid:n})=>{var i;const s=L(l=>l.components[n]),o=L(l=>l.conditionResults[n]);if(!s)return null;const t=s.conditions&&"when"in s.conditions?s.conditions:void 0;if(M.isHiddenByCondition(t,o))return null;const e=rn(s.type);if(!e)return console.warn(`FBRE: No renderer for component type "${s.type}"`),null;const r=(i=s.properties)==null?void 0:i.width,c=r&&r!=="full"?`fbre-component fbre-width-${r}`:"fbre-component";return p.jsx("div",{className:c,"data-uuid":n,children:p.jsx(e,{uuid:n})})},Se=({screenIndex:n})=>{const s=L(o=>{const t=o.screenOrder[n];return t?o.screens[t]:null});return s?p.jsx("div",{className:"fbre-screen",children:s.components.map(o=>p.jsx(xn,{uuid:o.uuid},o.uuid))}):null},Ue=({size:n=14})=>p.jsx("svg",{width:n,height:n,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",children:p.jsx("polyline",{points:"4,12 9.5,17.5 20,7"})}),Sn=({size:n=12})=>p.jsx("svg",{width:n,height:n,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M7 10l5 5 5-5z"})}),wn=({size:n=16})=>p.jsx("svg",{width:n,height:n,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"})}),jn=({size:n=16})=>p.jsx("svg",{width:n,height:n,viewBox:"0 0 24 24",fill:"currentColor",children:p.jsx("path",{d:"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"})}),Ae=({position:n,screenIndex:s,maxScreenIndex:o,allowInvalidTransition:t,screenValidity:e,summaryEnabled:r,summaryActive:c,atMaxScreen:i,nextButtonLabel:l,backButtonLabel:a,completionLoading:u,nextScreen:d,prevScreen:m,onFlowComplete:f})=>{const y=u??!1;if(n==="back"){const b=s===0&&!c||y;return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--secondary",disabled:b,onClick:m,children:[p.jsx(wn,{}),a||"Back"]})}const g=r?i&&c:i,h=r?i?c?"Done":"Review":"Next":i?"Done":"Next";if(o===0){const b=r?c?"Complete":"Review":"Complete",S=y||!e;return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--primary",disabled:S,onClick:()=>{r&&!c?d():f()},children:[l||b,y?p.jsx("span",{className:"fbre-btn__spinner"}):p.jsx(Ue,{size:16})]})}const v=y||!t&&!e||h==="Done"&&!e,C=()=>{g?f():d()};return p.jsxs("button",{type:"button",className:"fbre-btn fbre-btn--primary",disabled:v,onClick:C,children:[l||h,y?p.jsx("span",{className:"fbre-btn__spinner"}):g?p.jsx(Ue,{size:16}):p.jsx(jn,{})]})},Rn=({current:n,total:s,stepperStyle:o})=>{if(s<=1)return null;const t=s>1?n/(s-1)*100:100,r=!!o&&o!=="default"||s<=6;return p.jsxs(p.Fragment,{children:[r&&p.jsx("div",{className:"fbre-stepper fbre-stepper--dots",children:Array.from({length:s},(c,i)=>p.jsx("div",{className:`fbre-stepper__dot${i===n?" active":i<n?" completed":""}`},i))}),r&&p.jsxs("div",{className:"fbre-stepper fbre-stepper--bar-alt",children:[p.jsx("div",{className:"fbre-stepper__track",children:p.jsx("div",{className:"fbre-stepper__fill",style:{width:`${t}%`}})}),p.jsxs("span",{className:"fbre-stepper__counter",children:[n+1," of ",s]})]}),!r&&p.jsx("div",{className:"fbre-stepper fbre-stepper--bar",children:p.jsx("div",{className:"fbre-stepper__fill",style:{width:`${t}%`}})})]})},En=({current:n,total:s})=>s<=1?null:p.jsxs("div",{className:"fbre-stepper-text",children:["Step ",n+1," of ",s]}),Nn=n=>{const s=n.controlsLayout==="stacked",o=n.showStepper!==!1&&(n.stepperStyle??"default")!=="text";return p.jsxs("div",{className:s?"fbre-controls fbre-controls--stacked":"fbre-controls",children:[n.screenIndex>0||n.summaryActive?p.jsx(Ae,{position:"back",...n}):p.jsx("div",{}),o&&n.maxScreenIndex>0?p.jsx(Rn,{current:n.screenIndex,total:n.maxScreenIndex+1,stepperStyle:n.stepperStyle}):p.jsx("div",{}),p.jsx(Ae,{position:"next",...n})]})},Je=({component:n})=>{const{uuid:s,label:o,type:t,value:e,components:r}=n,c=()=>{if(e!=null)switch(t){case"fileUpload":return e.name;case"inputText":case"inputTextArea":case"inputNumber":case"dropDown":case"radio":case"rating":case"slider":return e.toString();case"computed":return typeof e=="number"?e.toString():"—";case"toggleSwitch":return e?"Yes":"No";case"checkbox":case"dropDownMulti":return e.length===0?"No options selected":e.join(", ")}return"No response given"};if(t==="signature"&&e){const a=typeof e=="string"&&e.startsWith("data:");return p.jsxs("div",{className:"fbre-summary__item",children:[p.jsx("div",{className:"fbre-summary__item-label",children:o}),p.jsx("div",{className:"fbre-summary__item-value",children:a?p.jsx("img",{src:e,alt:"Signature",style:{maxWidth:"100%",maxHeight:80}}):p.jsx("span",{style:{fontFamily:'"Brush Script MT", "Segoe Script", "Dancing Script", cursive',fontSize:24},children:e})})]})}const i=()=>r?r.map((a,u)=>p.jsx("div",{children:a.map(d=>p.jsx(Je,{component:d},d.uuid))},`${s}-${u}`)):null,l=e==null||typeof e=="string"&&e.length===0||Array.isArray(e)&&e.length===0;return p.jsxs("div",{className:`fbre-summary__item${t==="group"||t==="repeater"?" fbre-summary__item--group":""}`,children:[p.jsx("div",{className:"fbre-summary__item-label",children:o}),t!=="group"&&t!=="repeater"&&p.jsx("div",{className:`fbre-summary__item-value${l?" fbre-summary__item-value--empty":""}`,children:c()}),(t==="group"||t==="repeater")&&i()]})},Dn=/\$\{([^}]+)\}/g,In=(n,s)=>n.replace(Dn,(o,t)=>{const e=s.resolveCalculation(t);if(e!==null)return e;const r=s.resolveComponentValue(t);return r!==null?r:""});function kn(){const n=L(e=>e.components),s=L(e=>e.templateComponentMap),o=L(e=>e.calculations),t=L(e=>e.calculationResults);return x.useMemo(()=>{const e=new Map(o.map(c=>[c.uuid,c])),r=c=>{var d,m;const i=n[c];if(i)return i;const l=s[c];if(!l)return;const a=l.split(":"),u=n[a[0]];return(m=(d=u==null?void 0:u.addedComponents)==null?void 0:d[parseInt(a[1])])==null?void 0:m[parseInt(a[2])]};return{resolveComponentValue:c=>{const i=r(c);if(!i)return null;const l=i.value;return l==null||l===""?null:Array.isArray(l)?l.join(", "):String(l)},resolveCalculation:c=>{const i=e.get(c);if(!i)return null;const l=t[c]??null;return M.formatCalculationResult(l,i.format,i.decimalPlaces,i.currencySymbol)}}},[n,s,o,t])}const Bn=({screen:n,showToggle:s})=>{const o=kn(),[t,e]=x.useState(!0);return p.jsxs("div",{children:[p.jsxs("div",{className:"fbre-summary__screen-header",onClick:()=>s&&e(!t),children:[n.label&&p.jsx("span",{className:"fbre-summary__screen-label",children:In(n.label,o)}),s&&p.jsx("span",{className:`fbre-summary__screen-toggle${t?"":" collapsed"}`,children:p.jsx(Sn,{size:20})})]}),p.jsx("div",{className:`fbre-summary__screen-body${t?"":" collapsed"}`,children:n.components.map(r=>p.jsx(Je,{component:r},r.uuid))})]})},Fe=()=>{const n=ie(),s=L(e=>e.components),o=x.useMemo(()=>n.getState().getFlowData(),[n,s]),t=o.screens.length;return p.jsxs("div",{className:"fbre-summary",children:[p.jsx("h2",{className:"fbre-summary__title",children:"Review"}),p.jsx("div",{className:"fbre-summary__body",children:o.screens.map(e=>p.jsx(Bn,{screen:e,showToggle:t>1},e.uuid))})]})},ze=(n,s,o)=>s?!M.isHiddenByCondition(s,o[n]):!0,me=(n,s)=>{const o=n.screenOrder.length-1;let t=s+1;for(;t<=o;){const e=n.screenOrder[t],r=n.screens[e],c=r!=null&&r.conditions&&"when"in r.conditions?r.conditions:void 0;if(ze(e,c,n.conditionResults))return t;t++}return null},On=(n,s)=>{let o=s-1;for(;o>=0;){const t=n.screenOrder[o],e=n.screens[t],r=e!=null&&e.conditions&&"when"in e.conditions?e.conditions:void 0;if(ze(t,r,n.conditionResults))return o;o--}return 0},we=new Set(["radio","yesNo","cardSelect","dropDown"]),Tn=500;function We(n,s,o){const t=ie(),e=x.useRef(null),r=x.useRef(""),c=x.useRef(0);x.useEffect(()=>{if(!n)return;const i=t.getState(),l=Le(i,s);l&&(r.current=Pe(i.components,l)),c.current=i.selectionTick},[n,s,t]),x.useEffect(()=>{if(!n)return;const i=t.subscribe(l=>{const a=Le(l,s);if(!a)return;const u=l.selectionTick,d=u!==c.current;d&&(c.current=u);const m=Pe(l.components,a);if(m===r.current&&!d)return;const f=r.current;if(r.current=m,!(m!==f?Vn(l.components,a,f):d&&_n(l.components,a))||me(l,s)===null)return;const h=l.screenOrder[s];h&&l.screenValidity[h]===!1||(e.current&&clearTimeout(e.current),e.current=setTimeout(()=>{e.current=null,o()},Tn))});return()=>{i(),e.current&&(clearTimeout(e.current),e.current=null)}},[n,s,t,o]),x.useEffect(()=>()=>{e.current&&(clearTimeout(e.current),e.current=null)},[s])}function Le(n,s){const o=n.screenOrder[s];if(!o)return null;const t=n.screens[o];return t?t.components.map(e=>e.uuid):null}function Pe(n,s){const o=[];for(const t of s){const e=n[t];!e||!we.has(e.type)||o.push(`${t}=${JSON.stringify(e.value??null)}`)}return o.join("|")}function Vn(n,s,o){const t=new Map;for(const e of o.split("|")){const r=e.indexOf("=");r>0&&t.set(e.slice(0,r),e.slice(r+1))}for(const e of s){const r=n[e];if(!r||!we.has(r.type))continue;const c=JSON.stringify(r.value??null),i=t.get(e)??"null";if(c!==i&&c!=="null")return!0}return!1}function _n(n,s){for(const o of s){const t=n[o];if(!(!t||!we.has(t.type))&&t.value!==null&&t.value!==void 0)return!0}return!1}function Xe(n,s,o){const t=ie();x.useEffect(()=>{if(!n)return;const e=r=>{if(r.key!=="Enter")return;const c=r.target;if(c.tagName!=="INPUT"||!c.closest(".fbre-container"))return;const i=t.getState();if(me(i,s)===null)return;const a=i.screenOrder[s];a&&i.screenValidity[a]===!1||(r.preventDefault(),o())};return document.addEventListener("keydown",e),()=>document.removeEventListener("keydown",e)},[n,s,o,t])}const de=new Map,Mn=(n,s)=>{de.has(n)||de.set(n,new Set),de.get(n).add(s)},Un=(n,s)=>{var o;(o=de.get(n))==null||o.delete(s)},Ke=x.createContext(null);function An(){return x.useContext(Ke)}class re extends Error{constructor(s,o,t,e){super(s),this.name="ApiError",this.status=o,this.code=t,this.validationErrors=e}}class Qe extends re{constructor(s,o){super(`Request timed out after ${o}ms: ${s}`,0,"TIMEOUT"),this.name="TimeoutError"}}const Fn=new Set([502,503,504]),ae=2,Ln=[500,1500];function Pn(n){return new Promise(s=>setTimeout(s,n))}async function oe(n,s,o={}){const e=`${n.apiEndpoint||""}${s}`,r=n.timeout??3e4,c={...o.body?{"Content-Type":"application/json"}:{},...o.headers};n.apiKey&&(c.Authorization=`Bearer ${n.apiKey}`);let i;for(let l=0;l<=ae;l++){l>0&&await Pn(Ln[l-1]);try{const a=new AbortController,u=setTimeout(()=>a.abort(),r);let d;try{d=await fetch(e,{...o,headers:c,signal:a.signal})}finally{clearTimeout(u)}if(!d.ok){const m=await d.json().catch(()=>({})),f=new re(m.error||`HTTP ${d.status}`,d.status,m.code,m.validationErrors);if(l<ae&&Fn.has(d.status)){i=f;continue}throw f}return d.json()}catch(a){if(a instanceof re)throw a;if(a instanceof DOMException&&a.name==="AbortError"){const u=new Qe(e,r);if(l<ae){i=u;continue}throw u}if(a instanceof TypeError&&l<ae){i=a;continue}throw a}}throw i}async function $n(n,s){return oe(n,`/api/v1/public/flows/${s}`)}async function qn(n,s,o){const t={flowId:s};return o&&Object.keys(o).length>0&&(t.context=o),oe(n,"/api/v1/public/sessions",{method:"POST",body:JSON.stringify(t)})}async function Hn(n,s,o){return oe(n,`/api/v1/public/sessions/${s}/next`,{method:"POST",body:JSON.stringify({data:o})})}async function Yn(n,s,o){return oe(n,`/api/v1/public/sessions/${s}/prev`,{method:"POST",body:JSON.stringify({data:o})})}async function Gn(n,s,o){return oe(n,`/api/v1/public/sessions/${s}/complete`,{method:"POST",body:JSON.stringify({data:o})})}async function Jn(n,s){return oe(n,`/api/v1/public/sessions/${s}`)}const zn={clean:"dots",outlined:"dots","refined-clean":"pill","airy-clean":"glow","soft-outlined":"glow","defined-outlined":"bar","centered-minimal":"dots","stacked-cards":"dots","soft-float":"glow","bold-statement":"bar"},Wn=({screenIndex:n,onFlowComplete:s,onScreenChange:o})=>{var je,Re,Ee,Ne,De,Ie,ke,Be,Oe,Te,Ve;const t=L(D=>D.config),e=L(D=>D.screenOrder.length),r=L(D=>Object.keys(D.components).length>0),c=ie(),i=Math.max(0,Math.min(n,e-1)),[l,a]=x.useState(i),[u,d]=x.useState(!!(t!=null&&t.summary&&n>=e)),m=((je=t==null?void 0:t.navigation)==null?void 0:je.transition)??"none",f=m!=="none",[y,g]=x.useState(null),[h,v]=x.useState(!1),[C,b]=x.useState("forward"),[S,w]=x.useState(!1),O=x.useRef(null);x.useEffect(()=>{const D=Math.max(0,Math.min(n,e-1));a(D)},[n]);const E=L(D=>D.screenOrder[l]),T=L(D=>E?D.screenValidity[E]??!0:!0),V=L(D=>{var F;return E?(F=D.screens[E])==null?void 0:F.nextButtonLabel:void 0}),j=L(D=>{var F;return E?(F=D.screens[E])==null?void 0:F.backButtonLabel:void 0}),I=x.useCallback(D=>{o&&o(D,c.getState().getFlowData())},[o,c]),k=x.useCallback(D=>{g(l),v(u),b(D),w(!0),O.current&&(O.current.scrollTop=0)},[l,u]),J=x.useCallback(()=>{w(!1),g(null)},[]),U=x.useCallback(()=>{if(!S)if(u)f&&k("back"),d(!1),I(l);else{const D=c.getState(),F=On(D,l);F!==l&&(f&&k("back"),a(F),I(F))}},[S,u,l,f,k,I,c]),A=x.useCallback(()=>{if(S)return;const D=c.getState(),F=me(D,l);F!==null?(f&&k("forward"),a(F),I(F)):t!=null&&t.summary&&(f&&k("forward"),d(!0),I(l+1))},[S,l,t,f,k,I,c]),[G,P]=x.useState(!1),[B,q]=x.useState(null),H=x.useCallback(()=>{q(null);const D=s(c.getState().getFlowData());D&&typeof D.then=="function"&&(P(!0),D.then(()=>{P(!1)}).catch(F=>{P(!1),q(F instanceof Error?F.message:String(F))}))},[s,c]),X=L(D=>me(D,l)===null),R=((Re=t==null?void 0:t.theme)==null?void 0:Re.darkMode)??!1,N=((t==null?void 0:t.mode)??"standard")==="conversational";We(N&&!u,l,A),Xe(N&&!u,l,A);const $=((Ee=t==null?void 0:t.theme)==null?void 0:Ee.style)??"clean",ee=((Ne=t==null?void 0:t.controls)==null?void 0:Ne.stepperStyle)??"default",ne=ee==="default"?zn[$]??"dots":ee,en=((De=t==null?void 0:t.controls)==null?void 0:De.showStepper)!==!1&&ne==="text",he={};(Ie=t==null?void 0:t.theme)!=null&&Ie.color&&(he["--fbre-theme-color"]=t.theme.color);const nn=()=>h?p.jsx(Fe,{}):y!==null?p.jsx(Se,{screenIndex:y}):null,tn=S?"fbre-screen-wrapper fbre-screen-wrapper--transitioning":"fbre-screen-wrapper";return p.jsxs("div",{className:"fbre-container","data-style":$,"data-mode":R?"dark":"light","data-form-mode":N?"conversational":void 0,"data-stepper":ne!=="text"?ne:void 0,"data-transition":f?m:void 0,style:Object.keys(he).length>0?he:void 0,children:[en&&p.jsx(En,{current:l,total:e}),p.jsxs("div",{className:tn,ref:O,children:[S&&p.jsx("div",{className:"fbre-screen--exiting","data-direction":C,onAnimationEnd:J,children:nn()}),p.jsxs("div",{className:S?"fbre-screen--entering":void 0,"data-direction":S?C:void 0,children:[!u&&p.jsx(Se,{screenIndex:l}),u&&p.jsx(Fe,{})]})]}),B&&p.jsx("div",{className:"fbre-completion-error",children:B}),((ke=t==null?void 0:t.controls)==null?void 0:ke.show)!==!1&&r&&p.jsx(Nn,{screenIndex:l,maxScreenIndex:e-1,allowInvalidTransition:(Be=t==null?void 0:t.navigation)==null?void 0:Be.allowInvalidTransition,screenValidity:T,summaryEnabled:(t==null?void 0:t.summary)??!1,summaryActive:u,atMaxScreen:X,nextButtonLabel:V,backButtonLabel:j,completionLoading:G,showStepper:(Oe=t==null?void 0:t.controls)==null?void 0:Oe.showStepper,stepperStyle:(Te=t==null?void 0:t.controls)==null?void 0:Te.stepperStyle,controlsLayout:(Ve=t==null?void 0:t.controls)==null?void 0:Ve.layout,nextScreen:A,prevScreen:U,onFlowComplete:H})]})},Xn=[],Kn=()=>z.createStore((n,s)=>({flowUUID:"",metadata:{},config:{},externalContext:{},screenOrder:[],screens:{},screenValidity:{},components:{},groupComponentMap:{},templateComponentMap:{},conditionsByDependency:{},conditionResults:{},validationErrors:{},validationsByDependency:{},calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},selectionTick:0,sessionId:"",navigation:{canGoBack:!1,canGoForward:!1,screenNumber:1,totalScreens:1,progress:0,isLastScreen:!0},loading:!1,error:"",clearError:()=>{n({error:""})},loadFlow:()=>{},updateContext:o=>{n({externalContext:o})},updateComponentValue:(o,t)=>{var m,f;const e=s();if(o.includes(":")){const y=o.split(":"),g=y[0],h=parseInt(y[1]),v=parseInt(y[2]),C=e.components[g];if(!((f=(m=C==null?void 0:C.addedComponents)==null?void 0:m[h])!=null&&f[v]))return;C.addedComponents[h][v]={...C.addedComponents[h][v],value:t};const b=e.groupComponentMap[o]??o,S=e.conditionsByDependency[b];let w=e.conditionResults;if(S&&S.length>0){w={...e.conditionResults};for(const j of S)w[j.target]=te({...e},j.config)}const O={components:e.components,groupComponentMap:e.groupComponentMap},E=_(C.addedComponents[h][v],O);C.addedComponents[h][v].valid=E.length===0;const T={...e.validationErrors,[o]:E},V=e.validationsByDependency[o];if(V)for(const j of V){const I=$e(e,j.target);if(I){const k=_(I,O);I.valid=k.length===0,T[j.target]=k}}n({components:{...e.components},conditionResults:w,validationErrors:T});return}const r=e.components[o];if(!r)return;if(r.type==="group"||r.type==="repeater"){n({components:{...e.components}});return}if(t===r.value){n({selectionTick:(s().selectionTick??0)+1});return}r.value=t;const c=e.conditionsByDependency[o];let i=e.conditionResults;if(c&&c.length>0){i={...e.conditionResults};for(const y of c)i[y.target]=te({...e},y.config)}const l={components:e.components,groupComponentMap:e.groupComponentMap},a=_(r,l);r.valid=a.length===0;const u={...e.validationErrors,[o]:a},d=e.validationsByDependency[o];if(d)for(const y of d){const g=$e(e,y.target);if(g){const h=_(g,l);g.valid=h.length===0,u[y.target]=h}}n({components:{...e.components},conditionResults:i,validationErrors:u})},addGroupIteration:o=>{const t=s(),e=t.components[o];if(!e||e.type!=="group"||!e.components)return;e.addedComponents||(e.addedComponents=[]);const r=e.components.map((c,i)=>{const l=W(c);l.uuid=[o,e.addedComponents.length,i].join(":"),t.groupComponentMap[l.uuid]=c.uuid;const a=_(l);return l.valid=a.length===0,l});return e.addedComponents.push(r),n({components:{...t.components},groupComponentMap:{...t.groupComponentMap}}),e.addedComponents},addRepeaterIteration:o=>{const t=s(),e=t.components[o];if(!e||e.type!=="repeater"||!e.components)return;e.addedComponents||(e.addedComponents=[]);const r=e.components.map((c,i)=>{const l=W(c);l.uuid=[o,e.addedComponents.length,i].join(":");const a=_(l);return l.valid=a.length===0,l});return e.addedComponents.push(r),n({components:{...t.components}}),e.addedComponents},removeRepeaterIteration:(o,t)=>{const e=s(),r=e.components[o];if(!(!r||r.type!=="repeater"||!r.addedComponents||r.addedComponents.length<=1)){r.addedComponents.splice(t,1);for(let c=t;c<r.addedComponents.length;c++)r.addedComponents[c].forEach((i,l)=>{i.uuid=[o,c,l].join(":")});n({components:{...e.components}})}},getFlowData:()=>({uuid:"",metadata:{},screens:[]}),getScreenByIndex:o=>{const t=s();if(o!==0)return null;const e=t.screenOrder[0];return e?t.screens[e]:null},getScreenValidity:o=>{const t=s();if(o!==0)return!0;const e=t.screenOrder[0];return e?t.screenValidity[e]??!0:!0},getValidationErrors:o=>s().validationErrors[o]??Xn,evaluateFlowValidation:()=>{const o=s(),t=o.screenOrder[0];if(!t)return!0;const e=o.screens[t];if(!e)return!0;let r=!0;const c={components:o.components,groupComponentMap:o.groupComponentMap},i={...o.validationErrors};for(const a of e.components){const u=o.components[a.uuid];if(!u)continue;if((u.type==="group"||u.type==="repeater")&&u.addedComponents){for(const f of u.addedComponents)for(const y of f){const g=y.conditions&&"when"in y.conditions?y.conditions:void 0;if(g){const v=o.conditionResults[y.uuid]??!1;if(g.action==="show"&&!v||g.action==="hide"&&v)continue}const h=_(y,c);y.valid=h.length===0,i[y.uuid]=h,h.length>0&&(r=!1)}continue}const d=u.conditions&&"when"in u.conditions?u.conditions:void 0;if(d){const f=o.conditionResults[u.uuid]??!1;if(d.action==="show"&&!f||d.action==="hide"&&f)continue}const m=_(u,c);u.valid=m.length===0,i[u.uuid]=m,m.length>0&&(r=!1)}const l={...o.screenValidity,[t]:r};return n({components:{...o.components},validationErrors:i,screenValidity:l}),r},getMaxScreenCount:()=>0,getCalculationResult:o=>s().calculationResults[o]??null})),le=(n,s)=>{var y,g;const o={},t={},e={},r={},c={},i={},l=s.screen.uuid;for(const h of s.screen.components){const v=W(h);if(o[v.uuid]=v,(v.type==="group"||v.type==="repeater")&&h.components){v.addedComponents||(v.addedComponents=[]);const C=h.components.map((b,S)=>{var O;const w=W(b);if(w.uuid=[v.uuid,0,S].join(":"),v.type==="group"&&(t[w.uuid]=b.uuid),e[b.uuid]=w.uuid,(O=w.properties)!=null&&O.validation){const E=w.properties.validation;E.rules&&E.rules.length>0&&se(c,w.uuid,E)}return w});v.addedComponents.push(C)}if((y=v.properties)!=null&&y.validation){const C=v.properties.validation;C.rules&&C.rules.length>0&&se(c,v.uuid,C)}}for(const{targetUUID:h,config:v}of s.conditions)Q(r,h,v,h===l?"screen":"component");if(s.previousData)for(const h of s.previousData){const v=o[h.uuid];v&&(v.value=h.value)}const a={components:o};for(const h of Object.keys(o)){const v=o[h];if(v.type==="group"||v.type==="repeater"){if(v.addedComponents)for(const b of v.addedComponents)for(const S of b){const w=_(S,a);S.valid=w.length===0,w.length>0&&(i[S.uuid]=w)}continue}if(!((g=v.properties)!=null&&g.validation))continue;const C=_(v,a);v.valid=C.length===0,C.length>0&&(i[h]=C)}const u={},d={components:o},m=new Set;for(const h of Object.values(r))for(const v of h)m.has(v.target)||(m.add(v.target),u[v.target]=te(d,v.config));let f=!0;for(const h of s.screen.components){const v=o[h.uuid];if(v)if((v.type==="group"||v.type==="repeater")&&v.addedComponents)for(const C of v.addedComponents){for(const b of C)if(!b.valid){f=!1;break}if(!f)break}else v.valid===!1&&(f=!1)}n.setState({screenOrder:[l],screens:{[l]:{uuid:l,label:s.screen.label,components:s.screen.components,nextButtonLabel:s.screen.nextButtonLabel,backButtonLabel:s.screen.backButtonLabel}},screenValidity:{[l]:f},components:o,groupComponentMap:t,templateComponentMap:e,conditionsByDependency:r,conditionResults:u,validationErrors:i,validationsByDependency:c,calculations:[],calculationResults:{},calculationsByDependency:{},computedByDependency:{},sessionId:s.sessionId,navigation:s.navigation,loading:!1,error:"",externalContext:s.context??{},config:s.config??{}})},Ce=n=>{var r;const s=n.getState(),o=s.screenOrder[0];if(!o)return[];const t=s.screens[o];if(!t)return[];const e=[];for(const c of t.components){const i=s.components[c.uuid];if(!i||pe(i.type))continue;const l={uuid:i.uuid,type:i.type,value:i.value};(r=i.properties)!=null&&r.label&&(l.label=i.properties.label),(i.type==="group"||i.type==="repeater")&&i.addedComponents&&(l.components=i.addedComponents.map(a=>a.filter(u=>!pe(u.type)).map(u=>({uuid:u.uuid,type:u.type,value:u.value})))),e.push(l)}return e},$e=(n,s)=>Ge(n.components,s),Qn=6e4,Zn=({sessionEndpoint:n,flowId:s,apiKey:o,mode:t,theme:e,context:r,onFlowComplete:c,onScreenChange:i})=>{const l=x.useRef(null);l.current||(l.current=Kn());const a=l.current,u=z.useStore(a,R=>R.loading),d=z.useStore(a,R=>R.error),m=z.useStore(a,R=>R.navigation),f=z.useStore(a,R=>R.config),y=z.useStore(a,R=>{var Y;return(Y=R.config)==null?void 0:Y.mode}),g=z.useStore(a,R=>R.screenOrder),h=z.useStore(a,R=>R.screens),[v,C]=x.useState(""),b=x.useRef(null),S=x.useRef(Date.now()),w=x.useMemo(()=>({apiEndpoint:n,apiKey:o}),[n,o]),O=x.useCallback(R=>{C(R),b.current&&clearTimeout(b.current),b.current=setTimeout(()=>C(""),8e3)},[]),E=x.useCallback(()=>{C(""),b.current&&(clearTimeout(b.current),b.current=null)},[]),T=x.useCallback(async()=>{a.setState({loading:!0,error:""});try{const R=await qn(w,s,r);le(a,R)}catch(R){a.setState({loading:!1,error:R.message})}},[s,n,o,r]);x.useEffect(()=>{T()},[T]),x.useEffect(()=>{const R=async()=>{if(document.visibilityState!=="visible")return;const Y=a.getState();if(!(!Y.sessionId||Date.now()-S.current<Qn))try{const $=await Jn(w,Y.sessionId);le(a,$)}catch($){$ instanceof re&&$.status===404&&a.setState({loading:!1,error:"Session expired. Please start over.",screenOrder:[],screens:{}})}};return document.addEventListener("visibilitychange",R),()=>document.removeEventListener("visibilitychange",R)},[n,o]);const V=x.useCallback(async()=>{const R=a.getState();if(!(R.loading||!R.evaluateFlowValidation())){a.setState({loading:!0}),a.getState().clearError(),E();try{const N=Ce(a),$=await Hn(w,R.sessionId,N);le(a,$),S.current=Date.now(),i==null||i($.navigation.screenNumber)}catch(N){a.setState({loading:!1}),O(N.message)}}},[w,a,i,O,E]),j=x.useCallback(async()=>{const R=a.getState();if(!R.loading){a.setState({loading:!0}),a.getState().clearError(),E();try{const Y=Ce(a),N=await Yn(w,R.sessionId,Y);le(a,N),S.current=Date.now(),i==null||i(N.navigation.screenNumber)}catch(Y){a.setState({loading:!1}),O(Y.message)}}},[w,a,i,O,E]),I=x.useCallback(async()=>{const R=a.getState();if(!(R.loading||!R.evaluateFlowValidation())){a.setState({loading:!0}),a.getState().clearError(),E();try{const N=Ce(a),$=await Gn(w,R.sessionId,N);S.current=Date.now(),c($)}catch(N){a.setState({loading:!1}),O(N.message)}}},[w,a,c,O,E]),k={...f==null?void 0:f.theme,...e},U=(t??y??"standard")==="conversational",A=U&&!m.isLastScreen;We(A,0,V),Xe(A,0,V);const G=k.darkMode??!1,P={};k.color&&(P["--fbre-theme-color"]=k.color);const B=g[0],q=B?h[B]:void 0,H=q==null?void 0:q.nextButtonLabel,X=q==null?void 0:q.backButtonLabel;return u&&g.length===0?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-loading",children:[p.jsx("div",{className:"fbre-spinner"}),p.jsx("p",{children:"Loading form..."})]})}):d&&g.length===0?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-error",children:[p.jsx("p",{children:"Failed to load form"}),p.jsx("p",{className:"fbre-remote-error__detail",children:d}),p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--next",onClick:T,style:{marginTop:12},children:"Retry"})]})}):g.length===0?null:p.jsx(ve.Provider,{value:a,children:p.jsxs("div",{className:"fbre-container","data-style":k.style??"clean","data-mode":G?"dark":"light","data-form-mode":U?"conversational":void 0,style:Object.keys(P).length>0?P:void 0,children:[p.jsx("div",{className:"fbre-screen-wrapper",children:p.jsx(Se,{screenIndex:0})}),p.jsx(et,{navigation:m,loading:u,onPrev:j,onNext:V,onComplete:I,nextButtonLabel:H,backButtonLabel:X}),p.jsxs("div",{className:`fbre-toast${v?" visible":""}`,children:[p.jsx("span",{children:v}),p.jsx("button",{type:"button",className:"fbre-toast__close",onClick:E,"aria-label":"Dismiss",children:"×"})]})]})})},et=({navigation:n,loading:s,onPrev:o,onNext:t,onComplete:e,nextButtonLabel:r,backButtonLabel:c})=>p.jsxs("div",{className:"fbre-controls",children:[p.jsx("div",{className:"fbre-controls__left",children:n.canGoBack&&p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--back",onClick:o,disabled:s,children:c||"Back"})}),p.jsx("div",{className:"fbre-controls__center",children:p.jsxs("span",{className:"fbre-controls__progress",children:[n.screenNumber," / ",n.totalScreens]})}),p.jsx("div",{className:"fbre-controls__right",children:n.isLastScreen?p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--complete",onClick:e,disabled:s,children:s?"Submitting...":r||"Complete"}):p.jsx("button",{type:"button",className:"fbre-btn fbre-btn--next",onClick:t,disabled:s,children:s?"Loading...":r||"Next"})})]});function nt(n,s,o,t,e){return!s&&!o&&!t&&!e?n:{...n,...e&&{mode:e},...s&&{theme:{...n.theme,...s}},...o&&{navigation:{...n.navigation,...o}},...t&&{controls:{...n.controls,...t}}}}const Ze=({flow:n,data:s,mode:o,theme:t,navigation:e,controls:r,screenIndex:c,context:i,storeRef:l,onFlowComplete:a,onScreenChange:u,onScreenValidationChange:d,apiCtx:m})=>{const f=x.useRef(null);f.current||(f.current=bn(),f.current.getState().loadFlow(n,s,i)),l&&(l.current=f.current),x.useEffect(()=>{f.current&&f.current.getState().loadFlow(n,s,i)},[n.uuid]),x.useEffect(()=>{if(f.current){const h=f.current.getState(),v=nt(h.config,t,e,r,o);v!==h.config&&f.current.setState({config:v})}},[o,t,e,r]);const y=i?JSON.stringify(i):"";x.useEffect(()=>{f.current&&i&&f.current.getState().updateContext(i)},[y]);const g=p.jsx(ve.Provider,{value:f.current,children:p.jsx(Wn,{screenIndex:c??0,onFlowComplete:a,onScreenChange:u,onScreenValidationChange:d})});return m?p.jsx(Ke.Provider,{value:m,children:g}):g},tt=({flowId:n,apiEndpoint:s,apiKey:o,data:t,mode:e,theme:r,navigation:c,controls:i,screenIndex:l,context:a,storeRef:u,onFlowComplete:d,onScreenChange:m,onScreenValidationChange:f})=>{const[y,g]=x.useState(null),[h,v]=x.useState(""),[C,b]=x.useState(null);return x.useEffect(()=>{g(null),v("");const S={apiEndpoint:s,apiKey:o};$n(S,n).then(w=>{g(w.data),b({config:S,flowId:w.id,flowVersionId:w.versionId,tenantId:w.tenantId})}).catch(w=>v(w.message))},[n,s,o]),h?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-error",children:[p.jsx("p",{children:"Failed to load form"}),p.jsx("p",{className:"fbre-remote-error__detail",children:h})]})}):!y||!C?p.jsx("div",{className:"fbre-container",children:p.jsxs("div",{className:"fbre-remote-loading",children:[p.jsx("div",{className:"fbre-spinner"}),p.jsx("p",{children:"Loading form..."})]})}):p.jsx(Ze,{flow:y,data:t,mode:e,theme:r,navigation:c,controls:i,screenIndex:l,context:a,storeRef:u,onFlowComplete:d,onScreenChange:m,onScreenValidationChange:f,apiCtx:C})},ot=n=>"sessionEndpoint"in n?p.jsx(Zn,{...n}):"flowId"in n&&n.flowId?p.jsx(tt,{...n}):p.jsx(Ze,{...n});exports.ApiError=re;exports.FBRE=ot;exports.TimeoutError=Qe;exports.addFBREEventListener=Mn;exports.removeFBREEventListener=Un;exports.useFBREApi=An;exports.useFBREStore=L;exports.useFBREStoreApi=ie;
|