@sonata-innovations/fiber-fbre 2.0.0

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.
@@ -0,0 +1,2113 @@
1
+ import { jsx as m, jsxs as k } from "react/jsx-runtime";
2
+ import { createContext as Je, useContext as xe, useMemo as Ee, useState as P, useRef as ce, useEffect as K, useCallback as z } from "react";
3
+ import { useStore as W, createStore as Ye } from "zustand";
4
+ import { normalizeOptions as tn } from "@sonata-innovations/fiber-types";
5
+ import { validateValue as on, isHiddenByCondition as Q, extractFormulaReferences as Ie, evaluateFormula as ne, evaluateConditionConfig as rn, formatCalculationResult as sn } from "@sonata-innovations/fiber-shared";
6
+ const cn = /* @__PURE__ */ new Map(), an = (e) => cn.get(e), ln = /* @__PURE__ */ new Set([
7
+ "header",
8
+ "text",
9
+ "divider",
10
+ "callout",
11
+ "table"
12
+ ]), me = (e) => ln.has(e), dn = (e, r) => {
13
+ var o, t, n, s;
14
+ if (r.includes(":")) {
15
+ const i = r.split(":"), a = e.components[i[0]];
16
+ 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;
17
+ }
18
+ return (s = e.components[r]) == null ? void 0 : s.value;
19
+ }, F = (e, r) => {
20
+ var t;
21
+ const o = (t = e.properties) == null ? void 0 : t.validation;
22
+ return !o || !o.rules || o.rules.length === 0 ? [] : on(
23
+ e.value,
24
+ o,
25
+ r ? (n) => dn(r, n) : void 0
26
+ );
27
+ }, un = (e) => {
28
+ var o;
29
+ const r = (o = e.properties) == null ? void 0 : o.validation;
30
+ return !r || !r.rules ? !1 : r.rules.some((t) => t.type === "required");
31
+ }, Y = (e) => {
32
+ var o, t;
33
+ const r = { ...e };
34
+ return Array.isArray((o = r.properties) == null ? void 0 : o.options) && (r.properties = {
35
+ ...r.properties,
36
+ options: tn(r.properties.options)
37
+ }), e.type === "computed" ? (r.value = null, r.valid = !0) : !me(e.type) && e.type !== "group" && e.type !== "repeater" && (r.value = null, r.valid = !un(e)), (e.type === "checkbox" || e.type === "dropDownMulti") && (r.value = []), e.type === "radio" && "options" in e.properties && e.properties.options.length > 0 && (r.value = e.properties.options[0].value), e.type === "toggleSwitch" && (r.value = !1), e.type === "colorPicker" && ((t = e.properties) != null && t.defaultValue) && (r.value = e.properties.defaultValue), e.type === "slider" && (r.value = "initValue" in e.properties ? e.properties.initValue : "min" in e.properties ? e.properties.min : 0), r;
38
+ }, ae = (e, r, o) => {
39
+ var t;
40
+ for (const n of o.rules)
41
+ if (n.type === "matchesField" && ((t = n.params) != null && t.field)) {
42
+ const s = n.params.field;
43
+ e[s] || (e[s] = []), e[s].push({
44
+ target: r,
45
+ config: o
46
+ });
47
+ }
48
+ }, Z = (e, r) => {
49
+ const o = e.screens[r];
50
+ return o ? !o.components.some((n) => {
51
+ var a;
52
+ const s = e.components[n.uuid];
53
+ if (!s) return !1;
54
+ if (s.type === "group" || s.type === "repeater")
55
+ return (a = s.addedComponents) == null ? void 0 : a.some(
56
+ (c) => c.some((d) => {
57
+ const l = d.conditions && "when" in d.conditions ? d.conditions : void 0;
58
+ return l && Q(l, e.conditionResults[d.uuid]) ? !1 : !d.valid;
59
+ })
60
+ );
61
+ const i = s.conditions && "when" in s.conditions ? s.conditions : void 0;
62
+ return Q(i, e.conditionResults[s.uuid]) ? !1 : s && "valid" in s && !s.valid;
63
+ }) : !0;
64
+ };
65
+ function pn(e) {
66
+ const r = {};
67
+ for (const o of e) {
68
+ const t = Ie(o.formula);
69
+ for (const n of t)
70
+ r[n] || (r[n] = []), r[n].includes(o.uuid) || r[n].push(o.uuid);
71
+ }
72
+ return r;
73
+ }
74
+ function qe(e) {
75
+ const r = new Map(e.map((i) => [i.uuid, i])), o = new Set(e.map((i) => i.uuid)), t = /* @__PURE__ */ new Set(), n = [];
76
+ function s(i, a) {
77
+ if (t.has(i) || a.has(i)) return;
78
+ a.add(i);
79
+ const c = r.get(i);
80
+ if (!c) return;
81
+ const d = Ie(c.formula);
82
+ for (const l of d)
83
+ o.has(l) && s(l, a);
84
+ a.delete(i), t.add(i), n.push(c);
85
+ }
86
+ for (const i of e)
87
+ s(i.uuid, /* @__PURE__ */ new Set());
88
+ return n;
89
+ }
90
+ function He(e, r) {
91
+ return {
92
+ resolveValue: (o) => {
93
+ if (o in r)
94
+ return r[o];
95
+ const t = q(e, o);
96
+ if (!t) return null;
97
+ const n = t.value;
98
+ if (n == null || n === "") return null;
99
+ const s = Number(n);
100
+ return isNaN(s) ? null : s;
101
+ },
102
+ resolveOptionMetadata: (o, t) => {
103
+ var l;
104
+ const n = q(e, o);
105
+ if (!n) return null;
106
+ const s = n.value;
107
+ if (!s) return null;
108
+ const i = (l = n.properties) == null ? void 0 : l.options;
109
+ if (!Array.isArray(i)) return null;
110
+ const a = i.find(
111
+ (u) => u.value === s || u.label === s
112
+ );
113
+ if (!(a != null && a.metadata)) return null;
114
+ const c = a.metadata[t];
115
+ if (c == null) return null;
116
+ const d = Number(c);
117
+ return isNaN(d) ? null : d;
118
+ },
119
+ resolveRepeaterValues: (o) => {
120
+ for (const t of Object.keys(e.components)) {
121
+ const n = e.components[t];
122
+ if (n.type !== "repeater" || !n.addedComponents || !n.components) continue;
123
+ const s = n.components.findIndex((a) => a.uuid === o);
124
+ if (s === -1) continue;
125
+ const i = [];
126
+ for (const a of n.addedComponents) {
127
+ const c = a[s];
128
+ if (!c) continue;
129
+ const d = c.value;
130
+ if (d == null || d === "") continue;
131
+ const l = Number(d);
132
+ isNaN(l) || i.push(l);
133
+ }
134
+ return i.length > 0 ? i : null;
135
+ }
136
+ return null;
137
+ }
138
+ };
139
+ }
140
+ function Se(e) {
141
+ const r = e.calculations ?? [];
142
+ if (r.length === 0) return {};
143
+ const o = qe(r), t = {}, n = He(e, t);
144
+ for (const s of o)
145
+ t[s.uuid] = ne(s.formula, n);
146
+ return t;
147
+ }
148
+ function be(e, r) {
149
+ const o = e.calculationsByDependency, t = e.calculations ?? [];
150
+ if (t.length === 0 || !o) return null;
151
+ const n = /* @__PURE__ */ new Set(), s = [r];
152
+ for (; s.length > 0; ) {
153
+ const l = s.shift(), u = o[l];
154
+ if (u)
155
+ for (const p of u)
156
+ n.has(p) || (n.add(p), s.push(p));
157
+ }
158
+ if (n.size === 0) return null;
159
+ const i = qe(t), a = { ...e.calculationResults }, c = He(e, a);
160
+ let d = !1;
161
+ for (const l of i)
162
+ if (n.has(l.uuid)) {
163
+ const u = ne(l.formula, c);
164
+ a[l.uuid] !== u && (a[l.uuid] = u, d = !0);
165
+ }
166
+ return d ? a : null;
167
+ }
168
+ function fn(e) {
169
+ const r = {}, o = (t) => {
170
+ var s;
171
+ if (t.type !== "computed" || !((s = t.properties) != null && s.formula)) return;
172
+ const n = Ie(t.properties.formula);
173
+ for (const i of n)
174
+ r[i] || (r[i] = []), r[i].includes(t.uuid) || r[i].push(t.uuid);
175
+ };
176
+ for (const t of Object.values(e))
177
+ if (o(t), (t.type === "repeater" || t.type === "group") && t.components)
178
+ for (const n of t.components)
179
+ o(n);
180
+ return r;
181
+ }
182
+ function We(e, r, o, t) {
183
+ var c, d;
184
+ const n = e.components[o], s = ((c = n == null ? void 0 : n.components) == null ? void 0 : c.map((l) => l.uuid)) ?? [], i = (d = n == null ? void 0 : n.addedComponents) == null ? void 0 : d[t], a = /* @__PURE__ */ new Map();
185
+ if (i)
186
+ for (let l = 0; l < i.length; l++) {
187
+ const u = s[l];
188
+ u && a.set(u, i[l]);
189
+ }
190
+ return {
191
+ resolveValue: (l) => {
192
+ if (l in r)
193
+ return r[l];
194
+ const u = a.get(l);
195
+ if (u) {
196
+ const g = u.value;
197
+ if (g == null || g === "") return null;
198
+ const h = Number(g);
199
+ return isNaN(h) ? null : h;
200
+ }
201
+ const p = q(e, l);
202
+ if (!p) return null;
203
+ const f = p.value;
204
+ if (f == null || f === "") return null;
205
+ const y = Number(f);
206
+ return isNaN(y) ? null : y;
207
+ },
208
+ resolveOptionMetadata: (l, u) => {
209
+ var w;
210
+ const f = a.get(l) ?? q(e, l);
211
+ if (!f) return null;
212
+ const y = f.value;
213
+ if (!y) return null;
214
+ const g = (w = f.properties) == null ? void 0 : w.options;
215
+ if (!Array.isArray(g)) return null;
216
+ const h = g.find(
217
+ (C) => C.value === y || C.label === y
218
+ );
219
+ if (!(h != null && h.metadata)) return null;
220
+ const v = h.metadata[u];
221
+ if (v == null) return null;
222
+ const b = Number(v);
223
+ return isNaN(b) ? null : b;
224
+ },
225
+ resolveRepeaterValues: (l) => {
226
+ for (const u of Object.keys(e.components)) {
227
+ const p = e.components[u];
228
+ if (p.type !== "repeater" || !p.addedComponents || !p.components) continue;
229
+ const f = p.components.findIndex((g) => g.uuid === l);
230
+ if (f === -1) continue;
231
+ const y = [];
232
+ for (const g of p.addedComponents) {
233
+ const h = g[f];
234
+ if (!h) continue;
235
+ const v = h.value;
236
+ if (v == null || v === "") continue;
237
+ const b = Number(v);
238
+ isNaN(b) || y.push(b);
239
+ }
240
+ return y.length > 0 ? y : null;
241
+ }
242
+ return null;
243
+ }
244
+ };
245
+ }
246
+ function ve(e, r) {
247
+ return {
248
+ resolveValue: (o) => {
249
+ if (o in r)
250
+ return r[o];
251
+ const t = q(e, o);
252
+ if (!t) return null;
253
+ const n = t.value;
254
+ if (n == null || n === "") return null;
255
+ const s = Number(n);
256
+ return isNaN(s) ? null : s;
257
+ },
258
+ resolveOptionMetadata: (o, t) => {
259
+ var l;
260
+ const n = q(e, o);
261
+ if (!n) return null;
262
+ const s = n.value;
263
+ if (!s) return null;
264
+ const i = (l = n.properties) == null ? void 0 : l.options;
265
+ if (!Array.isArray(i)) return null;
266
+ const a = i.find(
267
+ (u) => u.value === s || u.label === s
268
+ );
269
+ if (!(a != null && a.metadata)) return null;
270
+ const c = a.metadata[t];
271
+ if (c == null) return null;
272
+ const d = Number(c);
273
+ return isNaN(d) ? null : d;
274
+ },
275
+ resolveRepeaterValues: (o) => {
276
+ for (const t of Object.keys(e.components)) {
277
+ const n = e.components[t];
278
+ if (n.type !== "repeater" || !n.addedComponents || !n.components) continue;
279
+ const s = n.components.findIndex((a) => a.uuid === o);
280
+ if (s === -1) continue;
281
+ const i = [];
282
+ for (const a of n.addedComponents) {
283
+ const c = a[s];
284
+ if (!c) continue;
285
+ const d = c.value;
286
+ if (d == null || d === "") continue;
287
+ const l = Number(d);
288
+ isNaN(l) || i.push(l);
289
+ }
290
+ return i.length > 0 ? i : null;
291
+ }
292
+ return null;
293
+ }
294
+ };
295
+ }
296
+ function Re(e) {
297
+ var o, t;
298
+ const r = e.calculationResults ?? {};
299
+ for (const n of Object.values(e.components))
300
+ if (n.type === "computed" && ((o = n.properties) != null && o.formula)) {
301
+ const s = ve(e, r), i = ne(n.properties.formula, s);
302
+ n.value = i;
303
+ }
304
+ for (const n of Object.values(e.components))
305
+ if ((n.type === "repeater" || n.type === "group") && n.addedComponents && n.components)
306
+ for (let s = 0; s < n.addedComponents.length; s++) {
307
+ const i = n.addedComponents[s];
308
+ for (let a = 0; a < i.length; a++) {
309
+ const c = i[a], d = n.components[a];
310
+ if ((d == null ? void 0 : d.type) === "computed" && ((t = d.properties) != null && t.formula)) {
311
+ const l = n.type === "repeater" ? We(e, r, n.uuid, s) : ve(e, r), u = ne(d.properties.formula, l);
312
+ c.value = u;
313
+ }
314
+ }
315
+ }
316
+ }
317
+ function mn(e, r) {
318
+ var i, a;
319
+ const o = e.computedByDependency;
320
+ if (!o) return !1;
321
+ const t = o[r];
322
+ if (!t || t.length === 0) return !1;
323
+ const n = e.calculationResults ?? {};
324
+ let s = !1;
325
+ for (const c of t) {
326
+ const d = e.components[c];
327
+ if (d && d.type === "computed" && ((i = d.properties) != null && i.formula)) {
328
+ const l = ve(e, n), u = ne(d.properties.formula, l);
329
+ d.value !== u && (e.components[c] = { ...d, value: u }, s = !0);
330
+ continue;
331
+ }
332
+ for (const l of Object.values(e.components))
333
+ if ((l.type === "repeater" || l.type === "group") && l.components && l.addedComponents) {
334
+ const u = l.components.findIndex((f) => f.uuid === c);
335
+ if (u === -1) continue;
336
+ const p = l.components[u];
337
+ if ((p == null ? void 0 : p.type) !== "computed" || !((a = p.properties) != null && a.formula)) continue;
338
+ for (let f = 0; f < l.addedComponents.length; f++) {
339
+ const y = l.addedComponents[f][u];
340
+ if (!y) continue;
341
+ const g = l.type === "repeater" ? We(e, n, l.uuid, f) : ve(e, n), h = ne(p.properties.formula, g);
342
+ y.value !== h && (l.addedComponents[f][u] = { ...y, value: h }, s = !0);
343
+ }
344
+ }
345
+ }
346
+ return s;
347
+ }
348
+ const Xe = (e, r) => {
349
+ var o, t;
350
+ if (r.includes(":")) {
351
+ const n = r.split(":"), s = e[n[0]];
352
+ return (t = (o = s == null ? void 0 : s.addedComponents) == null ? void 0 : o[parseInt(n[1])]) == null ? void 0 : t[parseInt(n[2])];
353
+ }
354
+ return e[r];
355
+ }, pe = (e, r) => {
356
+ for (const o of e.screenOrder)
357
+ if (e.screens[o].components.some((n) => n.uuid === r))
358
+ return o;
359
+ return null;
360
+ }, vn = (e, r) => {
361
+ if (!r.includes(":")) return null;
362
+ const o = r.split(":");
363
+ if (o.length !== 3) return null;
364
+ const t = o[0], n = parseInt(o[2]), s = e.components[t];
365
+ if (!s || s.type !== "repeater" || !s.components) return null;
366
+ const i = s.components[n];
367
+ return (i == null ? void 0 : i.uuid) ?? null;
368
+ }, Ae = (e, r, o, t, n) => {
369
+ const s = r(), i = s.groupComponentMap[o] ?? o, a = gn(e, r, i), c = {
370
+ components: s.components,
371
+ groupComponentMap: s.groupComponentMap
372
+ }, d = F(t, c);
373
+ t.valid = d.length === 0;
374
+ const l = {
375
+ ...s.validationErrors,
376
+ [o]: d
377
+ }, u = s.validationsByDependency[o];
378
+ if (u)
379
+ for (const w of u) {
380
+ const C = q(s, w.target);
381
+ if (C) {
382
+ const N = F(C, c);
383
+ C.valid = N.length === 0, l[w.target] = N;
384
+ }
385
+ }
386
+ const p = pe(s, n), f = {
387
+ components: { ...s.components },
388
+ validationErrors: l
389
+ };
390
+ if (a && (f.conditionResults = a), p) {
391
+ const w = { ...s, ...f };
392
+ f.screenValidity = {
393
+ ...s.screenValidity,
394
+ [p]: Z(w, p)
395
+ };
396
+ }
397
+ const y = { ...s, ...f }, g = o.includes(":") ? vn(y, o) : null, h = [], v = (w) => {
398
+ if (mn(y, w)) {
399
+ const C = y.computedByDependency[w] ?? [];
400
+ for (const N of C)
401
+ h.includes(N) || (h.push(N), v(N));
402
+ }
403
+ };
404
+ v(i), g && g !== i && v(g);
405
+ let b = be(y, i);
406
+ if (g && g !== i) {
407
+ const w = b ? { ...y, calculationResults: b } : y, C = be(w, g);
408
+ C && (b = C);
409
+ }
410
+ for (const w of h) {
411
+ const C = b ? { ...y, calculationResults: b } : y, N = be(C, w);
412
+ N && (b = N);
413
+ }
414
+ b && (f.calculationResults = b), e(f);
415
+ }, hn = (e, r) => ({
416
+ updateComponentValue: (o, t) => {
417
+ var i, a;
418
+ const n = r();
419
+ if (o.includes(":")) {
420
+ const c = o.split(":"), d = c[0], l = parseInt(c[1]), u = parseInt(c[2]), p = n.components[d];
421
+ if (!((a = (i = p == null ? void 0 : p.addedComponents) == null ? void 0 : i[l]) != null && a[u])) return;
422
+ const f = p.addedComponents[l][u].value !== t;
423
+ if (p.addedComponents[l][u] = { ...p.addedComponents[l][u], value: t }, f) {
424
+ Ae(
425
+ e,
426
+ r,
427
+ o,
428
+ p.addedComponents[l][u],
429
+ d
430
+ );
431
+ return;
432
+ }
433
+ e({ components: { ...n.components } });
434
+ return;
435
+ }
436
+ const s = n.components[o];
437
+ if (s) {
438
+ if (s.type === "group" || s.type === "repeater") {
439
+ e({ components: { ...n.components } });
440
+ return;
441
+ }
442
+ t !== s.value && (s.value = t, Ae(e, r, o, s, o));
443
+ }
444
+ },
445
+ addGroupIteration: (o) => {
446
+ const t = r(), n = t.components[o];
447
+ if (!n || n.type !== "group" || !n.components)
448
+ return;
449
+ n.addedComponents || (n.addedComponents = []);
450
+ const s = { ...t.conditionsByDependency }, i = n.components.map((d, l) => {
451
+ const u = Y(d);
452
+ if (u.uuid = [o, n.addedComponents.length, l].join(
453
+ ":"
454
+ ), t.groupComponentMap[u.uuid] = d.uuid, u.conditions && Object.keys(u.conditions).length > 0) {
455
+ const f = "when" in u.conditions ? u.conditions : null;
456
+ f && (u.conditions = f, X(
457
+ s,
458
+ u.uuid,
459
+ f,
460
+ "component"
461
+ ));
462
+ }
463
+ const p = F(u);
464
+ return u.valid = p.length === 0, u;
465
+ });
466
+ n.addedComponents.push(i);
467
+ const a = pe(t, o), c = { ...t.screenValidity };
468
+ return a && (c[a] = Z(
469
+ {
470
+ ...t
471
+ },
472
+ a
473
+ )), e({
474
+ components: { ...t.components },
475
+ groupComponentMap: { ...t.groupComponentMap },
476
+ conditionsByDependency: s,
477
+ screenValidity: c
478
+ }), n.addedComponents;
479
+ },
480
+ addRepeaterIteration: (o) => {
481
+ const t = r(), n = t.components[o];
482
+ if (!n || n.type !== "repeater" || !n.components)
483
+ return;
484
+ n.addedComponents || (n.addedComponents = []);
485
+ const s = { ...t.conditionsByDependency }, i = n.components.map((u, p) => {
486
+ const f = Y(u);
487
+ if (f.uuid = [
488
+ o,
489
+ n.addedComponents.length,
490
+ p
491
+ ].join(":"), f.conditions && Object.keys(f.conditions).length > 0) {
492
+ const g = "when" in f.conditions ? f.conditions : null;
493
+ g && (f.conditions = g, X(
494
+ s,
495
+ f.uuid,
496
+ g,
497
+ "component"
498
+ ));
499
+ }
500
+ const y = F(f);
501
+ return f.valid = y.length === 0, f;
502
+ });
503
+ n.addedComponents.push(i);
504
+ const a = pe(t, o), c = { ...t.screenValidity };
505
+ a && (c[a] = Z(
506
+ {
507
+ ...t
508
+ },
509
+ a
510
+ ));
511
+ const d = {
512
+ ...t,
513
+ components: { ...t.components }
514
+ };
515
+ Re(d);
516
+ const l = Se(d);
517
+ return e({
518
+ components: { ...t.components },
519
+ conditionsByDependency: s,
520
+ screenValidity: c,
521
+ calculationResults: l
522
+ }), n.addedComponents;
523
+ },
524
+ removeRepeaterIteration: (o, t) => {
525
+ var g;
526
+ const n = r(), s = n.components[o];
527
+ if (!s || s.type !== "repeater" || !s.addedComponents)
528
+ return;
529
+ const i = ((g = s.properties) == null ? void 0 : g.minIterations) ?? 1;
530
+ if (s.addedComponents.length <= i) return;
531
+ const a = s.addedComponents[t], c = new Set(a.map((h) => h.uuid));
532
+ s.addedComponents.splice(t, 1);
533
+ const d = { ...n.conditionsByDependency }, l = { ...n.validationErrors };
534
+ for (const h of c)
535
+ delete l[h], delete d[h];
536
+ for (const h of Object.keys(d)) {
537
+ const v = d[h], b = v.filter((w) => !c.has(w.target));
538
+ b.length === 0 ? delete d[h] : b.length !== v.length && (d[h] = b);
539
+ }
540
+ for (let h = t; h < s.addedComponents.length; h++)
541
+ s.addedComponents[h].forEach((v, b) => {
542
+ const w = v.uuid, C = [o, h, b].join(":");
543
+ if (w !== C) {
544
+ v.uuid = C, w in l && (l[C] = l[w], delete l[w]), w in d && (d[C] = d[w], delete d[w]);
545
+ for (const N of Object.values(d))
546
+ for (const T of N)
547
+ T.target === w && (T.target = C);
548
+ }
549
+ });
550
+ const u = pe(n, o), p = { ...n.screenValidity };
551
+ u && (p[u] = Z(
552
+ {
553
+ ...n
554
+ },
555
+ u
556
+ ));
557
+ const f = {
558
+ ...n,
559
+ components: { ...n.components }
560
+ };
561
+ Re(f);
562
+ const y = Se(f);
563
+ e({
564
+ components: { ...n.components },
565
+ conditionsByDependency: d,
566
+ validationErrors: l,
567
+ screenValidity: p,
568
+ calculationResults: y
569
+ });
570
+ }
571
+ }), q = (e, r) => Xe(e.components, r), X = (e, r, o, t) => {
572
+ const n = /* @__PURE__ */ new Set();
573
+ for (const i of o.when.rules)
574
+ n.add(i.source);
575
+ const s = { target: r, type: t, config: o };
576
+ for (const i of n)
577
+ e[i] || (e[i] = []), e[i].push(s);
578
+ }, yn = (e, r) => {
579
+ var o, t;
580
+ return r.includes(":") ? (o = q(e, r)) == null ? void 0 : o.value : (t = e.components[r]) == null ? void 0 : t.value;
581
+ }, te = (e, r) => rn(r, (o, t) => (t == null ? void 0 : t.sourceType) === "context" ? e.externalContext[t.source] : yn(e, o)), gn = (e, r, o) => {
582
+ const t = r(), n = t.conditionsByDependency[o];
583
+ if (!n || n.length === 0) return null;
584
+ let s = !1;
585
+ const i = { ...t.conditionResults };
586
+ for (const a of n) {
587
+ const c = te(t, a.config);
588
+ i[a.target] !== c && (i[a.target] = c, s = !0);
589
+ }
590
+ return s ? i : null;
591
+ }, bn = (e) => {
592
+ const r = {}, o = /* @__PURE__ */ new Set();
593
+ for (const t of Object.values(e.conditionsByDependency))
594
+ for (const n of t)
595
+ o.has(n.target) || (o.add(n.target), r[n.target] = te(e, n.config));
596
+ return r;
597
+ }, Ce = (e) => {
598
+ if (!e || me(e.type)) return null;
599
+ const r = "label" in e.properties ? e.properties.label : "", o = {
600
+ uuid: e.uuid,
601
+ type: e.type,
602
+ value: e.value
603
+ };
604
+ return r && (o.label = r), o;
605
+ }, je = (e) => {
606
+ if (e.conditions && "when" in e.conditions)
607
+ return e.conditions;
608
+ }, Cn = (e) => {
609
+ const r = {
610
+ uuid: e.flowUUID,
611
+ metadata: e.metadata,
612
+ screens: []
613
+ }, o = (t) => {
614
+ const n = e.screens[t];
615
+ if (!n) return !1;
616
+ const s = n.conditions && "when" in n.conditions ? n.conditions : void 0;
617
+ return Q(s, e.conditionResults[t]);
618
+ };
619
+ return r.screens = e.screenOrder.filter((t) => !o(t)).map((t) => {
620
+ const n = e.screens[t], s = {
621
+ uuid: t,
622
+ components: n.components.map((i) => {
623
+ const a = e.components[i.uuid];
624
+ if (!a) return null;
625
+ const c = je(a);
626
+ if (Q(c, e.conditionResults[i.uuid]))
627
+ return null;
628
+ if ((a.type === "group" || a.type === "repeater") && a.addedComponents) {
629
+ const d = Ce(a);
630
+ return d ? {
631
+ ...d,
632
+ components: a.addedComponents.map(
633
+ (l) => l.filter((u) => {
634
+ const p = je(u);
635
+ return !Q(
636
+ p,
637
+ e.conditionResults[u.uuid]
638
+ );
639
+ }).map((u) => Ce(u)).filter((u) => u !== null)
640
+ )
641
+ } : null;
642
+ }
643
+ return Ce(a);
644
+ }).filter((i) => i !== null)
645
+ };
646
+ return n.label && (s.label = n.label), s;
647
+ }), r;
648
+ }, ye = Je(
649
+ null
650
+ );
651
+ function L(e) {
652
+ const r = xe(ye);
653
+ if (!r)
654
+ throw new Error("useFBREStore must be used within an FBRE provider");
655
+ return W(r, e);
656
+ }
657
+ const Ke = () => {
658
+ const e = xe(ye);
659
+ if (!e)
660
+ throw new Error("useFBREStoreApi must be used within an FBRE provider");
661
+ return e;
662
+ }, le = (e) => !e || Object.keys(e).length === 0 ? null : "when" in e ? e : null, wn = [], Sn = () => Ye((e, r) => ({
663
+ // Identity
664
+ flowUUID: "",
665
+ metadata: {},
666
+ config: {},
667
+ externalContext: {},
668
+ // Screens
669
+ screenOrder: [],
670
+ screens: {},
671
+ screenValidity: {},
672
+ // Components
673
+ components: {},
674
+ groupComponentMap: {},
675
+ templateComponentMap: {},
676
+ // Conditions
677
+ conditionsByDependency: {},
678
+ conditionResults: {},
679
+ // Validation
680
+ validationErrors: {},
681
+ validationsByDependency: {},
682
+ // Calculations
683
+ calculations: [],
684
+ calculationResults: {},
685
+ calculationsByDependency: {},
686
+ // Computed components
687
+ computedByDependency: {},
688
+ // Actions
689
+ loadFlow: (o, t, n) => {
690
+ var T;
691
+ const s = {}, i = [], a = {}, c = {}, d = {}, l = {}, u = {}, p = {}, f = {};
692
+ o.screens.forEach((x) => {
693
+ const M = le(x.conditions), U = M ? { ...x, conditions: M } : { ...x };
694
+ s[x.uuid] = U, i.push(x.uuid), M && X(
695
+ l,
696
+ x.uuid,
697
+ M,
698
+ "screen"
699
+ ), x.components.forEach((S) => {
700
+ var V;
701
+ const D = le(S.conditions), B = Y(S);
702
+ if (D && (B.conditions = D), a[S.uuid] = B, D && X(
703
+ l,
704
+ S.uuid,
705
+ D,
706
+ "component"
707
+ ), (V = B.properties) != null && V.validation && ae(
708
+ u,
709
+ S.uuid,
710
+ B.properties.validation
711
+ ), (S.type === "group" || S.type === "repeater") && S.components) {
712
+ const $ = a[S.uuid];
713
+ $.addedComponents || ($.addedComponents = []);
714
+ const J = S.properties ?? {}, R = S.type === "repeater" ? J.initialData : void 0, E = S.type === "repeater" ? J.minIterations : void 0, _ = R ? R.length : S.type === "repeater" && E && E > 1 ? E : 1;
715
+ for (let O = 0; O < _; O++) {
716
+ const ee = S.components.map((H, ge) => {
717
+ var ie;
718
+ const A = Y(H);
719
+ A.uuid = [
720
+ S.uuid,
721
+ $.addedComponents.length,
722
+ ge
723
+ ].join(":"), R && R[O] && H.uuid in R[O] && (A.value = R[O][H.uuid]), S.type === "group" && (c[A.uuid] = H.uuid), O === 0 && (d[H.uuid] = A.uuid);
724
+ const re = le(A.conditions);
725
+ re && (A.conditions = re, X(
726
+ l,
727
+ A.uuid,
728
+ re,
729
+ "component"
730
+ )), (ie = A.properties) != null && ie.validation && ae(
731
+ u,
732
+ A.uuid,
733
+ A.properties.validation
734
+ );
735
+ const se = F(A);
736
+ return A.valid = se.length === 0, se.length > 0 && (p[A.uuid] = se), A;
737
+ });
738
+ $.addedComponents.push(ee);
739
+ }
740
+ }
741
+ });
742
+ }), t && t.screens.forEach((x) => {
743
+ const M = i.indexOf(x.uuid);
744
+ M < 0 || x.components.forEach((U) => {
745
+ const S = a[U.uuid];
746
+ if (S)
747
+ if ((S.type === "group" || S.type === "repeater") && U.components && S.addedComponents) {
748
+ if (U.components.length > 1) {
749
+ const D = S.addedComponents.length, B = U.components.length - D, G = o.screens[M].components.find(
750
+ (V) => V.uuid === S.uuid
751
+ );
752
+ for (let V = 0; V < B; V++)
753
+ if (G != null && G.components) {
754
+ const $ = G.components.map(
755
+ (J, R) => {
756
+ var ee;
757
+ const E = Y(J);
758
+ E.uuid = [
759
+ S.uuid,
760
+ S.addedComponents.length,
761
+ R
762
+ ].join(":"), S.type === "group" && (c[E.uuid] = J.uuid), d[J.uuid] || (d[J.uuid] = E.uuid);
763
+ const _ = le(
764
+ E.conditions
765
+ );
766
+ _ && (E.conditions = _, X(
767
+ l,
768
+ E.uuid,
769
+ _,
770
+ "component"
771
+ )), (ee = E.properties) != null && ee.validation && ae(
772
+ u,
773
+ E.uuid,
774
+ E.properties.validation
775
+ );
776
+ const O = F(E);
777
+ return E.valid = O.length === 0, O.length > 0 && (p[E.uuid] = O), E;
778
+ }
779
+ );
780
+ S.addedComponents.push($);
781
+ }
782
+ }
783
+ U.components.forEach((D) => {
784
+ D.forEach((B) => {
785
+ for (const G of S.addedComponents) {
786
+ const V = G.find(($) => $.uuid === B.uuid);
787
+ if (V) {
788
+ V.value = B.value;
789
+ const $ = F(V);
790
+ V.valid = $.length === 0, p[V.uuid] = $;
791
+ break;
792
+ }
793
+ }
794
+ });
795
+ });
796
+ } else {
797
+ S.value = U.value;
798
+ const D = F(S);
799
+ S.valid = D.length === 0, p[S.uuid] = D;
800
+ }
801
+ });
802
+ });
803
+ for (const x of Object.keys(a)) {
804
+ const M = a[x];
805
+ if (!(M.type === "group" || M.type === "repeater" || !((T = M.properties) != null && T.validation)) && !(x in p)) {
806
+ const U = F(M);
807
+ M.valid = U.length === 0, U.length > 0 && (p[x] = U);
808
+ }
809
+ }
810
+ const y = {
811
+ ...r(),
812
+ externalContext: n ?? {},
813
+ screens: s,
814
+ screenOrder: i,
815
+ components: a,
816
+ groupComponentMap: c,
817
+ templateComponentMap: d,
818
+ conditionsByDependency: l,
819
+ conditionResults: {},
820
+ validationErrors: p,
821
+ validationsByDependency: u
822
+ }, g = bn(y);
823
+ y.conditionResults = g, i.forEach((x) => {
824
+ f[x] = Z(y, x);
825
+ });
826
+ const h = fn(a), v = {
827
+ ...y,
828
+ computedByDependency: h,
829
+ calculationResults: {},
830
+ screenValidity: f
831
+ };
832
+ Re(v);
833
+ const b = o.calculations ?? [], w = pn(b), C = {
834
+ ...v,
835
+ calculations: b
836
+ }, N = Se(C);
837
+ e({
838
+ flowUUID: o.uuid,
839
+ metadata: o.metadata,
840
+ config: o.config ?? {},
841
+ externalContext: n ?? {},
842
+ screens: s,
843
+ screenOrder: i,
844
+ components: a,
845
+ groupComponentMap: c,
846
+ templateComponentMap: d,
847
+ conditionsByDependency: l,
848
+ conditionResults: g,
849
+ screenValidity: f,
850
+ validationErrors: p,
851
+ validationsByDependency: u,
852
+ calculations: b,
853
+ calculationResults: N,
854
+ calculationsByDependency: w,
855
+ computedByDependency: h
856
+ });
857
+ },
858
+ updateContext: (o) => {
859
+ const t = r(), n = t.externalContext, s = [];
860
+ for (const d of Object.keys(o))
861
+ n[d] !== o[d] && s.push(d);
862
+ for (const d of Object.keys(n))
863
+ !(d in o) && !s.includes(d) && s.push(d);
864
+ if (s.length === 0) return;
865
+ const i = { ...t.conditionResults };
866
+ let a = !1;
867
+ const c = /* @__PURE__ */ new Set();
868
+ for (const d of s) {
869
+ const l = t.conditionsByDependency[d];
870
+ if (!(!l || l.length === 0))
871
+ for (const u of l) {
872
+ if (c.has(u.target)) continue;
873
+ c.add(u.target);
874
+ const p = { ...t, externalContext: o }, f = te(p, u.config);
875
+ i[u.target] !== f && (i[u.target] = f, a = !0);
876
+ }
877
+ }
878
+ if (a) {
879
+ const d = { ...t, conditionResults: i }, l = {};
880
+ for (const u of t.screenOrder)
881
+ l[u] = Z(d, u);
882
+ e({ externalContext: o, conditionResults: i, screenValidity: l });
883
+ } else
884
+ e({ externalContext: o });
885
+ },
886
+ ...hn(e, r),
887
+ getFlowData: () => Cn(r()),
888
+ getScreenByIndex: (o) => {
889
+ const t = r();
890
+ return o < 0 || o >= t.screenOrder.length ? null : t.screens[t.screenOrder[o]];
891
+ },
892
+ getScreenValidity: (o) => {
893
+ const t = r();
894
+ return o < 0 || o >= t.screenOrder.length ? !0 : t.screenValidity[t.screenOrder[o]] ?? !0;
895
+ },
896
+ getValidationErrors: (o) => r().validationErrors[o] ?? wn,
897
+ evaluateFlowValidation: () => {
898
+ const o = r(), t = {};
899
+ let n = !0;
900
+ return o.screenOrder.forEach((s) => {
901
+ const i = Z(o, s);
902
+ t[s] = i, i || (n = !1);
903
+ }), e({ screenValidity: t }), n;
904
+ },
905
+ getMaxScreenCount: () => r().screenOrder.length - 1,
906
+ getCalculationResult: (o) => r().calculationResults[o] ?? null
907
+ })), Rn = ({ uuid: e }) => {
908
+ var a;
909
+ const r = L((c) => c.components[e]), o = L((c) => c.conditionResults[e]);
910
+ if (!r) return null;
911
+ const t = r.conditions && "when" in r.conditions ? r.conditions : void 0;
912
+ if (Q(t, o)) return null;
913
+ const n = an(r.type);
914
+ if (!n)
915
+ return console.warn(`FBRE: No renderer for component type "${r.type}"`), null;
916
+ const s = (a = r.properties) == null ? void 0 : a.width, i = s && s !== "full" ? `fbre-component fbre-width-${s}` : "fbre-component";
917
+ return /* @__PURE__ */ m("div", { className: i, "data-uuid": e, children: /* @__PURE__ */ m(n, { uuid: e }) });
918
+ }, Ne = ({ screenIndex: e }) => {
919
+ const r = L((o) => {
920
+ const t = o.screenOrder[e];
921
+ return t ? o.screens[t] : null;
922
+ });
923
+ return r ? /* @__PURE__ */ m("div", { className: "fbre-screen", children: r.components.map((o) => /* @__PURE__ */ m(Rn, { uuid: o.uuid }, o.uuid)) }) : null;
924
+ }, Le = ({ size: e = 14 }) => /* @__PURE__ */ m(
925
+ "svg",
926
+ {
927
+ width: e,
928
+ height: e,
929
+ viewBox: "0 0 24 24",
930
+ fill: "none",
931
+ stroke: "currentColor",
932
+ strokeWidth: "2.5",
933
+ strokeLinecap: "round",
934
+ strokeLinejoin: "round",
935
+ children: /* @__PURE__ */ m("polyline", { points: "4,12 9.5,17.5 20,7" })
936
+ }
937
+ ), Nn = ({ size: e = 12 }) => /* @__PURE__ */ m("svg", { width: e, height: e, viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ m("path", { d: "M7 10l5 5 5-5z" }) }), xn = ({ size: e = 16 }) => /* @__PURE__ */ m("svg", { width: e, height: e, viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ m("path", { d: "M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z" }) }), En = ({ size: e = 16 }) => /* @__PURE__ */ m("svg", { width: e, height: e, viewBox: "0 0 24 24", fill: "currentColor", children: /* @__PURE__ */ m("path", { d: "M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z" }) }), $e = ({
938
+ position: e,
939
+ screenIndex: r,
940
+ maxScreenIndex: o,
941
+ allowInvalidTransition: t,
942
+ screenValidity: n,
943
+ summaryEnabled: s,
944
+ summaryActive: i,
945
+ atMaxScreen: a,
946
+ nextButtonLabel: c,
947
+ backButtonLabel: d,
948
+ completionLoading: l,
949
+ nextScreen: u,
950
+ prevScreen: p,
951
+ onFlowComplete: f
952
+ }) => {
953
+ const y = l ?? !1;
954
+ if (e === "back")
955
+ return /* @__PURE__ */ k(
956
+ "button",
957
+ {
958
+ type: "button",
959
+ className: "fbre-btn fbre-btn--secondary",
960
+ disabled: r === 0 && !i || y,
961
+ onClick: p,
962
+ children: [
963
+ /* @__PURE__ */ m(xn, {}),
964
+ d || "Back"
965
+ ]
966
+ }
967
+ );
968
+ const g = s ? a && i : a, h = s ? a ? i ? "Done" : "Review" : "Next" : a ? "Done" : "Next";
969
+ return o === 0 ? /* @__PURE__ */ k(
970
+ "button",
971
+ {
972
+ type: "button",
973
+ className: "fbre-btn fbre-btn--primary",
974
+ disabled: y || !n,
975
+ onClick: () => {
976
+ s && !i ? u() : f();
977
+ },
978
+ children: [
979
+ c || (s ? i ? "Complete" : "Review" : "Complete"),
980
+ y ? /* @__PURE__ */ m("span", { className: "fbre-btn__spinner" }) : /* @__PURE__ */ m(Le, { size: 16 })
981
+ ]
982
+ }
983
+ ) : /* @__PURE__ */ k(
984
+ "button",
985
+ {
986
+ type: "button",
987
+ className: "fbre-btn fbre-btn--primary",
988
+ disabled: y || !t && !n || h === "Done" && !n,
989
+ onClick: () => {
990
+ g ? f() : u();
991
+ },
992
+ children: [
993
+ c || h,
994
+ y ? /* @__PURE__ */ m("span", { className: "fbre-btn__spinner" }) : g ? /* @__PURE__ */ m(Le, { size: 16 }) : /* @__PURE__ */ m(En, {})
995
+ ]
996
+ }
997
+ );
998
+ }, In = ({ current: e, total: r }) => {
999
+ if (r <= 1) return null;
1000
+ const o = (e + 1) / r * 100;
1001
+ return r <= 6 ? /* @__PURE__ */ m("div", { className: "fbre-stepper fbre-stepper--dots", children: Array.from({ length: r }, (t, n) => /* @__PURE__ */ m(
1002
+ "div",
1003
+ {
1004
+ className: `fbre-stepper__dot${n === e ? " active" : n < e ? " completed" : ""}`
1005
+ },
1006
+ n
1007
+ )) }) : /* @__PURE__ */ m("div", { className: "fbre-stepper fbre-stepper--bar", children: /* @__PURE__ */ m(
1008
+ "div",
1009
+ {
1010
+ className: "fbre-stepper__fill",
1011
+ style: { width: `${o}%` }
1012
+ }
1013
+ ) });
1014
+ }, Dn = ({ current: e, total: r }) => r <= 1 ? null : /* @__PURE__ */ k("div", { className: "fbre-stepper-text", children: [
1015
+ "Step ",
1016
+ e + 1,
1017
+ " of ",
1018
+ r
1019
+ ] }), Bn = (e) => {
1020
+ const r = e.controlsLayout === "stacked", o = e.showStepper !== !1 && (e.stepperStyle ?? "default") === "default";
1021
+ return /* @__PURE__ */ k(
1022
+ "div",
1023
+ {
1024
+ className: r ? "fbre-controls fbre-controls--stacked" : "fbre-controls",
1025
+ children: [
1026
+ e.screenIndex > 0 || e.summaryActive ? /* @__PURE__ */ m($e, { position: "back", ...e }) : /* @__PURE__ */ m("div", {}),
1027
+ o && e.maxScreenIndex > 0 && /* @__PURE__ */ m(In, { current: e.screenIndex, total: e.maxScreenIndex + 1 }),
1028
+ /* @__PURE__ */ m($e, { position: "next", ...e })
1029
+ ]
1030
+ }
1031
+ );
1032
+ }, Qe = ({
1033
+ component: e
1034
+ }) => {
1035
+ const { uuid: r, label: o, type: t, value: n, components: s } = e, i = () => {
1036
+ if (n != null)
1037
+ switch (t) {
1038
+ case "fileUpload":
1039
+ return n.name;
1040
+ case "inputText":
1041
+ case "inputTextArea":
1042
+ case "inputNumber":
1043
+ case "dropDown":
1044
+ case "radio":
1045
+ case "rating":
1046
+ case "slider":
1047
+ return n.toString();
1048
+ case "computed":
1049
+ return typeof n == "number" ? n.toString() : "—";
1050
+ case "toggleSwitch":
1051
+ return n ? "Yes" : "No";
1052
+ case "checkbox":
1053
+ case "dropDownMulti":
1054
+ return n.length === 0 ? "No options selected" : n.join(", ");
1055
+ }
1056
+ return "No response given";
1057
+ };
1058
+ if (t === "signature" && n) {
1059
+ const d = typeof n == "string" && n.startsWith("data:");
1060
+ return /* @__PURE__ */ k("div", { className: "fbre-summary__item", children: [
1061
+ /* @__PURE__ */ m("div", { className: "fbre-summary__item-label", children: o }),
1062
+ /* @__PURE__ */ m("div", { className: "fbre-summary__item-value", children: d ? /* @__PURE__ */ m(
1063
+ "img",
1064
+ {
1065
+ src: n,
1066
+ alt: "Signature",
1067
+ style: { maxWidth: "100%", maxHeight: 80 }
1068
+ }
1069
+ ) : /* @__PURE__ */ m("span", { style: { fontFamily: '"Brush Script MT", "Segoe Script", "Dancing Script", cursive', fontSize: 24 }, children: n }) })
1070
+ ] });
1071
+ }
1072
+ const a = () => s ? s.map((d, l) => /* @__PURE__ */ m("div", { children: d.map((u) => /* @__PURE__ */ m(Qe, { component: u }, u.uuid)) }, `${r}-${l}`)) : null, c = n == null || typeof n == "string" && n.length === 0 || Array.isArray(n) && n.length === 0;
1073
+ return /* @__PURE__ */ k(
1074
+ "div",
1075
+ {
1076
+ className: `fbre-summary__item${t === "group" || t === "repeater" ? " fbre-summary__item--group" : ""}`,
1077
+ children: [
1078
+ /* @__PURE__ */ m("div", { className: "fbre-summary__item-label", children: o }),
1079
+ t !== "group" && t !== "repeater" && /* @__PURE__ */ m(
1080
+ "div",
1081
+ {
1082
+ className: `fbre-summary__item-value${c ? " fbre-summary__item-value--empty" : ""}`,
1083
+ children: i()
1084
+ }
1085
+ ),
1086
+ (t === "group" || t === "repeater") && a()
1087
+ ]
1088
+ }
1089
+ );
1090
+ }, Vn = /\$\{([^}]+)\}/g, On = (e, r) => e.replace(Vn, (o, t) => {
1091
+ const n = r.resolveCalculation(t);
1092
+ if (n !== null) return n;
1093
+ const s = r.resolveComponentValue(t);
1094
+ return s !== null ? s : "";
1095
+ });
1096
+ function kn() {
1097
+ const e = L((n) => n.components), r = L((n) => n.templateComponentMap), o = L((n) => n.calculations), t = L((n) => n.calculationResults);
1098
+ return Ee(() => {
1099
+ const n = new Map(o.map((i) => [i.uuid, i])), s = (i) => {
1100
+ var u, p;
1101
+ const a = e[i];
1102
+ if (a) return a;
1103
+ const c = r[i];
1104
+ if (!c) return;
1105
+ const d = c.split(":"), l = e[d[0]];
1106
+ return (p = (u = l == null ? void 0 : l.addedComponents) == null ? void 0 : u[parseInt(d[1])]) == null ? void 0 : p[parseInt(d[2])];
1107
+ };
1108
+ return {
1109
+ resolveComponentValue: (i) => {
1110
+ const a = s(i);
1111
+ if (!a) return null;
1112
+ const c = a.value;
1113
+ return c == null || c === "" ? null : Array.isArray(c) ? c.join(", ") : String(c);
1114
+ },
1115
+ resolveCalculation: (i) => {
1116
+ const a = n.get(i);
1117
+ if (!a) return null;
1118
+ const c = t[i] ?? null;
1119
+ return sn(
1120
+ c,
1121
+ a.format,
1122
+ a.decimalPlaces,
1123
+ a.currencySymbol
1124
+ );
1125
+ }
1126
+ };
1127
+ }, [e, r, o, t]);
1128
+ }
1129
+ const Mn = ({ screen: e, showToggle: r }) => {
1130
+ const o = kn(), [t, n] = P(!0);
1131
+ return /* @__PURE__ */ k("div", { children: [
1132
+ /* @__PURE__ */ k(
1133
+ "div",
1134
+ {
1135
+ className: "fbre-summary__screen-header",
1136
+ onClick: () => r && n(!t),
1137
+ children: [
1138
+ e.label && /* @__PURE__ */ m("span", { className: "fbre-summary__screen-label", children: On(e.label, o) }),
1139
+ r && /* @__PURE__ */ m(
1140
+ "span",
1141
+ {
1142
+ className: `fbre-summary__screen-toggle${t ? "" : " collapsed"}`,
1143
+ children: /* @__PURE__ */ m(Nn, { size: 20 })
1144
+ }
1145
+ )
1146
+ ]
1147
+ }
1148
+ ),
1149
+ /* @__PURE__ */ m(
1150
+ "div",
1151
+ {
1152
+ className: `fbre-summary__screen-body${t ? "" : " collapsed"}`,
1153
+ children: e.components.map((s) => /* @__PURE__ */ m(Qe, { component: s }, s.uuid))
1154
+ }
1155
+ )
1156
+ ] });
1157
+ }, Pe = () => {
1158
+ const e = Ke(), r = L((n) => n.components), o = Ee(
1159
+ () => e.getState().getFlowData(),
1160
+ [e, r]
1161
+ ), t = o.screens.length;
1162
+ return /* @__PURE__ */ k("div", { className: "fbre-summary", children: [
1163
+ /* @__PURE__ */ m("h2", { className: "fbre-summary__title", children: "Review" }),
1164
+ /* @__PURE__ */ m("div", { className: "fbre-summary__body", children: o.screens.map((n) => /* @__PURE__ */ m(
1165
+ Mn,
1166
+ {
1167
+ screen: n,
1168
+ showToggle: t > 1
1169
+ },
1170
+ n.uuid
1171
+ )) })
1172
+ ] });
1173
+ }, Ze = (e, r, o) => r ? !Q(r, o[e]) : !0, Ge = (e, r) => {
1174
+ const o = e.screenOrder.length - 1;
1175
+ let t = r + 1;
1176
+ for (; t <= o; ) {
1177
+ const n = e.screenOrder[t], s = e.screens[n], i = s != null && s.conditions && "when" in s.conditions ? s.conditions : void 0;
1178
+ if (Ze(n, i, e.conditionResults)) return t;
1179
+ t++;
1180
+ }
1181
+ return null;
1182
+ }, _n = (e, r) => {
1183
+ let o = r - 1;
1184
+ for (; o >= 0; ) {
1185
+ const t = e.screenOrder[o], n = e.screens[t], s = n != null && n.conditions && "when" in n.conditions ? n.conditions : void 0;
1186
+ if (Ze(t, s, e.conditionResults)) return o;
1187
+ o--;
1188
+ }
1189
+ return 0;
1190
+ }, fe = /* @__PURE__ */ new Map(), rt = (e, r) => {
1191
+ fe.has(e) || fe.set(e, /* @__PURE__ */ new Set()), fe.get(e).add(r);
1192
+ }, st = (e, r) => {
1193
+ var o;
1194
+ (o = fe.get(e)) == null || o.delete(r);
1195
+ }, en = Je(null);
1196
+ function it() {
1197
+ return xe(en);
1198
+ }
1199
+ class he extends Error {
1200
+ constructor(r, o, t, n) {
1201
+ super(r), this.name = "ApiError", this.status = o, this.code = t, this.validationErrors = n;
1202
+ }
1203
+ }
1204
+ class Tn extends he {
1205
+ constructor(r, o) {
1206
+ super(`Request timed out after ${o}ms: ${r}`, 0, "TIMEOUT"), this.name = "TimeoutError";
1207
+ }
1208
+ }
1209
+ const Un = /* @__PURE__ */ new Set([502, 503, 504]), de = 2, Fn = [500, 1500];
1210
+ function An(e) {
1211
+ return new Promise((r) => setTimeout(r, e));
1212
+ }
1213
+ async function oe(e, r, o = {}) {
1214
+ const n = `${e.apiEndpoint || ""}${r}`, s = e.timeout ?? 3e4, i = {
1215
+ ...o.body ? { "Content-Type": "application/json" } : {},
1216
+ ...o.headers
1217
+ };
1218
+ e.apiKey && (i.Authorization = `Bearer ${e.apiKey}`);
1219
+ let a;
1220
+ for (let c = 0; c <= de; c++) {
1221
+ c > 0 && await An(Fn[c - 1]);
1222
+ try {
1223
+ const d = new AbortController(), l = setTimeout(() => d.abort(), s);
1224
+ let u;
1225
+ try {
1226
+ u = await fetch(n, {
1227
+ ...o,
1228
+ headers: i,
1229
+ signal: d.signal
1230
+ });
1231
+ } finally {
1232
+ clearTimeout(l);
1233
+ }
1234
+ if (!u.ok) {
1235
+ const p = await u.json().catch(() => ({})), f = new he(
1236
+ p.error || `HTTP ${u.status}`,
1237
+ u.status,
1238
+ p.code,
1239
+ p.validationErrors
1240
+ );
1241
+ if (c < de && Un.has(u.status)) {
1242
+ a = f;
1243
+ continue;
1244
+ }
1245
+ throw f;
1246
+ }
1247
+ return u.json();
1248
+ } catch (d) {
1249
+ if (d instanceof he)
1250
+ throw d;
1251
+ if (d instanceof DOMException && d.name === "AbortError") {
1252
+ const l = new Tn(n, s);
1253
+ if (c < de) {
1254
+ a = l;
1255
+ continue;
1256
+ }
1257
+ throw l;
1258
+ }
1259
+ if (d instanceof TypeError && c < de) {
1260
+ a = d;
1261
+ continue;
1262
+ }
1263
+ throw d;
1264
+ }
1265
+ }
1266
+ throw a;
1267
+ }
1268
+ async function jn(e, r) {
1269
+ return oe(e, `/api/v1/public/flows/${r}`);
1270
+ }
1271
+ async function Ln(e, r, o) {
1272
+ const t = { flowId: r };
1273
+ return o && Object.keys(o).length > 0 && (t.context = o), oe(e, "/api/v1/public/sessions", {
1274
+ method: "POST",
1275
+ body: JSON.stringify(t)
1276
+ });
1277
+ }
1278
+ async function $n(e, r, o) {
1279
+ return oe(
1280
+ e,
1281
+ `/api/v1/public/sessions/${r}/next`,
1282
+ {
1283
+ method: "POST",
1284
+ body: JSON.stringify({ data: o })
1285
+ }
1286
+ );
1287
+ }
1288
+ async function Pn(e, r, o) {
1289
+ return oe(
1290
+ e,
1291
+ `/api/v1/public/sessions/${r}/prev`,
1292
+ {
1293
+ method: "POST",
1294
+ body: JSON.stringify({ data: o })
1295
+ }
1296
+ );
1297
+ }
1298
+ async function Gn(e, r, o) {
1299
+ return oe(
1300
+ e,
1301
+ `/api/v1/public/sessions/${r}/complete`,
1302
+ {
1303
+ method: "POST",
1304
+ body: JSON.stringify({ data: o })
1305
+ }
1306
+ );
1307
+ }
1308
+ async function zn(e, r) {
1309
+ return oe(
1310
+ e,
1311
+ `/api/v1/public/sessions/${r}`
1312
+ );
1313
+ }
1314
+ const Jn = ({
1315
+ screenIndex: e,
1316
+ onFlowComplete: r,
1317
+ onScreenChange: o
1318
+ }) => {
1319
+ var ie, De, Be, Ve, Oe, ke, Me, _e, Te, Ue, Fe;
1320
+ const t = L((I) => I.config), n = L((I) => I.screenOrder.length), s = L((I) => Object.keys(I.components).length > 0), i = Ke(), a = Math.max(
1321
+ 0,
1322
+ Math.min(e, n - 1)
1323
+ ), [c, d] = P(a), [l, u] = P(
1324
+ !!(t != null && t.summary && e >= n)
1325
+ ), p = ((ie = t == null ? void 0 : t.navigation) == null ? void 0 : ie.transition) ?? "none", f = p !== "none", [y, g] = P(null), [h, v] = P(!1), [b, w] = P("forward"), [C, N] = P(!1), T = ce(null);
1326
+ K(() => {
1327
+ const I = Math.max(0, Math.min(e, n - 1));
1328
+ d(I);
1329
+ }, [e]);
1330
+ const x = L((I) => I.screenOrder[c]), M = L(
1331
+ (I) => x ? I.screenValidity[x] ?? !0 : !0
1332
+ ), U = L(
1333
+ (I) => {
1334
+ var j;
1335
+ return x ? (j = I.screens[x]) == null ? void 0 : j.nextButtonLabel : void 0;
1336
+ }
1337
+ ), S = L(
1338
+ (I) => {
1339
+ var j;
1340
+ return x ? (j = I.screens[x]) == null ? void 0 : j.backButtonLabel : void 0;
1341
+ }
1342
+ ), D = z(
1343
+ (I) => {
1344
+ o && o(I, i.getState().getFlowData());
1345
+ },
1346
+ [o, i]
1347
+ ), B = z(
1348
+ (I) => {
1349
+ g(c), v(l), w(I), N(!0), T.current && (T.current.scrollTop = 0);
1350
+ },
1351
+ [c, l]
1352
+ ), G = z(() => {
1353
+ N(!1), g(null);
1354
+ }, []), V = z(() => {
1355
+ if (!C)
1356
+ if (l)
1357
+ f && B("back"), u(!1), D(c);
1358
+ else {
1359
+ const I = i.getState(), j = _n(I, c);
1360
+ j !== c && (f && B("back"), d(j), D(j));
1361
+ }
1362
+ }, [
1363
+ C,
1364
+ l,
1365
+ c,
1366
+ f,
1367
+ B,
1368
+ D,
1369
+ i
1370
+ ]), $ = z(() => {
1371
+ if (C) return;
1372
+ const I = i.getState(), j = Ge(I, c);
1373
+ j !== null ? (f && B("forward"), d(j), D(j)) : t != null && t.summary && (f && B("forward"), u(!0), D(c + 1));
1374
+ }, [
1375
+ C,
1376
+ c,
1377
+ t,
1378
+ f,
1379
+ B,
1380
+ D,
1381
+ i
1382
+ ]), [J, R] = P(!1), [E, _] = P(null), O = z(() => {
1383
+ _(null);
1384
+ const I = r(i.getState().getFlowData());
1385
+ I && typeof I.then == "function" && (R(!0), I.then(() => {
1386
+ R(!1);
1387
+ }).catch((j) => {
1388
+ R(!1), _(j instanceof Error ? j.message : String(j));
1389
+ }));
1390
+ }, [r, i]), ee = L((I) => Ge(I, c) === null), H = ((De = t == null ? void 0 : t.theme) == null ? void 0 : De.darkMode) ?? !1, ge = ((Be = t == null ? void 0 : t.controls) == null ? void 0 : Be.showStepper) !== !1 && ((Ve = t == null ? void 0 : t.controls) == null ? void 0 : Ve.stepperStyle) === "text", A = {};
1391
+ (Oe = t == null ? void 0 : t.theme) != null && Oe.color && (A["--fbre-theme-color"] = t.theme.color);
1392
+ const re = () => h ? /* @__PURE__ */ m(Pe, {}) : y !== null ? /* @__PURE__ */ m(Ne, { screenIndex: y }) : null, se = C ? "fbre-screen-wrapper fbre-screen-wrapper--transitioning" : "fbre-screen-wrapper";
1393
+ return /* @__PURE__ */ k(
1394
+ "div",
1395
+ {
1396
+ className: "fbre-container",
1397
+ "data-style": ((ke = t == null ? void 0 : t.theme) == null ? void 0 : ke.style) ?? "clean",
1398
+ "data-mode": H ? "dark" : "light",
1399
+ "data-transition": f ? p : void 0,
1400
+ style: Object.keys(A).length > 0 ? A : void 0,
1401
+ children: [
1402
+ ge && /* @__PURE__ */ m(Dn, { current: c, total: n }),
1403
+ /* @__PURE__ */ k("div", { className: se, ref: T, children: [
1404
+ C && /* @__PURE__ */ m(
1405
+ "div",
1406
+ {
1407
+ className: "fbre-screen--exiting",
1408
+ "data-direction": b,
1409
+ onAnimationEnd: G,
1410
+ children: re()
1411
+ }
1412
+ ),
1413
+ /* @__PURE__ */ k(
1414
+ "div",
1415
+ {
1416
+ className: C ? "fbre-screen--entering" : void 0,
1417
+ "data-direction": C ? b : void 0,
1418
+ children: [
1419
+ !l && /* @__PURE__ */ m(Ne, { screenIndex: c }),
1420
+ l && /* @__PURE__ */ m(Pe, {})
1421
+ ]
1422
+ }
1423
+ )
1424
+ ] }),
1425
+ E && /* @__PURE__ */ m("div", { className: "fbre-completion-error", children: E }),
1426
+ ((Me = t == null ? void 0 : t.controls) == null ? void 0 : Me.show) !== !1 && s && /* @__PURE__ */ m(
1427
+ Bn,
1428
+ {
1429
+ screenIndex: c,
1430
+ maxScreenIndex: n - 1,
1431
+ allowInvalidTransition: (_e = t == null ? void 0 : t.navigation) == null ? void 0 : _e.allowInvalidTransition,
1432
+ screenValidity: M,
1433
+ summaryEnabled: (t == null ? void 0 : t.summary) ?? !1,
1434
+ summaryActive: l,
1435
+ atMaxScreen: ee,
1436
+ nextButtonLabel: U,
1437
+ backButtonLabel: S,
1438
+ completionLoading: J,
1439
+ showStepper: (Te = t == null ? void 0 : t.controls) == null ? void 0 : Te.showStepper,
1440
+ stepperStyle: (Ue = t == null ? void 0 : t.controls) == null ? void 0 : Ue.stepperStyle,
1441
+ controlsLayout: (Fe = t == null ? void 0 : t.controls) == null ? void 0 : Fe.layout,
1442
+ nextScreen: $,
1443
+ prevScreen: V,
1444
+ onFlowComplete: O
1445
+ }
1446
+ )
1447
+ ]
1448
+ }
1449
+ );
1450
+ }, Yn = [], qn = () => Ye((e, r) => ({
1451
+ // Identity (minimal — server drives the flow)
1452
+ flowUUID: "",
1453
+ metadata: {},
1454
+ config: {},
1455
+ externalContext: {},
1456
+ // Screens (single screen at a time)
1457
+ screenOrder: [],
1458
+ screens: {},
1459
+ screenValidity: {},
1460
+ // Components
1461
+ components: {},
1462
+ groupComponentMap: {},
1463
+ templateComponentMap: {},
1464
+ // Conditions (intra-screen only)
1465
+ conditionsByDependency: {},
1466
+ conditionResults: {},
1467
+ // Validation
1468
+ validationErrors: {},
1469
+ validationsByDependency: {},
1470
+ // Calculations (not used in server-driven mode yet)
1471
+ calculations: [],
1472
+ calculationResults: {},
1473
+ calculationsByDependency: {},
1474
+ // Computed components
1475
+ computedByDependency: {},
1476
+ // Server-driven specific
1477
+ sessionId: "",
1478
+ navigation: {
1479
+ canGoBack: !1,
1480
+ canGoForward: !1,
1481
+ screenNumber: 1,
1482
+ totalScreens: 1,
1483
+ progress: 0,
1484
+ isLastScreen: !0
1485
+ },
1486
+ loading: !1,
1487
+ error: "",
1488
+ // Actions
1489
+ clearError: () => {
1490
+ e({ error: "" });
1491
+ },
1492
+ /**
1493
+ * loadFlow is repurposed to load a server screen payload.
1494
+ * Not used directly — use loadScreen() pattern via set() instead.
1495
+ */
1496
+ loadFlow: () => {
1497
+ },
1498
+ updateContext: (o) => {
1499
+ e({ externalContext: o });
1500
+ },
1501
+ updateComponentValue: (o, t) => {
1502
+ var p, f;
1503
+ const n = r();
1504
+ if (o.includes(":")) {
1505
+ const y = o.split(":"), g = y[0], h = parseInt(y[1]), v = parseInt(y[2]), b = n.components[g];
1506
+ if (!((f = (p = b == null ? void 0 : b.addedComponents) == null ? void 0 : p[h]) != null && f[v])) return;
1507
+ b.addedComponents[h][v] = { ...b.addedComponents[h][v], value: t };
1508
+ const w = n.groupComponentMap[o] ?? o, C = n.conditionsByDependency[w];
1509
+ let N = n.conditionResults;
1510
+ if (C && C.length > 0) {
1511
+ N = { ...n.conditionResults };
1512
+ for (const S of C)
1513
+ N[S.target] = te(
1514
+ { ...n },
1515
+ S.config
1516
+ );
1517
+ }
1518
+ const T = {
1519
+ components: n.components,
1520
+ groupComponentMap: n.groupComponentMap
1521
+ }, x = F(
1522
+ b.addedComponents[h][v],
1523
+ T
1524
+ );
1525
+ b.addedComponents[h][v].valid = x.length === 0;
1526
+ const M = {
1527
+ ...n.validationErrors,
1528
+ [o]: x
1529
+ }, U = n.validationsByDependency[o];
1530
+ if (U)
1531
+ for (const S of U) {
1532
+ const D = ze(n, S.target);
1533
+ if (D) {
1534
+ const B = F(D, T);
1535
+ D.valid = B.length === 0, M[S.target] = B;
1536
+ }
1537
+ }
1538
+ e({
1539
+ components: { ...n.components },
1540
+ conditionResults: N,
1541
+ validationErrors: M
1542
+ });
1543
+ return;
1544
+ }
1545
+ const s = n.components[o];
1546
+ if (!s) return;
1547
+ if (s.type === "group" || s.type === "repeater") {
1548
+ e({ components: { ...n.components } });
1549
+ return;
1550
+ }
1551
+ if (t === s.value) return;
1552
+ s.value = t;
1553
+ const i = n.conditionsByDependency[o];
1554
+ let a = n.conditionResults;
1555
+ if (i && i.length > 0) {
1556
+ a = { ...n.conditionResults };
1557
+ for (const y of i)
1558
+ a[y.target] = te(
1559
+ { ...n },
1560
+ y.config
1561
+ );
1562
+ }
1563
+ const c = {
1564
+ components: n.components,
1565
+ groupComponentMap: n.groupComponentMap
1566
+ }, d = F(s, c);
1567
+ s.valid = d.length === 0;
1568
+ const l = { ...n.validationErrors, [o]: d }, u = n.validationsByDependency[o];
1569
+ if (u)
1570
+ for (const y of u) {
1571
+ const g = ze(n, y.target);
1572
+ if (g) {
1573
+ const h = F(g, c);
1574
+ g.valid = h.length === 0, l[y.target] = h;
1575
+ }
1576
+ }
1577
+ e({
1578
+ components: { ...n.components },
1579
+ conditionResults: a,
1580
+ validationErrors: l
1581
+ });
1582
+ },
1583
+ addGroupIteration: (o) => {
1584
+ const t = r(), n = t.components[o];
1585
+ if (!n || n.type !== "group" || !n.components)
1586
+ return;
1587
+ n.addedComponents || (n.addedComponents = []);
1588
+ const s = n.components.map((i, a) => {
1589
+ const c = Y(i);
1590
+ c.uuid = [o, n.addedComponents.length, a].join(
1591
+ ":"
1592
+ ), t.groupComponentMap[c.uuid] = i.uuid;
1593
+ const d = F(c);
1594
+ return c.valid = d.length === 0, c;
1595
+ });
1596
+ return n.addedComponents.push(s), e({
1597
+ components: { ...t.components },
1598
+ groupComponentMap: { ...t.groupComponentMap }
1599
+ }), n.addedComponents;
1600
+ },
1601
+ addRepeaterIteration: (o) => {
1602
+ const t = r(), n = t.components[o];
1603
+ if (!n || n.type !== "repeater" || !n.components)
1604
+ return;
1605
+ n.addedComponents || (n.addedComponents = []);
1606
+ const s = n.components.map((i, a) => {
1607
+ const c = Y(i);
1608
+ c.uuid = [
1609
+ o,
1610
+ n.addedComponents.length,
1611
+ a
1612
+ ].join(":");
1613
+ const d = F(c);
1614
+ return c.valid = d.length === 0, c;
1615
+ });
1616
+ return n.addedComponents.push(s), e({ components: { ...t.components } }), n.addedComponents;
1617
+ },
1618
+ removeRepeaterIteration: (o, t) => {
1619
+ const n = r(), s = n.components[o];
1620
+ if (!(!s || s.type !== "repeater" || !s.addedComponents || s.addedComponents.length <= 1)) {
1621
+ s.addedComponents.splice(t, 1);
1622
+ for (let i = t; i < s.addedComponents.length; i++)
1623
+ s.addedComponents[i].forEach((a, c) => {
1624
+ a.uuid = [o, i, c].join(":");
1625
+ });
1626
+ e({ components: { ...n.components } });
1627
+ }
1628
+ },
1629
+ getFlowData: () => ({ uuid: "", metadata: {}, screens: [] }),
1630
+ getScreenByIndex: (o) => {
1631
+ const t = r();
1632
+ if (o !== 0) return null;
1633
+ const n = t.screenOrder[0];
1634
+ return n ? t.screens[n] : null;
1635
+ },
1636
+ getScreenValidity: (o) => {
1637
+ const t = r();
1638
+ if (o !== 0) return !0;
1639
+ const n = t.screenOrder[0];
1640
+ return n ? t.screenValidity[n] ?? !0 : !0;
1641
+ },
1642
+ getValidationErrors: (o) => r().validationErrors[o] ?? Yn,
1643
+ evaluateFlowValidation: () => {
1644
+ const o = r(), t = o.screenOrder[0];
1645
+ if (!t) return !0;
1646
+ const n = o.screens[t];
1647
+ if (!n) return !0;
1648
+ let s = !0;
1649
+ const i = {
1650
+ components: o.components,
1651
+ groupComponentMap: o.groupComponentMap
1652
+ }, a = { ...o.validationErrors };
1653
+ for (const d of n.components) {
1654
+ const l = o.components[d.uuid];
1655
+ if (!l) continue;
1656
+ if ((l.type === "group" || l.type === "repeater") && l.addedComponents) {
1657
+ for (const f of l.addedComponents)
1658
+ for (const y of f) {
1659
+ const g = y.conditions && "when" in y.conditions ? y.conditions : void 0;
1660
+ if (g) {
1661
+ const v = o.conditionResults[y.uuid] ?? !1;
1662
+ if (g.action === "show" && !v || g.action === "hide" && v)
1663
+ continue;
1664
+ }
1665
+ const h = F(y, i);
1666
+ y.valid = h.length === 0, a[y.uuid] = h, h.length > 0 && (s = !1);
1667
+ }
1668
+ continue;
1669
+ }
1670
+ const u = l.conditions && "when" in l.conditions ? l.conditions : void 0;
1671
+ if (u) {
1672
+ const f = o.conditionResults[l.uuid] ?? !1;
1673
+ if (u.action === "show" && !f || u.action === "hide" && f)
1674
+ continue;
1675
+ }
1676
+ const p = F(l, i);
1677
+ l.valid = p.length === 0, a[l.uuid] = p, p.length > 0 && (s = !1);
1678
+ }
1679
+ const c = { ...o.screenValidity, [t]: s };
1680
+ return e({
1681
+ components: { ...o.components },
1682
+ validationErrors: a,
1683
+ screenValidity: c
1684
+ }), s;
1685
+ },
1686
+ getMaxScreenCount: () => 0,
1687
+ getCalculationResult: (o) => r().calculationResults[o] ?? null
1688
+ })), ue = (e, r) => {
1689
+ var y, g;
1690
+ const o = {}, t = {}, n = {}, s = {}, i = {}, a = {}, c = r.screen.uuid;
1691
+ for (const h of r.screen.components) {
1692
+ const v = Y(h);
1693
+ if (o[v.uuid] = v, (v.type === "group" || v.type === "repeater") && h.components) {
1694
+ v.addedComponents || (v.addedComponents = []);
1695
+ const b = h.components.map((w, C) => {
1696
+ var T;
1697
+ const N = Y(w);
1698
+ if (N.uuid = [v.uuid, 0, C].join(":"), v.type === "group" && (t[N.uuid] = w.uuid), n[w.uuid] = N.uuid, (T = N.properties) != null && T.validation) {
1699
+ const x = N.properties.validation;
1700
+ x.rules && x.rules.length > 0 && ae(i, N.uuid, x);
1701
+ }
1702
+ return N;
1703
+ });
1704
+ v.addedComponents.push(b);
1705
+ }
1706
+ if ((y = v.properties) != null && y.validation) {
1707
+ const b = v.properties.validation;
1708
+ b.rules && b.rules.length > 0 && ae(i, v.uuid, b);
1709
+ }
1710
+ }
1711
+ for (const { targetUUID: h, config: v } of r.conditions)
1712
+ X(s, h, v, h === c ? "screen" : "component");
1713
+ if (r.previousData)
1714
+ for (const h of r.previousData) {
1715
+ const v = o[h.uuid];
1716
+ v && (v.value = h.value);
1717
+ }
1718
+ const d = { components: o };
1719
+ for (const h of Object.keys(o)) {
1720
+ const v = o[h];
1721
+ if (v.type === "group" || v.type === "repeater") {
1722
+ if (v.addedComponents)
1723
+ for (const w of v.addedComponents)
1724
+ for (const C of w) {
1725
+ const N = F(C, d);
1726
+ C.valid = N.length === 0, N.length > 0 && (a[C.uuid] = N);
1727
+ }
1728
+ continue;
1729
+ }
1730
+ if (!((g = v.properties) != null && g.validation)) continue;
1731
+ const b = F(v, d);
1732
+ v.valid = b.length === 0, b.length > 0 && (a[h] = b);
1733
+ }
1734
+ const l = {}, u = {
1735
+ components: o
1736
+ }, p = /* @__PURE__ */ new Set();
1737
+ for (const h of Object.values(s))
1738
+ for (const v of h)
1739
+ p.has(v.target) || (p.add(v.target), l[v.target] = te(
1740
+ u,
1741
+ v.config
1742
+ ));
1743
+ let f = !0;
1744
+ for (const h of r.screen.components) {
1745
+ const v = o[h.uuid];
1746
+ if (v)
1747
+ if ((v.type === "group" || v.type === "repeater") && v.addedComponents)
1748
+ for (const b of v.addedComponents) {
1749
+ for (const w of b)
1750
+ if (!w.valid) {
1751
+ f = !1;
1752
+ break;
1753
+ }
1754
+ if (!f) break;
1755
+ }
1756
+ else v.valid === !1 && (f = !1);
1757
+ }
1758
+ e.setState({
1759
+ // Screen (single screen)
1760
+ screenOrder: [c],
1761
+ screens: {
1762
+ [c]: {
1763
+ uuid: c,
1764
+ label: r.screen.label,
1765
+ components: r.screen.components,
1766
+ nextButtonLabel: r.screen.nextButtonLabel,
1767
+ backButtonLabel: r.screen.backButtonLabel
1768
+ }
1769
+ },
1770
+ screenValidity: { [c]: f },
1771
+ // Components
1772
+ components: o,
1773
+ groupComponentMap: t,
1774
+ templateComponentMap: n,
1775
+ // Conditions
1776
+ conditionsByDependency: s,
1777
+ conditionResults: l,
1778
+ // Validation
1779
+ validationErrors: a,
1780
+ validationsByDependency: i,
1781
+ // Calculations (not used in server-driven mode yet)
1782
+ calculations: [],
1783
+ calculationResults: {},
1784
+ calculationsByDependency: {},
1785
+ // Computed components
1786
+ computedByDependency: {},
1787
+ // Server-driven state
1788
+ sessionId: r.sessionId,
1789
+ navigation: r.navigation,
1790
+ loading: !1,
1791
+ error: "",
1792
+ // External context
1793
+ externalContext: r.context ?? {},
1794
+ // Config
1795
+ config: r.config ?? {}
1796
+ });
1797
+ }, we = (e) => {
1798
+ var s;
1799
+ const r = e.getState(), o = r.screenOrder[0];
1800
+ if (!o) return [];
1801
+ const t = r.screens[o];
1802
+ if (!t) return [];
1803
+ const n = [];
1804
+ for (const i of t.components) {
1805
+ const a = r.components[i.uuid];
1806
+ if (!a || me(a.type)) continue;
1807
+ const c = {
1808
+ uuid: a.uuid,
1809
+ type: a.type,
1810
+ value: a.value
1811
+ };
1812
+ (s = a.properties) != null && s.label && (c.label = a.properties.label), (a.type === "group" || a.type === "repeater") && a.addedComponents && (c.components = a.addedComponents.map(
1813
+ (d) => d.filter((l) => !me(l.type)).map((l) => ({
1814
+ uuid: l.uuid,
1815
+ type: l.type,
1816
+ value: l.value
1817
+ }))
1818
+ )), n.push(c);
1819
+ }
1820
+ return n;
1821
+ }, ze = (e, r) => Xe(e.components, r), Hn = 6e4, Wn = ({
1822
+ sessionEndpoint: e,
1823
+ flowId: r,
1824
+ apiKey: o,
1825
+ theme: t,
1826
+ context: n,
1827
+ onFlowComplete: s,
1828
+ onScreenChange: i
1829
+ }) => {
1830
+ const a = ce(null);
1831
+ a.current || (a.current = qn());
1832
+ const c = a.current, d = W(c, (R) => R.loading), l = W(c, (R) => R.error), u = W(c, (R) => R.navigation), p = W(c, (R) => R.config), f = W(c, (R) => R.screenOrder), y = W(c, (R) => R.screens), [g, h] = P(""), v = ce(null), b = ce(Date.now()), w = Ee(
1833
+ () => ({ apiEndpoint: e, apiKey: o }),
1834
+ [e, o]
1835
+ ), C = z((R) => {
1836
+ h(R), v.current && clearTimeout(v.current), v.current = setTimeout(() => h(""), 8e3);
1837
+ }, []), N = z(() => {
1838
+ h(""), v.current && (clearTimeout(v.current), v.current = null);
1839
+ }, []), T = z(async () => {
1840
+ c.setState({ loading: !0, error: "" });
1841
+ try {
1842
+ const R = await Ln(w, r, n);
1843
+ ue(c, R);
1844
+ } catch (R) {
1845
+ c.setState({ loading: !1, error: R.message });
1846
+ }
1847
+ }, [r, e, o, n]);
1848
+ K(() => {
1849
+ T();
1850
+ }, [T]), K(() => {
1851
+ const R = async () => {
1852
+ if (document.visibilityState !== "visible") return;
1853
+ const E = c.getState();
1854
+ if (!(!E.sessionId || Date.now() - b.current < Hn))
1855
+ try {
1856
+ const O = await zn(w, E.sessionId);
1857
+ ue(c, O);
1858
+ } catch (O) {
1859
+ O instanceof he && O.status === 404 && c.setState({
1860
+ loading: !1,
1861
+ error: "Session expired. Please start over.",
1862
+ screenOrder: [],
1863
+ screens: {}
1864
+ });
1865
+ }
1866
+ };
1867
+ return document.addEventListener("visibilitychange", R), () => document.removeEventListener("visibilitychange", R);
1868
+ }, [e, o]);
1869
+ const x = z(async () => {
1870
+ const R = c.getState();
1871
+ if (!(R.loading || !R.evaluateFlowValidation())) {
1872
+ c.setState({ loading: !0 }), c.getState().clearError(), N();
1873
+ try {
1874
+ const _ = we(c), O = await $n(w, R.sessionId, _);
1875
+ ue(c, O), b.current = Date.now(), i == null || i(O.navigation.screenNumber);
1876
+ } catch (_) {
1877
+ c.setState({ loading: !1 }), C(_.message);
1878
+ }
1879
+ }
1880
+ }, [w, c, i, C, N]), M = z(async () => {
1881
+ const R = c.getState();
1882
+ if (!R.loading) {
1883
+ c.setState({ loading: !0 }), c.getState().clearError(), N();
1884
+ try {
1885
+ const E = we(c), _ = await Pn(w, R.sessionId, E);
1886
+ ue(c, _), b.current = Date.now(), i == null || i(_.navigation.screenNumber);
1887
+ } catch (E) {
1888
+ c.setState({ loading: !1 }), C(E.message);
1889
+ }
1890
+ }
1891
+ }, [w, c, i, C, N]), U = z(async () => {
1892
+ const R = c.getState();
1893
+ if (!(R.loading || !R.evaluateFlowValidation())) {
1894
+ c.setState({ loading: !0 }), c.getState().clearError(), N();
1895
+ try {
1896
+ const _ = we(c), O = await Gn(w, R.sessionId, _);
1897
+ b.current = Date.now(), s(O);
1898
+ } catch (_) {
1899
+ c.setState({ loading: !1 }), C(_.message);
1900
+ }
1901
+ }
1902
+ }, [w, c, s, C, N]), S = { ...p == null ? void 0 : p.theme, ...t }, D = S.darkMode ?? !1, B = {};
1903
+ S.color && (B["--fbre-theme-color"] = S.color);
1904
+ const G = f[0], V = G ? y[G] : void 0, $ = V == null ? void 0 : V.nextButtonLabel, J = V == null ? void 0 : V.backButtonLabel;
1905
+ return d && f.length === 0 ? /* @__PURE__ */ m("div", { className: "fbre-container", children: /* @__PURE__ */ k("div", { className: "fbre-remote-loading", children: [
1906
+ /* @__PURE__ */ m("div", { className: "fbre-spinner" }),
1907
+ /* @__PURE__ */ m("p", { children: "Loading form..." })
1908
+ ] }) }) : l && f.length === 0 ? /* @__PURE__ */ m("div", { className: "fbre-container", children: /* @__PURE__ */ k("div", { className: "fbre-remote-error", children: [
1909
+ /* @__PURE__ */ m("p", { children: "Failed to load form" }),
1910
+ /* @__PURE__ */ m("p", { className: "fbre-remote-error__detail", children: l }),
1911
+ /* @__PURE__ */ m(
1912
+ "button",
1913
+ {
1914
+ type: "button",
1915
+ className: "fbre-btn fbre-btn--next",
1916
+ onClick: T,
1917
+ style: { marginTop: 12 },
1918
+ children: "Retry"
1919
+ }
1920
+ )
1921
+ ] }) }) : f.length === 0 ? null : /* @__PURE__ */ m(
1922
+ ye.Provider,
1923
+ {
1924
+ value: c,
1925
+ children: /* @__PURE__ */ k(
1926
+ "div",
1927
+ {
1928
+ className: "fbre-container",
1929
+ "data-style": S.style ?? "clean",
1930
+ "data-mode": D ? "dark" : "light",
1931
+ style: Object.keys(B).length > 0 ? B : void 0,
1932
+ children: [
1933
+ /* @__PURE__ */ m("div", { className: "fbre-screen-wrapper", children: /* @__PURE__ */ m(Ne, { screenIndex: 0 }) }),
1934
+ /* @__PURE__ */ m(
1935
+ Xn,
1936
+ {
1937
+ navigation: u,
1938
+ loading: d,
1939
+ onPrev: M,
1940
+ onNext: x,
1941
+ onComplete: U,
1942
+ nextButtonLabel: $,
1943
+ backButtonLabel: J
1944
+ }
1945
+ ),
1946
+ /* @__PURE__ */ k("div", { className: `fbre-toast${g ? " visible" : ""}`, children: [
1947
+ /* @__PURE__ */ m("span", { children: g }),
1948
+ /* @__PURE__ */ m(
1949
+ "button",
1950
+ {
1951
+ type: "button",
1952
+ className: "fbre-toast__close",
1953
+ onClick: N,
1954
+ "aria-label": "Dismiss",
1955
+ children: "×"
1956
+ }
1957
+ )
1958
+ ] })
1959
+ ]
1960
+ }
1961
+ )
1962
+ }
1963
+ );
1964
+ }, Xn = ({
1965
+ navigation: e,
1966
+ loading: r,
1967
+ onPrev: o,
1968
+ onNext: t,
1969
+ onComplete: n,
1970
+ nextButtonLabel: s,
1971
+ backButtonLabel: i
1972
+ }) => /* @__PURE__ */ k("div", { className: "fbre-controls", children: [
1973
+ /* @__PURE__ */ m("div", { className: "fbre-controls__left", children: e.canGoBack && /* @__PURE__ */ m(
1974
+ "button",
1975
+ {
1976
+ type: "button",
1977
+ className: "fbre-btn fbre-btn--back",
1978
+ onClick: o,
1979
+ disabled: r,
1980
+ children: i || "Back"
1981
+ }
1982
+ ) }),
1983
+ /* @__PURE__ */ m("div", { className: "fbre-controls__center", children: /* @__PURE__ */ k("span", { className: "fbre-controls__progress", children: [
1984
+ e.screenNumber,
1985
+ " / ",
1986
+ e.totalScreens
1987
+ ] }) }),
1988
+ /* @__PURE__ */ m("div", { className: "fbre-controls__right", children: e.isLastScreen ? /* @__PURE__ */ m(
1989
+ "button",
1990
+ {
1991
+ type: "button",
1992
+ className: "fbre-btn fbre-btn--complete",
1993
+ onClick: n,
1994
+ disabled: r,
1995
+ children: r ? "Submitting..." : s || "Complete"
1996
+ }
1997
+ ) : /* @__PURE__ */ m(
1998
+ "button",
1999
+ {
2000
+ type: "button",
2001
+ className: "fbre-btn fbre-btn--next",
2002
+ onClick: t,
2003
+ disabled: r,
2004
+ children: r ? "Loading..." : s || "Next"
2005
+ }
2006
+ ) })
2007
+ ] });
2008
+ function Kn(e, r, o, t) {
2009
+ return !r && !o && !t ? e : {
2010
+ ...e,
2011
+ ...r && { theme: { ...e.theme, ...r } },
2012
+ ...o && { navigation: { ...e.navigation, ...o } },
2013
+ ...t && { controls: { ...e.controls, ...t } }
2014
+ };
2015
+ }
2016
+ const nn = ({
2017
+ flow: e,
2018
+ data: r,
2019
+ theme: o,
2020
+ navigation: t,
2021
+ controls: n,
2022
+ screenIndex: s,
2023
+ context: i,
2024
+ storeRef: a,
2025
+ onFlowComplete: c,
2026
+ onScreenChange: d,
2027
+ onScreenValidationChange: l,
2028
+ apiCtx: u
2029
+ }) => {
2030
+ const p = ce(null);
2031
+ p.current || (p.current = Sn(), p.current.getState().loadFlow(e, r, i)), a && (a.current = p.current), K(() => {
2032
+ p.current && p.current.getState().loadFlow(e, r, i);
2033
+ }, [e.uuid]), K(() => {
2034
+ if (p.current) {
2035
+ const g = p.current.getState(), h = Kn(g.config, o, t, n);
2036
+ h !== g.config && p.current.setState({ config: h });
2037
+ }
2038
+ }, [o, t, n]);
2039
+ const f = i ? JSON.stringify(i) : "";
2040
+ K(() => {
2041
+ p.current && i && p.current.getState().updateContext(i);
2042
+ }, [f]);
2043
+ const y = /* @__PURE__ */ m(ye.Provider, { value: p.current, children: /* @__PURE__ */ m(
2044
+ Jn,
2045
+ {
2046
+ screenIndex: s ?? 0,
2047
+ onFlowComplete: c,
2048
+ onScreenChange: d,
2049
+ onScreenValidationChange: l
2050
+ }
2051
+ ) });
2052
+ return u ? /* @__PURE__ */ m(en.Provider, { value: u, children: y }) : y;
2053
+ }, Qn = ({
2054
+ flowId: e,
2055
+ apiEndpoint: r,
2056
+ apiKey: o,
2057
+ data: t,
2058
+ theme: n,
2059
+ navigation: s,
2060
+ controls: i,
2061
+ screenIndex: a,
2062
+ context: c,
2063
+ storeRef: d,
2064
+ onFlowComplete: l,
2065
+ onScreenChange: u,
2066
+ onScreenValidationChange: p
2067
+ }) => {
2068
+ const [f, y] = P(null), [g, h] = P(""), [v, b] = P(null);
2069
+ return K(() => {
2070
+ y(null), h("");
2071
+ const w = { apiEndpoint: r, apiKey: o };
2072
+ jn(w, e).then((C) => {
2073
+ y(C.data), b({
2074
+ config: w,
2075
+ flowId: C.id,
2076
+ flowVersionId: C.versionId,
2077
+ tenantId: C.tenantId
2078
+ });
2079
+ }).catch((C) => h(C.message));
2080
+ }, [e, r, o]), g ? /* @__PURE__ */ m("div", { className: "fbre-container", children: /* @__PURE__ */ k("div", { className: "fbre-remote-error", children: [
2081
+ /* @__PURE__ */ m("p", { children: "Failed to load form" }),
2082
+ /* @__PURE__ */ m("p", { className: "fbre-remote-error__detail", children: g })
2083
+ ] }) }) : !f || !v ? /* @__PURE__ */ m("div", { className: "fbre-container", children: /* @__PURE__ */ k("div", { className: "fbre-remote-loading", children: [
2084
+ /* @__PURE__ */ m("div", { className: "fbre-spinner" }),
2085
+ /* @__PURE__ */ m("p", { children: "Loading form..." })
2086
+ ] }) }) : /* @__PURE__ */ m(
2087
+ nn,
2088
+ {
2089
+ flow: f,
2090
+ data: t,
2091
+ theme: n,
2092
+ navigation: s,
2093
+ controls: i,
2094
+ screenIndex: a,
2095
+ context: c,
2096
+ storeRef: d,
2097
+ onFlowComplete: l,
2098
+ onScreenChange: u,
2099
+ onScreenValidationChange: p,
2100
+ apiCtx: v
2101
+ }
2102
+ );
2103
+ }, ct = (e) => "sessionEndpoint" in e ? /* @__PURE__ */ m(Wn, { ...e }) : "flowId" in e && e.flowId ? /* @__PURE__ */ m(Qn, { ...e }) : /* @__PURE__ */ m(nn, { ...e });
2104
+ export {
2105
+ he as ApiError,
2106
+ ct as FBRE,
2107
+ Tn as TimeoutError,
2108
+ rt as addFBREEventListener,
2109
+ st as removeFBREEventListener,
2110
+ it as useFBREApi,
2111
+ L as useFBREStore,
2112
+ Ke as useFBREStoreApi
2113
+ };