@picked/client 0.0.50-next.2 → 0.0.50-next.4

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.
Files changed (51) hide show
  1. package/dist/api-C2lg7yBn.cjs +1 -0
  2. package/dist/api-CHaIe0uL.js +37 -0
  3. package/dist/cjs/index.js +2 -2
  4. package/dist/cjs/plugins/create-experience/index.js +1 -1
  5. package/dist/cjs/plugins/dynamic-experiences/index.js +1 -1
  6. package/dist/cjs/plugins/experience-browser-storage/index.js +1 -1
  7. package/dist/cjs/plugins/history-browser-storage/index.js +1 -1
  8. package/dist/cjs/plugins/responses-browser-storage/index.js +1 -1
  9. package/dist/cjs/plugins/responses-indexeddb/index.js +1 -1
  10. package/dist/cjs/plugins/single-page-app/index.js +1 -1
  11. package/dist/cjs/plugins/web-components/index.js +1 -1
  12. package/dist/cjs/plugins/window-object/index.js +1 -1
  13. package/dist/cjs/server/index.js +1 -1
  14. package/dist/esm/index.js +1126 -1220
  15. package/dist/esm/plugins/create-experience/index.js +461 -468
  16. package/dist/esm/plugins/dynamic-experiences/index.js +106 -123
  17. package/dist/esm/plugins/experience-browser-storage/index.js +32 -37
  18. package/dist/esm/plugins/history-browser-storage/index.js +23 -25
  19. package/dist/esm/plugins/responses-browser-storage/index.js +36 -39
  20. package/dist/esm/plugins/responses-indexeddb/index.js +81 -80
  21. package/dist/esm/plugins/single-page-app/index.js +31 -35
  22. package/dist/esm/plugins/web-components/index.js +18 -20
  23. package/dist/esm/plugins/window-object/index.js +9 -9
  24. package/dist/esm/server/index.js +95 -89
  25. package/dist/experience-instance-BRpI9I4s.js +710 -0
  26. package/dist/experience-instance-BUTnwbSJ.cjs +1 -0
  27. package/dist/id-BniPxOdF.js +4 -0
  28. package/dist/id-CIbS4P60.cjs +1 -0
  29. package/dist/logger-6z_gb4rA.js +4 -0
  30. package/dist/logger-fbvQC40v.cjs +1 -0
  31. package/dist/object-BReQiaSS.cjs +1 -0
  32. package/dist/object-Clj5zVr1.js +24 -0
  33. package/dist/storage-BFjb8azI.cjs +1 -0
  34. package/dist/storage-DyxpJ0pg.js +101 -0
  35. package/dist/supports-0XIfd4-R.js +85 -0
  36. package/dist/supports-Dsjg-82e.cjs +1 -0
  37. package/package.json +7 -7
  38. package/dist/api-6XSnv0Kv.cjs +0 -1
  39. package/dist/api-Bt6xz3mn.js +0 -32
  40. package/dist/experience-instance-BsXG5XJ4.cjs +0 -1
  41. package/dist/experience-instance-fvSEb72i.js +0 -819
  42. package/dist/id-BKIWbSTy.cjs +0 -1
  43. package/dist/id-BW-g3y32.js +0 -6
  44. package/dist/logger-BsmHUUw1.cjs +0 -1
  45. package/dist/logger-C9a75KZn.js +0 -5
  46. package/dist/object-BbFTsGh3.cjs +0 -1
  47. package/dist/object-JwZ58NOH.js +0 -28
  48. package/dist/storage-Be5xWNPC.cjs +0 -1
  49. package/dist/storage-Co_LbKr_.js +0 -239
  50. package/dist/supports-8YT4QKI7.js +0 -95
  51. package/dist/supports-YvpQZxbh.cjs +0 -1
package/dist/esm/index.js CHANGED
@@ -1,1266 +1,1172 @@
1
- import { createDomain as x, createApi as j, attach as U, createStore as T, createEffect as R, sample as k, combine as D, createEvent as V } from "effector";
2
- import { createFactory as b, invoke as I } from "@withease/factories";
3
- import { g as W, t as te, c as re, i as se, e as P, a as ne, b as ie, d as oe } from "../experience-instance-fvSEb72i.js";
4
- import { f as zt } from "../experience-instance-fvSEb72i.js";
5
- import { l as w } from "../logger-C9a75KZn.js";
6
- import { c as A, l as C, s as O } from "../storage-Co_LbKr_.js";
7
- import { a as Ot } from "../storage-Co_LbKr_.js";
8
- import { p as ae, g as ce } from "../api-Bt6xz3mn.js";
9
- import { g as ue } from "../id-BW-g3y32.js";
10
- import { i as de } from "../object-JwZ58NOH.js";
11
- import { s as L, a as _, b as le, c as pe, d as fe } from "../supports-8YT4QKI7.js";
12
- import { e as Ut, f as Dt, g as Vt, h as Nt, i as Tt, j as Lt, k as _t } from "../supports-8YT4QKI7.js";
13
- const Y = x("experience"), ge = b(
14
- ({ client: t, experience: e }) => {
15
- const r = Y.store(e), s = j(r, {
16
- merge(d, g) {
17
- return {
18
- ...d,
19
- ...g.experience
20
- };
21
- }
22
- }), n = r.map((d) => ({
23
- name: d.name,
24
- decisionLogic: d.condition,
25
- targetSelectors: d.targets,
26
- initializeOpenLogic: e.initializeOpen
27
- })), p = t.components.listStore.map(
28
- (d) => (e.componentName ? d.find((g) => g.name === e.componentName) : void 0) || d.find((g) => g.name === `experience:${e.id}`) || d.find((g) => g.name === "experience"),
29
- { skipVoid: !1 }
30
- ), a = r.map((d) => ({
31
- steps: d.steps,
32
- nodes: d.nodes,
33
- edges: d.edges,
34
- conditionals: d.conditionals
35
- })), i = r.map((d) => d.fields), c = e.actions ?? [];
36
- return {
37
- id: e.id,
38
- configStore: n,
39
- treeStore: a,
40
- fieldsStore: i,
41
- componentStore: p,
42
- actions: c,
43
- merge: s.merge
44
- };
45
- }
46
- );
47
- function me(t) {
48
- return I(ge, t);
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, u as c } from "../experience-instance-BRpI9I4s.js";
2
+ import { t as l } from "../object-Clj5zVr1.js";
3
+ import { t as u } from "../logger-6z_gb4rA.js";
4
+ import { a as d, c as f, d as p, i as ee, l as m, n as h, o as g, p as te, r as _, s as ne, t as re, u as ie } from "../supports-0XIfd4-R.js";
5
+ import { i as ae, n as v, r as y, t as b } from "../storage-DyxpJ0pg.js";
6
+ import { n as x, r as oe } from "../api-CHaIe0uL.js";
7
+ import { t as se } from "../id-BniPxOdF.js";
8
+ import { attach as S, combine as C, createApi as w, createDomain as T, createEffect as E, createEvent as D, createStore as O, sample as k } from "effector";
9
+ import { createFactory as A, invoke as j } from "@withease/factories";
10
+ //#region src/entities/experience/experience.ts
11
+ var M = T("experience"), ce = A(({ client: e, experience: t }) => {
12
+ let n = M.store(t), r = w(n, { merge(e, t) {
13
+ return {
14
+ ...e,
15
+ ...t.experience
16
+ };
17
+ } }), i = n.map((e) => ({
18
+ name: e.name,
19
+ decisionLogic: e.condition,
20
+ targetSelectors: e.targets,
21
+ initializeOpenLogic: t.initializeOpen
22
+ })), a = e.components.listStore.map((e) => (t.componentName ? e.find((e) => e.name === t.componentName) : void 0) || e.find((e) => e.name === `experience:${t.id}`) || e.find((e) => e.name === "experience"), { skipVoid: !1 }), o = n.map((e) => ({
23
+ steps: e.steps,
24
+ nodes: e.nodes,
25
+ edges: e.edges,
26
+ conditionals: e.conditionals
27
+ })), s = n.map((e) => e.fields), c = t.actions ?? [];
28
+ return {
29
+ id: t.id,
30
+ configStore: i,
31
+ treeStore: o,
32
+ fieldsStore: s,
33
+ componentStore: a,
34
+ actions: c,
35
+ merge: r.merge
36
+ };
37
+ });
38
+ function le(e) {
39
+ return j(ce, e);
49
40
  }
50
- const he = b(({ client: t }) => {
51
- const e = Y.store({}), r = j(e, {
52
- register(c, d) {
53
- return {
54
- ...c,
55
- [d.id]: d
56
- };
57
- }
58
- });
59
- function s(c) {
60
- const d = e.getState()[c.id];
61
- if (d) {
62
- d.merge({ experience: c, strategy: "override" });
63
- return;
64
- }
65
- const g = me({ client: t, experience: c });
66
- r.register(g);
67
- }
68
- const n = e.map((c) => Object.values(c));
69
- function p(c) {
70
- return e.map((d) => d[c], { skipVoid: !1 });
71
- }
72
- const a = U({
73
- source: e,
74
- effect: async (c, { id: d, options: g }) => {
75
- const S = c[d];
76
- if (!S) {
77
- w("warn", `Experience with id '${d}' not found.`);
78
- return;
79
- }
80
- if (g?.bypassCondition) {
81
- N(t, S, g);
82
- return;
83
- }
84
- await Q(t, S) && N(t, S, g);
85
- }
86
- }), i = U({
87
- source: t.experienceInstances.listStore,
88
- effect: (c, { id: d, options: g }) => {
89
- const S = c.filter((m) => m.experienceId === d);
90
- if (S.length > 0)
91
- for (const m of S)
92
- m.open();
93
- else
94
- a({ id: d, options: { ...g, initialiseOpen: !0 } });
95
- }
96
- });
97
- return {
98
- /** Registers an experience in the index. If an experience with the same ID already exists, it is merged. */
99
- register: s,
100
- /** Returns a derived store for an experience by its ID. The store value is `undefined` when no match exists. */
101
- get: p,
102
- /** @deprecated Use `experiences.get(id)` instead. */
103
- getById: p,
104
- /** Effector effect that launches an experience by ID, creating instances for each DOM target. */
105
- launch: a,
106
- /** Effector effect that opens an experience's existing instances, or launches it with `initialiseOpen` if none exist. */
107
- open: i,
108
- /** Derived store containing all registered experiences as an array. */
109
- listStore: n
110
- };
41
+ var ue = A(({ client: e }) => {
42
+ let t = M.store({}), n = w(t, { register(e, t) {
43
+ return {
44
+ ...e,
45
+ [t.id]: t
46
+ };
47
+ } });
48
+ function r(r) {
49
+ let i = t.getState()[r.id];
50
+ if (i) {
51
+ i.merge({
52
+ experience: r,
53
+ strategy: "override"
54
+ });
55
+ return;
56
+ }
57
+ let a = le({
58
+ client: e,
59
+ experience: r
60
+ });
61
+ n.register(a);
62
+ }
63
+ let i = t.map((e) => Object.values(e));
64
+ function a(e) {
65
+ return t.map((t) => t[e], { skipVoid: !1 });
66
+ }
67
+ let o = S({
68
+ source: t,
69
+ effect: async (t, { id: n, options: r }) => {
70
+ let i = t[n];
71
+ if (!i) {
72
+ `${n}`;
73
+ return;
74
+ }
75
+ if (r?.bypassCondition) {
76
+ N(e, i, r);
77
+ return;
78
+ }
79
+ await F(e, i) && N(e, i, r);
80
+ }
81
+ });
82
+ return {
83
+ register: r,
84
+ get: a,
85
+ getById: a,
86
+ launch: o,
87
+ open: S({
88
+ source: e.experienceInstances.listStore,
89
+ effect: (e, { id: t, options: n }) => {
90
+ let r = e.filter((e) => e.experienceId === t);
91
+ if (r.length > 0) for (let e of r) e.open();
92
+ else o({
93
+ id: t,
94
+ options: {
95
+ ...n,
96
+ initialiseOpen: !0
97
+ }
98
+ });
99
+ }
100
+ }),
101
+ listStore: i
102
+ };
111
103
  });
112
- function ye(t) {
113
- return I(he, t);
104
+ function de(e) {
105
+ return j(ue, e);
114
106
  }
115
- const Se = (t, e) => {
116
- e.register(t);
117
- }, N = (t, e, r) => {
118
- const { targetSelectors: s } = e.configStore.getState(), n = W(s, e.id);
119
- n.forEach(
120
- ({ elements: a }) => a.forEach((i) => te(i, e.id))
121
- ), re(n, e.id).forEach((a) => {
122
- r ? t.experienceInstances.spawn(e, a, r) : t.experienceInstances.spawn(e, a);
123
- });
124
- }, H = async (t) => {
125
- await Promise.all(
126
- t.experiences.listStore.getState().map(async (e) => {
127
- await Q(t, e) && N(t, e);
128
- })
129
- );
130
- }, Q = async (t, e) => {
131
- if (!t.preconditionPassed || !t.session?.isUser)
132
- return !1;
133
- const { decisionLogic: r, targetSelectors: s } = e.configStore.getState();
134
- return W(s, e.id).length < 1 || s.length === 0 ? !1 : r === void 0 ? !0 : se(r) ? await P(t, r) === !0 : !0;
135
- }, ve = (t, e, r) => {
136
- const s = e.name, n = e.setup(t);
137
- if (n) {
138
- const p = r[s] ?? {};
139
- r[s] = Object.freeze({ ...p, ...n });
140
- }
141
- }, ke = x("component"), we = b(
142
- ({ client: t, component: e, name: r }) => {
143
- const s = t.frameworks.getByName(e.frameworkName);
144
- return {
145
- name: r,
146
- component: e,
147
- frameworkStore: s
148
- };
149
- }
150
- );
151
- function be(t) {
152
- return I(we, t);
107
+ var fe = (e, t) => {
108
+ t.register(e);
109
+ }, N = (e, r, i) => {
110
+ let { targetSelectors: o } = r.configStore.getState(), s = a(o, r.id);
111
+ s.forEach(({ elements: e }) => e.forEach((e) => t(e, r.id))), n(s, r.id).forEach((t) => {
112
+ i ? e.experienceInstances.spawn(r, t, i) : e.experienceInstances.spawn(r, t);
113
+ });
114
+ }, P = async (e) => {
115
+ await Promise.all(e.experiences.listStore.getState().map(async (t) => {
116
+ await F(e, t) && N(e, t);
117
+ }));
118
+ }, F = async (e, t) => {
119
+ if (!e.preconditionPassed || !e.session?.isUser) return !1;
120
+ let { decisionLogic: n, targetSelectors: i } = t.configStore.getState();
121
+ return a(i, t.id).length < 1 || i.length === 0 ? !1 : n === void 0 ? !0 : c(n) ? await r(e, n) === !0 : !0;
122
+ }, I = (e, t, n) => {
123
+ let r = t.name, i = t.setup(e);
124
+ if (i) {
125
+ let e = n[r] ?? {};
126
+ n[r] = Object.freeze({
127
+ ...e,
128
+ ...i
129
+ });
130
+ }
131
+ }, L = T("component"), R = A(({ client: e, component: t, name: n }) => ({
132
+ name: n,
133
+ component: t,
134
+ frameworkStore: e.frameworks.getByName(t.frameworkName)
135
+ }));
136
+ function z(e) {
137
+ return j(R, e);
153
138
  }
154
- const Ie = b(({ client: t }) => {
155
- const e = ke.store({}), r = j(e, {
156
- register(a, i) {
157
- return {
158
- ...a,
159
- [i.name]: i
160
- };
161
- }
162
- });
163
- function s(a, i) {
164
- e.getState()[a] && w(
165
- "warn",
166
- `Component '${a}' already registered. Registering again will override the previous registration.`
167
- );
168
- const d = be({ client: t, name: a, component: i });
169
- r.register(d);
170
- }
171
- const n = e.map((a) => Object.values(a));
172
- function p(a) {
173
- return e.map((i) => i[a], { skipVoid: !1 });
174
- }
175
- return {
176
- register: s,
177
- getByName: p,
178
- listStore: n
179
- };
139
+ var B = A(({ client: e }) => {
140
+ let t = L.store({}), n = w(t, { register(e, t) {
141
+ return {
142
+ ...e,
143
+ [t.name]: t
144
+ };
145
+ } });
146
+ function r(r, i) {
147
+ t.getState()[r] && `${r}`;
148
+ let a = z({
149
+ client: e,
150
+ name: r,
151
+ component: i
152
+ });
153
+ n.register(a);
154
+ }
155
+ let i = t.map((e) => Object.values(e));
156
+ function a(e) {
157
+ return t.map((t) => t[e], { skipVoid: !1 });
158
+ }
159
+ return {
160
+ register: r,
161
+ getByName: a,
162
+ listStore: i
163
+ };
180
164
  });
181
- function Fe(t) {
182
- return I(Ie, t);
165
+ function V(e) {
166
+ return j(B, e);
183
167
  }
184
- const G = (t, e, r) => {
185
- r.register(t, e);
186
- }, xe = x("framework"), je = b(({ client: t }) => {
187
- const e = xe.store({}), r = j(e, {
188
- register(a, i) {
189
- return {
190
- ...a,
191
- [i.name]: i.framework
192
- };
193
- }
194
- });
195
- function s(a, i) {
196
- e.getState()[a] && w(
197
- "warn",
198
- `Framework '${a}' already registered. Registering again will override the previous registration.`
199
- ), r.register({ name: a, framework: i });
200
- }
201
- const n = e.map((a) => Object.values(a));
202
- function p(a) {
203
- return e.map((i) => i[a], { skipVoid: !1 });
204
- }
205
- return {
206
- register: s,
207
- getByName: p,
208
- listStore: n
209
- };
168
+ var H = (e, t, n) => {
169
+ n.register(e, t);
170
+ }, pe = T("framework"), me = A(({ client: e }) => {
171
+ let t = pe.store({}), n = w(t, { register(e, t) {
172
+ return {
173
+ ...e,
174
+ [t.name]: t.framework
175
+ };
176
+ } });
177
+ function r(e, r) {
178
+ t.getState()[e] && `${e}`, n.register({
179
+ name: e,
180
+ framework: r
181
+ });
182
+ }
183
+ let i = t.map((e) => Object.values(e));
184
+ function a(e) {
185
+ return t.map((t) => t[e], { skipVoid: !1 });
186
+ }
187
+ return {
188
+ register: r,
189
+ getByName: a,
190
+ listStore: i
191
+ };
210
192
  });
211
- function Ee(t) {
212
- return I(je, t);
193
+ function he(e) {
194
+ return j(me, e);
213
195
  }
214
- const Pe = (t, e) => {
215
- e.register(t.name, t);
216
- }, Ae = 6e3, $e = A("last_ping", "global"), Be = async (t) => {
217
- try {
218
- const e = Date.now(), r = C.get($e);
219
- let s = 0;
220
- if (r.isSet() && (s = r.json() ?? 0), s && e - s < Ae)
221
- return !0;
222
- const n = {
223
- projectId: t.projectId,
224
- path: window.location.pathname
225
- }, p = {}, i = document.getElementById("picked_script")?.dataset.verceltoken;
226
- i && (p["x-vercel-protection-bypass"] = i);
227
- const d = (await ae(t.apiUrl, "ping", n, p)).ok;
228
- return d && r.set(e), d;
229
- } catch (e) {
230
- return w("error", e), !1;
231
- }
196
+ var ge = (e, t) => {
197
+ t.register(e.name, e);
198
+ }, _e = 6e3, ve = b("last_ping", "global"), ye = async (e) => {
199
+ try {
200
+ let t = Date.now(), n = v.get(ve), r = 0;
201
+ if (n.isSet() && (r = n.json() ?? 0), r && t - r < _e) return !0;
202
+ let i = {
203
+ projectId: e.projectId,
204
+ path: window.location.pathname
205
+ }, a = {}, o = document.getElementById("picked_script")?.dataset.verceltoken;
206
+ o && (a["x-vercel-protection-bypass"] = o);
207
+ let s = (await x(e.apiUrl, "ping", i, a)).ok;
208
+ return s && n.set(t), s;
209
+ } catch {
210
+ return !1;
211
+ }
232
212
  };
233
- function X(t) {
234
- const e = typeof window < "u", r = {
235
- client: t,
236
- responses: t.responses.listStore.getState(),
237
- fields: t.fields.listStore.getState(),
238
- location: e ? window.location : void 0,
239
- document: e ? window.document : void 0,
240
- sessionStorage: e ? window.sessionStorage : void 0,
241
- localStorage: e ? window.localStorage : void 0
242
- };
243
- return Object.freeze(r);
213
+ //#endregion
214
+ //#region src/utils/state-snaphot.ts
215
+ function U(e) {
216
+ let t = typeof window < "u", n = {
217
+ client: e,
218
+ responses: e.responses.listStore.getState(),
219
+ fields: e.fields.listStore.getState(),
220
+ location: t ? window.location : void 0,
221
+ document: t ? window.document : void 0,
222
+ sessionStorage: t ? window.sessionStorage : void 0,
223
+ localStorage: t ? window.localStorage : void 0
224
+ };
225
+ return Object.freeze(n);
244
226
  }
245
- const Re = x("predicate"), ze = b(({ client: t }) => {
246
- const e = Re.store({}), r = j(e, {
247
- register(i, c) {
248
- return {
249
- ...i,
250
- [c.name]: {
251
- name: c.name,
252
- predicate: c.predicate
253
- }
254
- };
255
- }
256
- });
257
- function s(i, c) {
258
- e.getState()[i] && w(
259
- "warn",
260
- `Predicate '${i}' already registered. Registering again will override the previous registration.`
261
- ), r.register(Object.freeze({ name: i, predicate: c }));
262
- }
263
- const n = e.map((i) => Object.values(i));
264
- function p(i) {
265
- return e.map((c) => c[i]?.predicate, { skipVoid: !1 });
266
- }
267
- function a(i) {
268
- const c = T(() => {
269
- }, {
270
- skipVoid: !1
271
- }), d = R(async () => {
272
- const g = p(i).getState();
273
- if (!g)
274
- return;
275
- const S = X(t);
276
- return (...m) => g({ ...S, args: m });
277
- });
278
- return k({
279
- source: { responses: t.responses.listStore, fields: t.fields.listStore },
280
- target: d
281
- }), k({
282
- clock: d.doneData,
283
- filter: (g) => g !== void 0,
284
- target: c
285
- }), d(), c;
286
- }
287
- return {
288
- register: s,
289
- getByName: p,
290
- listStore: n,
291
- getEvokablePredicate: a
292
- };
227
+ //#endregion
228
+ //#region src/entities/predicate/predicate.ts
229
+ var be = T("predicate"), xe = A(({ client: e }) => {
230
+ let t = be.store({}), n = w(t, { register(e, t) {
231
+ return {
232
+ ...e,
233
+ [t.name]: {
234
+ name: t.name,
235
+ predicate: t.predicate
236
+ }
237
+ };
238
+ } });
239
+ function r(e, r) {
240
+ t.getState()[e] && `${e}`, n.register(Object.freeze({
241
+ name: e,
242
+ predicate: r
243
+ }));
244
+ }
245
+ let i = t.map((e) => Object.values(e));
246
+ function a(e) {
247
+ return t.map((t) => t[e]?.predicate, { skipVoid: !1 });
248
+ }
249
+ function o(t) {
250
+ let n = O(() => void 0, { skipVoid: !1 }), r = E(async () => {
251
+ let n = a(t).getState();
252
+ if (!n) return;
253
+ let r = U(e);
254
+ return (...e) => n({
255
+ ...r,
256
+ args: e
257
+ });
258
+ });
259
+ return k({
260
+ source: {
261
+ responses: e.responses.listStore,
262
+ fields: e.fields.listStore
263
+ },
264
+ target: r
265
+ }), k({
266
+ clock: r.doneData,
267
+ filter: (e) => e !== void 0,
268
+ target: n
269
+ }), r(), n;
270
+ }
271
+ return {
272
+ register: r,
273
+ getByName: a,
274
+ listStore: i,
275
+ getEvokablePredicate: o
276
+ };
293
277
  });
294
- function Ce(t) {
295
- return I(ze, t);
278
+ function Se(e) {
279
+ return j(xe, e);
296
280
  }
297
- const Oe = (t, e, r) => {
298
- r.register(t, e);
299
- }, J = x("feature"), Me = b(({ client: t }) => {
300
- const e = J.store({}), r = J.store([]), s = j(e, {
301
- register(o, u) {
302
- return {
303
- ...o,
304
- [u.id]: u.feature
305
- };
306
- }
307
- });
308
- function n(o, u) {
309
- e.getState()[o] && w(
310
- "warn",
311
- `Feature '${o}' already registered. Registering again will override the previous registration.`
312
- );
313
- const l = {
314
- id: o,
315
- feature: {
316
- ...u,
317
- id: o
318
- }
319
- };
320
- s.register(
321
- Object.freeze({
322
- id: o,
323
- feature: l
324
- })
325
- );
326
- }
327
- const p = R(
328
- async ({
329
- id: o,
330
- key: u,
331
- storage: f = "local"
332
- }) => {
333
- const l = A(`feature:${o}:${u}`, "global"), y = (f === "session" ? O : C).get(l).boolean();
334
- return y === null ? null : { id: o, key: u, bucket: y, storage: f };
335
- }
336
- ), a = R((o) => {
337
- if (!o)
338
- return null;
339
- const u = A(`feature:${o.id}:${o.key}`, "global");
340
- (o.storage === "session" ? O : C).set(u, o.bucket);
341
- }), i = /* @__PURE__ */ new Map(), c = U({
342
- source: { allFeaturesStore: e, resolvedFeaturesStore: r },
343
- effect: async ({ allFeaturesStore: o, resolvedFeaturesStore: u }, { id: f, decide: l, key: h }) => {
344
- const y = u.find(
345
- (B) => B.id === f && B.key === h
346
- );
347
- if (y)
348
- return y;
349
- const F = o[f];
350
- if (!F)
351
- throw new Error(`Feature with id '${f}' not found`);
352
- const $ = i.get(`${f}:${h}`);
353
- if ($ !== void 0) {
354
- const B = await $;
355
- return { id: F.id, bucket: B, key: h, storage: F.feature.storage };
356
- }
357
- const v = l();
358
- i.set(`${f}:${h}`, v);
359
- const ee = await v;
360
- return { id: F.id, bucket: ee, key: h, storage: F.feature.storage };
361
- }
362
- });
363
- k({
364
- clock: s.register,
365
- fn: (o) => {
366
- const { id: u, feature: f } = o;
367
- return { id: u, key: f.feature.key, storage: f.feature.storage };
368
- },
369
- target: p
370
- }), k({
371
- source: e,
372
- clock: c.doneData,
373
- fn: (o, u) => {
374
- const { id: f, bucket: l, storage: h } = u, y = o[f]?.feature.key;
375
- return y ? { key: y, id: f, bucket: l, storage: h } : null;
376
- },
377
- target: a
378
- });
379
- const d = D({ allFeaturesStore: e, resolvedFeaturesStore: r }).map(
380
- ({
381
- allFeaturesStore: o,
382
- resolvedFeaturesStore: u
383
- }) => Object.values(o).map((f) => {
384
- const l = u.find(
385
- (h) => h.id === f.id && h.key === f.feature.key
386
- );
387
- return l ? {
388
- id: f.id,
389
- name: f.feature.name,
390
- key: f.feature.key,
391
- getBucket: () => ({ id: f.id, bucket: l.bucket }),
392
- isResolved: !0
393
- } : {
394
- id: f.id,
395
- name: f.feature.name,
396
- key: f.feature.key,
397
- getBucket: async () => await c(f.feature),
398
- isResolved: !1
399
- };
400
- })
401
- );
402
- k({
403
- clock: c.doneData,
404
- source: { resolvedFeatures: r },
405
- filter: ({ resolvedFeatures: o }, { id: u, key: f }) => !o.some(
406
- (h) => h.id === u && h.key === f
407
- ),
408
- fn: (o, u) => u,
409
- target: t.eventBus.prepend((o) => ({
410
- type: "feature_decide",
411
- feature: o
412
- }))
413
- }), k({
414
- clock: [p.doneData, c.doneData],
415
- source: r,
416
- fn: (o, u) => !u || o.some(
417
- (l) => l.id === u.id && l.key === u.key
418
- ) ? o : [...o, u],
419
- target: r
420
- });
421
- function g(o) {
422
- return d.map((u) => u.find((f) => f.id === o));
423
- }
424
- function S(o) {
425
- return e.map((u) => u[o], { skipVoid: !1 });
426
- }
427
- function m(o) {
428
- const u = T(
429
- async () => {
430
- },
431
- { skipVoid: !1 }
432
- ), f = R(async () => {
433
- const l = g(o).getState();
434
- if (l)
435
- return async () => (await l.getBucket()).bucket;
436
- });
437
- return k({
438
- source: r,
439
- filter: (l) => l.some((h) => h.id === o),
440
- target: f
441
- }), k({
442
- clock: f.doneData,
443
- filter: (l) => l !== void 0,
444
- target: u
445
- }), f(), u;
446
- }
447
- return {
448
- register: n,
449
- getById: g,
450
- getByName: S,
451
- listStore: d,
452
- getEvokableFeature: m
453
- };
281
+ var Ce = (e, t, n) => {
282
+ n.register(e, t);
283
+ }, W = T("feature"), we = A(({ client: e }) => {
284
+ let t = W.store({}), n = W.store([]), r = w(t, { register(e, t) {
285
+ return {
286
+ ...e,
287
+ [t.id]: t.feature
288
+ };
289
+ } });
290
+ function i(e, n) {
291
+ t.getState()[e] && `${e}`;
292
+ let i = {
293
+ id: e,
294
+ feature: {
295
+ ...n,
296
+ id: e
297
+ }
298
+ };
299
+ r.register(Object.freeze({
300
+ id: e,
301
+ feature: i
302
+ }));
303
+ }
304
+ let a = E(async ({ id: e, key: t, storage: n = "local" }) => {
305
+ let r = b(`feature:${e}:${t}`, "global"), i = (n === "session" ? y : v).get(r).boolean();
306
+ return i === null ? null : {
307
+ id: e,
308
+ key: t,
309
+ bucket: i,
310
+ storage: n
311
+ };
312
+ }), o = E((e) => {
313
+ if (!e) return null;
314
+ let t = b(`feature:${e.id}:${e.key}`, "global");
315
+ (e.storage === "session" ? y : v).set(t, e.bucket);
316
+ }), s = /* @__PURE__ */ new Map(), c = S({
317
+ source: {
318
+ allFeaturesStore: t,
319
+ resolvedFeaturesStore: n
320
+ },
321
+ effect: async ({ allFeaturesStore: e, resolvedFeaturesStore: t }, { id: n, decide: r, key: i }) => {
322
+ let a = t.find((e) => e.id === n && e.key === i);
323
+ if (a) return a;
324
+ let o = e[n];
325
+ if (!o) throw Error(`Feature with id '${n}' not found`);
326
+ let c = s.get(`${n}:${i}`);
327
+ if (c !== void 0) {
328
+ let e = await c;
329
+ return {
330
+ id: o.id,
331
+ bucket: e,
332
+ key: i,
333
+ storage: o.feature.storage
334
+ };
335
+ }
336
+ let l = r();
337
+ s.set(`${n}:${i}`, l);
338
+ let u = await l;
339
+ return {
340
+ id: o.id,
341
+ bucket: u,
342
+ key: i,
343
+ storage: o.feature.storage
344
+ };
345
+ }
346
+ });
347
+ k({
348
+ clock: r.register,
349
+ fn: (e) => {
350
+ let { id: t, feature: n } = e;
351
+ return {
352
+ id: t,
353
+ key: n.feature.key,
354
+ storage: n.feature.storage
355
+ };
356
+ },
357
+ target: a
358
+ }), k({
359
+ source: t,
360
+ clock: c.doneData,
361
+ fn: (e, t) => {
362
+ let { id: n, bucket: r, storage: i } = t, a = e[n]?.feature.key;
363
+ return a ? {
364
+ key: a,
365
+ id: n,
366
+ bucket: r,
367
+ storage: i
368
+ } : null;
369
+ },
370
+ target: o
371
+ });
372
+ let l = C({
373
+ allFeaturesStore: t,
374
+ resolvedFeaturesStore: n
375
+ }).map(({ allFeaturesStore: e, resolvedFeaturesStore: t }) => Object.values(e).map((e) => {
376
+ let n = t.find((t) => t.id === e.id && t.key === e.feature.key);
377
+ return n ? {
378
+ id: e.id,
379
+ name: e.feature.name,
380
+ key: e.feature.key,
381
+ getBucket: () => ({
382
+ id: e.id,
383
+ bucket: n.bucket
384
+ }),
385
+ isResolved: !0
386
+ } : {
387
+ id: e.id,
388
+ name: e.feature.name,
389
+ key: e.feature.key,
390
+ getBucket: async () => await c(e.feature),
391
+ isResolved: !1
392
+ };
393
+ }));
394
+ k({
395
+ clock: c.doneData,
396
+ source: { resolvedFeatures: n },
397
+ filter: ({ resolvedFeatures: e }, { id: t, key: n }) => !e.some((e) => e.id === t && e.key === n),
398
+ fn: (e, t) => t,
399
+ target: e.eventBus.prepend((e) => ({
400
+ type: "feature_decide",
401
+ feature: e
402
+ }))
403
+ }), k({
404
+ clock: [a.doneData, c.doneData],
405
+ source: n,
406
+ fn: (e, t) => !t || e.some((e) => e.id === t.id && e.key === t.key) ? e : [...e, t],
407
+ target: n
408
+ });
409
+ function u(e) {
410
+ return l.map((t) => t.find((t) => t.id === e));
411
+ }
412
+ function d(e) {
413
+ return t.map((t) => t[e], { skipVoid: !1 });
414
+ }
415
+ function f(e) {
416
+ let t = O(async () => void 0, { skipVoid: !1 }), r = E(async () => {
417
+ let t = u(e).getState();
418
+ if (t) return async () => (await t.getBucket()).bucket;
419
+ });
420
+ return k({
421
+ source: n,
422
+ filter: (t) => t.some((t) => t.id === e),
423
+ target: r
424
+ }), k({
425
+ clock: r.doneData,
426
+ filter: (e) => e !== void 0,
427
+ target: t
428
+ }), r(), t;
429
+ }
430
+ return {
431
+ register: i,
432
+ getById: u,
433
+ getByName: d,
434
+ listStore: l,
435
+ getEvokableFeature: f
436
+ };
454
437
  });
455
- function Ue(t) {
456
- return I(Me, t);
438
+ function Te(e) {
439
+ return j(we, e);
457
440
  }
458
- const De = (t, e, r) => {
459
- r.register(t, e);
460
- }, E = x("history");
461
- function q(t, e) {
462
- return {
463
- // eslint-disable-next-line sonarjs/pseudo-random
464
- id: ue(`history-entry-${Date.now()}-${Math.random()}}`),
465
- node: e,
466
- at: Date.now(),
467
- experienceId: t
468
- };
441
+ var Ee = (e, t, n) => {
442
+ n.register(e, t);
443
+ }, G = T("history");
444
+ function K(e, t) {
445
+ return {
446
+ id: se(`history-entry-${Date.now()}-${Math.random()}}`),
447
+ node: t,
448
+ at: Date.now(),
449
+ experienceId: e
450
+ };
469
451
  }
470
- const Ve = b(() => {
471
- const t = E.store({ entries: [] }), e = E.event(), r = E.event(), s = E.event(), n = E.event();
472
- t.on(e, (a, i) => ({
473
- entries: [...a.entries, q(i.experienceId, i.node)]
474
- })), t.on(r, (a, i) => ({
475
- entries: [
476
- ...a.entries.slice(0, -1),
477
- q(i.experienceId, i.node)
478
- ]
479
- })), t.on(n, (a, i) => i);
480
- function p(a, i) {
481
- const c = { lastStepIndex: -1, secondLastStepIndex: -1 };
482
- for (let d = a.length - 1; d >= 0; d--) {
483
- const g = a[d], S = g?.node;
484
- if (!(!S || !ne(S)) && !(i && g?.experienceId !== i))
485
- if (c.lastStepIndex === -1)
486
- c.lastStepIndex = d;
487
- else {
488
- c.secondLastStepIndex = d;
489
- break;
490
- }
491
- }
492
- return c;
493
- }
494
- return t.on(s, (a, i) => {
495
- const { secondLastStepIndex: c } = p(a.entries, i.experienceId), d = c !== -1 ? a.entries.slice(0, c + 1) : a.entries;
496
- return {
497
- ...a,
498
- entries: d
499
- };
500
- }), {
501
- push: e,
502
- replace: r,
503
- back: s,
504
- restore: n,
505
- entriesStore: t.map((a) => a.entries)
506
- };
452
+ var De = A(() => {
453
+ let e = G.store({ entries: [] }), t = G.event(), n = G.event(), r = G.event(), i = G.event();
454
+ e.on(t, (e, t) => ({ entries: [...e.entries, K(t.experienceId, t.node)] })), e.on(n, (e, t) => ({ entries: [...e.entries.slice(0, -1), K(t.experienceId, t.node)] })), e.on(i, (e, t) => t);
455
+ function a(e, t) {
456
+ let n = {
457
+ lastStepIndex: -1,
458
+ secondLastStepIndex: -1
459
+ };
460
+ for (let r = e.length - 1; r >= 0; r--) {
461
+ let i = e[r], a = i?.node;
462
+ if (!(!a || !o(a)) && !(t && i?.experienceId !== t)) if (n.lastStepIndex === -1) n.lastStepIndex = r;
463
+ else {
464
+ n.secondLastStepIndex = r;
465
+ break;
466
+ }
467
+ }
468
+ return n;
469
+ }
470
+ return e.on(r, (e, t) => {
471
+ let { secondLastStepIndex: n } = a(e.entries, t.experienceId), r = n === -1 ? e.entries : e.entries.slice(0, n + 1);
472
+ return {
473
+ ...e,
474
+ entries: r
475
+ };
476
+ }), {
477
+ push: t,
478
+ replace: n,
479
+ back: r,
480
+ restore: i,
481
+ entriesStore: e.map((e) => e.entries)
482
+ };
507
483
  });
508
- function Ne() {
509
- return I(Ve);
484
+ function Oe() {
485
+ return j(De);
510
486
  }
511
- let Te = 0;
512
- const Le = (t) => {
513
- const e = T([]);
514
- return k({
515
- clock: t,
516
- source: e,
517
- fn: (r, s) => [
518
- ...r,
519
- {
520
- id: Te++,
521
- at: /* @__PURE__ */ new Date(),
522
- payload: s
523
- }
524
- ],
525
- target: e
526
- }), e;
527
- }, _e = x("field"), He = b(() => {
528
- const t = _e.store({}), e = j(t, {
529
- register(i, c) {
530
- return {
531
- ...i,
532
- [c.field.id]: c.field
533
- };
534
- }
535
- });
536
- function r(i) {
537
- e.register({ field: i });
538
- }
539
- function s(i, c) {
540
- const d = p(i).getState();
541
- if (!d)
542
- return;
543
- const g = Object.assign({}, d, {
544
- ...c
545
- });
546
- e.register({ field: { ...g } });
547
- }
548
- const n = t.map((i) => Object.values(i));
549
- function p(i) {
550
- return t.map((c) => c[i] || null);
551
- }
552
- function a(i) {
553
- return t.map((c) => Object.values(c).find((d) => d.name === i) || null);
554
- }
555
- return {
556
- register: r,
557
- update: s,
558
- getById: p,
559
- listStore: n,
560
- getByName: a
561
- };
487
+ //#endregion
488
+ //#region src/utils/events.ts
489
+ var ke = 0, Ae = (e) => {
490
+ let t = O([]);
491
+ return k({
492
+ clock: e,
493
+ source: t,
494
+ fn: (e, t) => [...e, {
495
+ id: ke++,
496
+ at: /* @__PURE__ */ new Date(),
497
+ payload: t
498
+ }],
499
+ target: t
500
+ }), t;
501
+ }, je = T("field"), Me = A(() => {
502
+ let e = je.store({}), t = w(e, { register(e, t) {
503
+ return {
504
+ ...e,
505
+ [t.field.id]: t.field
506
+ };
507
+ } });
508
+ function n(e) {
509
+ t.register({ field: e });
510
+ }
511
+ function r(e, n) {
512
+ let r = a(e).getState();
513
+ if (!r) return;
514
+ let i = Object.assign({}, r, { ...n });
515
+ t.register({ field: { ...i } });
516
+ }
517
+ let i = e.map((e) => Object.values(e));
518
+ function a(t) {
519
+ return e.map((e) => e[t] || null);
520
+ }
521
+ function o(t) {
522
+ return e.map((e) => Object.values(e).find((e) => e.name === t) || null);
523
+ }
524
+ return {
525
+ register: n,
526
+ update: r,
527
+ getById: a,
528
+ listStore: i,
529
+ getByName: o
530
+ };
562
531
  });
563
- function Ge() {
564
- return I(He);
532
+ function Ne() {
533
+ return j(Me);
565
534
  }
566
- const Je = (t, e) => {
567
- e.register(t);
568
- }, qe = x("action"), Ke = b(() => {
569
- const t = qe.store({}), e = j(t, {
570
- register(a, i) {
571
- return {
572
- ...a,
573
- [i.name]: {
574
- name: i.name,
575
- action: i.action
576
- }
577
- };
578
- }
579
- });
580
- function r(a, i) {
581
- e.register({ name: a, action: i });
582
- }
583
- const s = t.map((a) => Object.values(a));
584
- function n(a) {
585
- return t.map((i) => i[a], { skipVoid: !1 });
586
- }
587
- function p(a) {
588
- return t.map(
589
- (i) => {
590
- const c = i[a];
591
- return c ? c.action : void 0;
592
- },
593
- { skipVoid: !1 }
594
- );
595
- }
596
- return {
597
- register: r,
598
- getByName: n,
599
- listStore: s,
600
- getEvokableAction: p
601
- };
535
+ var Pe = (e, t) => {
536
+ t.register(e);
537
+ }, Fe = T("action"), Ie = A(() => {
538
+ let e = Fe.store({}), t = w(e, { register(e, t) {
539
+ return {
540
+ ...e,
541
+ [t.name]: {
542
+ name: t.name,
543
+ action: t.action
544
+ }
545
+ };
546
+ } });
547
+ function n(e, n) {
548
+ t.register({
549
+ name: e,
550
+ action: n
551
+ });
552
+ }
553
+ let r = e.map((e) => Object.values(e));
554
+ function i(t) {
555
+ return e.map((e) => e[t], { skipVoid: !1 });
556
+ }
557
+ function a(t) {
558
+ return e.map((e) => {
559
+ let n = e[t];
560
+ return n ? n.action : void 0;
561
+ }, { skipVoid: !1 });
562
+ }
563
+ return {
564
+ register: n,
565
+ getByName: i,
566
+ listStore: r,
567
+ getEvokableAction: a
568
+ };
602
569
  });
603
- function We() {
604
- return I(Ke);
570
+ function Le() {
571
+ return j(Ie);
605
572
  }
606
- const Ye = (t, e, r) => {
607
- r.register(t, e);
573
+ var Re = (e, t, n) => {
574
+ n.register(e, t);
608
575
  };
609
- function M(t) {
610
- return typeof t > "u" || t === null || t === "" || Array.isArray(t) && !t.length;
576
+ //#endregion
577
+ //#region src/entities/response/response.ts
578
+ function q(e) {
579
+ return e == null || e === "" || Array.isArray(e) && !e.length;
611
580
  }
612
- function Qe(t) {
613
- return typeof t == "string" || typeof t == "number" || typeof t == "boolean" || t === null;
581
+ function ze(e) {
582
+ return typeof e == "string" || typeof e == "number" || typeof e == "boolean" || e === null;
614
583
  }
615
- function K(t) {
616
- try {
617
- return JSON.stringify(t), !0;
618
- } catch {
619
- return !1;
620
- }
584
+ function J(e) {
585
+ try {
586
+ return JSON.stringify(e), !0;
587
+ } catch {
588
+ return !1;
589
+ }
621
590
  }
622
- const Xe = x("response"), Ze = b(({ client: t }) => {
623
- const e = Xe.store({}), r = V(), s = V(), n = j(e, {
624
- register(m, o) {
625
- return {
626
- ...m,
627
- [o.fieldId]: o.response
628
- };
629
- },
630
- registerBatch(m, o) {
631
- return {
632
- ...m,
633
- ...o
634
- };
635
- },
636
- clear(m, o) {
637
- const { [o]: u, ...f } = m;
638
- return f;
639
- },
640
- clearAll() {
641
- return {};
642
- }
643
- });
644
- function p(m, o) {
645
- if (typeof m == "string") {
646
- if (o === void 0)
647
- throw new Error("Response is required when registering a single field");
648
- n.register({ fieldId: m, response: o });
649
- } else
650
- n.registerBatch(m);
651
- }
652
- async function a(m) {
653
- const o = [];
654
- return await Promise.allSettled(
655
- Object.entries(m).map(async ([u, f]) => {
656
- let l = f;
657
- const h = t.fields.getById(u).getState();
658
- if (!h) {
659
- o.push({
660
- fieldId: u,
661
- userMessage: `Field '${u}' not found`,
662
- systemMessage: `No field with ID '${u}' exists. Available field IDs are: ${Object.keys(t.fields.listStore.getState()).join(",")}.`,
663
- value: l
664
- });
665
- return;
666
- }
667
- if (h.multiple && (typeof l == "string" || typeof l == "number" || typeof l == "boolean") && (l = [l]), h.validation && !de(h.validation) && !M(l)) {
668
- const y = await P(t, h.validation, {
669
- $input: l
670
- });
671
- if (typeof y == "string") {
672
- o.push({
673
- fieldId: u,
674
- userMessage: y,
675
- systemMessage: `Field validation resulted in an error: ${y}.`,
676
- value: l
677
- });
678
- return;
679
- }
680
- if (y === !1) {
681
- o.push({
682
- fieldId: u,
683
- userMessage: "This field is invalid",
684
- systemMessage: "Field validation resulted in an unspecified error.",
685
- value: l
686
- });
687
- return;
688
- }
689
- }
690
- if (h.required && M(l)) {
691
- o.push({
692
- fieldId: u,
693
- userMessage: "This field is required",
694
- systemMessage: "Field is required.",
695
- value: l
696
- });
697
- return;
698
- }
699
- if (!h.multiple && Array.isArray(l) && l.length > 1) {
700
- o.push({
701
- fieldId: u,
702
- userMessage: "This field expects a single response",
703
- systemMessage: "Field expects a single response.",
704
- value: l
705
- });
706
- return;
707
- }
708
- if (h.type === "object" && !M(l))
709
- if (h.multiple) {
710
- if (!Array.isArray(l)) {
711
- o.push({
712
- fieldId: u,
713
- userMessage: "This field expects multiple values",
714
- systemMessage: "Field expects an array of objects.",
715
- value: l
716
- });
717
- return;
718
- }
719
- for (const y of l) {
720
- if (typeof y != "object" || y === null) {
721
- o.push({
722
- fieldId: u,
723
- userMessage: "This field expects object values",
724
- systemMessage: "Field expects an array of objects, but received non-object value.",
725
- value: l
726
- });
727
- return;
728
- }
729
- if (!K(y)) {
730
- o.push({
731
- fieldId: u,
732
- userMessage: "This field contains non-serializable data",
733
- systemMessage: "Field value contains non-JSON-serializable data (functions, circular references, etc.).",
734
- value: l
735
- });
736
- return;
737
- }
738
- }
739
- } else {
740
- if (Array.isArray(l)) {
741
- o.push({
742
- fieldId: u,
743
- userMessage: "This field expects a single value",
744
- systemMessage: "Field expects a single object, not an array.",
745
- value: l
746
- });
747
- return;
748
- }
749
- if (typeof l != "object" || l === null) {
750
- o.push({
751
- fieldId: u,
752
- userMessage: "This field expects an object value",
753
- systemMessage: "Field expects an object value.",
754
- value: l
755
- });
756
- return;
757
- }
758
- if (!K(l)) {
759
- o.push({
760
- fieldId: u,
761
- userMessage: "This field contains non-serializable data",
762
- systemMessage: "Field value contains non-JSON-serializable data (functions, circular references, etc.).",
763
- value: l
764
- });
765
- return;
766
- }
767
- }
768
- if (Array.isArray(h.options) && h.options.length > 0) {
769
- const y = Array.isArray(l) ? l : [l];
770
- if (h.type === "object") {
771
- const F = h.options.map((v) => JSON.stringify(v.value));
772
- y.every((v) => F.includes(JSON.stringify(v))) || o.push({
773
- fieldId: u,
774
- userMessage: "This field expects a valid option",
775
- systemMessage: "Field expects a valid option from the defined choices.",
776
- value: l
777
- });
778
- } else {
779
- const F = h.options.map((v) => v.value);
780
- y.every((v) => !Qe(v) || v === null ? !1 : F.includes(v)) || o.push({
781
- fieldId: u,
782
- userMessage: "This field expects a valid option",
783
- systemMessage: `Field expects a valid option. Valid options are: ${F.join(", ")}`,
784
- value: l
785
- });
786
- }
787
- }
788
- })
789
- ), { responsesToCommit: m, validationErrors: o };
790
- }
791
- async function i(m) {
792
- const { responsesToCommit: o, validationErrors: u } = await a(m);
793
- return u.length ? { errors: u } : (n.registerBatch(o), { errors: [] });
794
- }
795
- const c = e.map((m) => m), d = e.map((m) => Object.keys(m).map((u) => ({
796
- fieldId: u,
797
- value: m[u]
798
- })));
799
- function g(m) {
800
- const o = t.fields.getById(m), u = e.map((f) => f[m] || null);
801
- return D({ f: o, r: u }, ({ f, r: l }) => l || (f?.defaultValue ? f.defaultValue : null));
802
- }
803
- function S(m) {
804
- const o = t.fields.getByName(m);
805
- return D({ f: o, s: e }, ({ f: u, s: f }) => {
806
- const l = u && f[u.id] || null;
807
- return l || (u?.defaultValue ? u.defaultValue : null);
808
- });
809
- }
810
- return k({
811
- clock: r,
812
- target: n.clearAll
813
- }), k({
814
- clock: s,
815
- target: n.clear
816
- }), {
817
- register: p,
818
- clearAll: r,
819
- clear: s,
820
- commit: i,
821
- getResponsesToCommit: a,
822
- listStore: d,
823
- getAllStore: c,
824
- getByFieldId: g,
825
- getByFieldName: S
826
- };
591
+ var Be = T("response"), Ve = A(({ client: e }) => {
592
+ let t = Be.store({}), n = D(), i = D(), a = w(t, {
593
+ register(e, t) {
594
+ return {
595
+ ...e,
596
+ [t.fieldId]: t.response
597
+ };
598
+ },
599
+ registerBatch(e, t) {
600
+ return {
601
+ ...e,
602
+ ...t
603
+ };
604
+ },
605
+ clear(e, t) {
606
+ let { [t]: n, ...r } = e;
607
+ return r;
608
+ },
609
+ clearAll() {
610
+ return {};
611
+ }
612
+ });
613
+ function o(e, t) {
614
+ if (typeof e == "string") {
615
+ if (t === void 0) throw Error("Response is required when registering a single field");
616
+ a.register({
617
+ fieldId: e,
618
+ response: t
619
+ });
620
+ } else a.registerBatch(e);
621
+ }
622
+ async function s(t) {
623
+ let n = [];
624
+ return await Promise.allSettled(Object.entries(t).map(async ([t, i]) => {
625
+ let a = i, o = e.fields.getById(t).getState();
626
+ if (!o) {
627
+ n.push({
628
+ fieldId: t,
629
+ userMessage: `Field '${t}' not found`,
630
+ systemMessage: `No field with ID '${t}' exists. Available field IDs are: ${Object.keys(e.fields.listStore.getState()).join(",")}.`,
631
+ value: a
632
+ });
633
+ return;
634
+ }
635
+ if (o.multiple && (typeof a == "string" || typeof a == "number" || typeof a == "boolean") && (a = [a]), o.validation && !l(o.validation) && !q(a)) {
636
+ let i = await r(e, o.validation, { $input: a });
637
+ if (typeof i == "string") {
638
+ n.push({
639
+ fieldId: t,
640
+ userMessage: i,
641
+ systemMessage: `Field validation resulted in an error: ${i}.`,
642
+ value: a
643
+ });
644
+ return;
645
+ }
646
+ if (i === !1) {
647
+ n.push({
648
+ fieldId: t,
649
+ userMessage: "This field is invalid",
650
+ systemMessage: "Field validation resulted in an unspecified error.",
651
+ value: a
652
+ });
653
+ return;
654
+ }
655
+ }
656
+ if (o.required && q(a)) {
657
+ n.push({
658
+ fieldId: t,
659
+ userMessage: "This field is required",
660
+ systemMessage: "Field is required.",
661
+ value: a
662
+ });
663
+ return;
664
+ }
665
+ if (!o.multiple && Array.isArray(a) && a.length > 1) {
666
+ n.push({
667
+ fieldId: t,
668
+ userMessage: "This field expects a single response",
669
+ systemMessage: "Field expects a single response.",
670
+ value: a
671
+ });
672
+ return;
673
+ }
674
+ if (o.type === "object" && !q(a)) if (o.multiple) {
675
+ if (!Array.isArray(a)) {
676
+ n.push({
677
+ fieldId: t,
678
+ userMessage: "This field expects multiple values",
679
+ systemMessage: "Field expects an array of objects.",
680
+ value: a
681
+ });
682
+ return;
683
+ }
684
+ for (let e of a) {
685
+ if (typeof e != "object" || !e) {
686
+ n.push({
687
+ fieldId: t,
688
+ userMessage: "This field expects object values",
689
+ systemMessage: "Field expects an array of objects, but received non-object value.",
690
+ value: a
691
+ });
692
+ return;
693
+ }
694
+ if (!J(e)) {
695
+ n.push({
696
+ fieldId: t,
697
+ userMessage: "This field contains non-serializable data",
698
+ systemMessage: "Field value contains non-JSON-serializable data (functions, circular references, etc.).",
699
+ value: a
700
+ });
701
+ return;
702
+ }
703
+ }
704
+ } else {
705
+ if (Array.isArray(a)) {
706
+ n.push({
707
+ fieldId: t,
708
+ userMessage: "This field expects a single value",
709
+ systemMessage: "Field expects a single object, not an array.",
710
+ value: a
711
+ });
712
+ return;
713
+ }
714
+ if (typeof a != "object" || !a) {
715
+ n.push({
716
+ fieldId: t,
717
+ userMessage: "This field expects an object value",
718
+ systemMessage: "Field expects an object value.",
719
+ value: a
720
+ });
721
+ return;
722
+ }
723
+ if (!J(a)) {
724
+ n.push({
725
+ fieldId: t,
726
+ userMessage: "This field contains non-serializable data",
727
+ systemMessage: "Field value contains non-JSON-serializable data (functions, circular references, etc.).",
728
+ value: a
729
+ });
730
+ return;
731
+ }
732
+ }
733
+ if (Array.isArray(o.options) && o.options.length > 0) {
734
+ let e = Array.isArray(a) ? a : [a];
735
+ if (o.type === "object") {
736
+ let r = o.options.map((e) => JSON.stringify(e.value));
737
+ e.every((e) => r.includes(JSON.stringify(e))) || n.push({
738
+ fieldId: t,
739
+ userMessage: "This field expects a valid option",
740
+ systemMessage: "Field expects a valid option from the defined choices.",
741
+ value: a
742
+ });
743
+ } else {
744
+ let r = o.options.map((e) => e.value);
745
+ e.every((e) => !ze(e) || e === null ? !1 : r.includes(e)) || n.push({
746
+ fieldId: t,
747
+ userMessage: "This field expects a valid option",
748
+ systemMessage: `Field expects a valid option. Valid options are: ${r.join(", ")}`,
749
+ value: a
750
+ });
751
+ }
752
+ }
753
+ })), {
754
+ responsesToCommit: t,
755
+ validationErrors: n
756
+ };
757
+ }
758
+ async function c(e) {
759
+ let { responsesToCommit: t, validationErrors: n } = await s(e);
760
+ return n.length ? { errors: n } : (a.registerBatch(t), { errors: [] });
761
+ }
762
+ let u = t.map((e) => e), d = t.map((e) => Object.keys(e).map((t) => ({
763
+ fieldId: t,
764
+ value: e[t]
765
+ })));
766
+ function f(n) {
767
+ return C({
768
+ f: e.fields.getById(n),
769
+ r: t.map((e) => e[n] || null)
770
+ }, ({ f: e, r: t }) => t || (e?.defaultValue ? e.defaultValue : null));
771
+ }
772
+ function p(n) {
773
+ return C({
774
+ f: e.fields.getByName(n),
775
+ s: t
776
+ }, ({ f: e, s: t }) => e && t[e.id] || (e?.defaultValue ? e.defaultValue : null));
777
+ }
778
+ return k({
779
+ clock: n,
780
+ target: a.clearAll
781
+ }), k({
782
+ clock: i,
783
+ target: a.clear
784
+ }), {
785
+ register: o,
786
+ clearAll: n,
787
+ clear: i,
788
+ commit: c,
789
+ getResponsesToCommit: s,
790
+ listStore: d,
791
+ getAllStore: u,
792
+ getByFieldId: f,
793
+ getByFieldName: p
794
+ };
827
795
  });
828
- function et(t) {
829
- return I(Ze, t);
796
+ function He(e) {
797
+ return j(Ve, e);
830
798
  }
831
- function tt() {
832
- if (L()) {
833
- const t = [
834
- document.currentScript,
835
- document.getElementById("picked_script"),
836
- document.getElementById("picked"),
837
- document.getElementById("cvly"),
838
- document.getElementById("cvly-x")
839
- ];
840
- for (const e of t)
841
- if (e?.dataset.api)
842
- return e.dataset.api;
843
- }
844
- return "https://api.picked.app";
799
+ //#endregion
800
+ //#region src/utils/api-url.ts
801
+ function Ue() {
802
+ if (_()) {
803
+ let e = [
804
+ document.currentScript,
805
+ document.getElementById("picked_script"),
806
+ document.getElementById("picked"),
807
+ document.getElementById("cvly"),
808
+ document.getElementById("cvly-x")
809
+ ];
810
+ for (let t of e) if (t?.dataset.api) return t.dataset.api;
811
+ }
812
+ return "https://api.picked.app";
845
813
  }
846
- function rt() {
847
- if (!_())
848
- return {};
849
- const t = new URLSearchParams(window.location.search), e = {}, r = /* @__PURE__ */ new Set();
850
- for (const [n] of t.entries())
851
- n.startsWith("picked-") && r.add(n);
852
- for (const n of r) {
853
- const p = n.replace("picked-", "");
854
- e[p] = t.getAll(n);
855
- }
856
- const s = A("s");
857
- if (r.size > 0)
858
- O.set(s, e);
859
- else {
860
- const n = O.get(s);
861
- if (n)
862
- return n.json() || {};
863
- }
864
- return e;
814
+ //#endregion
815
+ //#region src/utils/search-params.ts
816
+ function We() {
817
+ if (!p()) return {};
818
+ let e = new URLSearchParams(window.location.search), t = {}, n = /* @__PURE__ */ new Set();
819
+ for (let [t] of e.entries()) t.startsWith("picked-") && n.add(t);
820
+ for (let r of n) {
821
+ let n = r.replace("picked-", "");
822
+ t[n] = e.getAll(r);
823
+ }
824
+ let r = b("s");
825
+ if (n.size > 0) y.set(r, t);
826
+ else {
827
+ let e = y.get(r);
828
+ if (e) return e.json() || {};
829
+ }
830
+ return t;
865
831
  }
866
- function st() {
867
- if (pe())
868
- return crypto.randomUUID();
869
- const t = Date.now().toString(36), e = Math.random().toString(36).substring(2, 15), r = Math.random().toString(36).substring(2, 15), s = Math.random().toString(36).substring(2, 15);
870
- return `${t}-${e}-${r}-${s}`;
832
+ //#endregion
833
+ //#region src/utils/user-session.ts
834
+ function Ge() {
835
+ return h() ? crypto.randomUUID() : `${Date.now().toString(36)}-${Math.random().toString(36).substring(2, 15)}-${Math.random().toString(36).substring(2, 15)}-${Math.random().toString(36).substring(2, 15)}`;
871
836
  }
872
- const nt = A("session", "global"), it = () => {
873
- const t = C.get(nt);
874
- let e;
875
- t.isSet() && (e = t.json());
876
- const r = (n) => t.set({
877
- id: n.id,
878
- isUser: n.isUser,
879
- identifiers: n.identifiers
880
- }), s = {
881
- id: e?.id ?? st(),
882
- isUser: e?.isUser ?? void 0,
883
- identifiers: e?.identifiers ?? void 0,
884
- setIsUser(n) {
885
- this.isUser = n, r(this);
886
- },
887
- setIdentifier(n, p) {
888
- this.identifiers = { ...this.identifiers, [n]: p }, r(this);
889
- },
890
- removeIdentifier(n) {
891
- this.identifiers && (this.identifiers = Object.fromEntries(
892
- Object.entries(this.identifiers).filter(([p]) => p !== n)
893
- ), this.identifiers = Object.keys(this.identifiers).length ? this.identifiers : void 0, r(this));
894
- }
895
- };
896
- return r(s), s;
897
- }, ot = () => le() ? it() : null;
898
- function at() {
899
- if (_())
900
- return globalThis.window.pickedConfig;
837
+ var Ke = b("session", "global"), qe = () => {
838
+ let e = v.get(Ke), t;
839
+ e.isSet() && (t = e.json());
840
+ let n = (t) => e.set({
841
+ id: t.id,
842
+ isUser: t.isUser,
843
+ identifiers: t.identifiers
844
+ }), r = {
845
+ id: t?.id ?? Ge(),
846
+ isUser: t?.isUser ?? void 0,
847
+ identifiers: t?.identifiers ?? void 0,
848
+ setIsUser(e) {
849
+ this.isUser = e, n(this);
850
+ },
851
+ setIdentifier(e, t) {
852
+ this.identifiers = {
853
+ ...this.identifiers,
854
+ [e]: t
855
+ }, n(this);
856
+ },
857
+ removeIdentifier(e) {
858
+ this.identifiers && (this.identifiers = Object.fromEntries(Object.entries(this.identifiers).filter(([t]) => t !== e)), this.identifiers = Object.keys(this.identifiers).length ? this.identifiers : void 0, n(this));
859
+ }
860
+ };
861
+ return n(r), r;
862
+ }, Je = () => g() ? qe() : null;
863
+ //#endregion
864
+ //#region src/utils/picked-config.ts
865
+ function Y() {
866
+ if (p()) return globalThis.window.pickedConfig;
901
867
  }
902
- function ct() {
903
- if (!_())
904
- return;
905
- const t = at(), e = t?.organizationId, r = t?.projectId;
906
- return e && r ? { organizationId: e, projectId: r } : void 0;
868
+ function Ye() {
869
+ if (!p()) return;
870
+ let e = Y(), t = e?.organizationId, n = e?.projectId;
871
+ return t && n ? {
872
+ organizationId: t,
873
+ projectId: n
874
+ } : void 0;
907
875
  }
908
- function ut() {
909
- if (!L())
910
- return;
911
- const t = document.getElementById("picked_script") || document.querySelector("script#picked") || // legacy support for 'cvly' script tag
912
- document.getElementById("cvly");
913
- if (!t)
914
- return;
915
- const e = t.dataset.organizationid || t.getAttribute("organization-id"), r = t.dataset.projectid || t.getAttribute("project-id");
916
- return e && r ? { organizationId: e, projectId: r } : void 0;
876
+ function Xe() {
877
+ if (!_()) return;
878
+ let e = document.getElementById("picked_script") || document.querySelector("script#picked") || document.getElementById("cvly");
879
+ if (!e) return;
880
+ let t = e.dataset.organizationid || e.getAttribute("organization-id"), n = e.dataset.projectid || e.getAttribute("project-id");
881
+ return t && n ? {
882
+ organizationId: t,
883
+ projectId: n
884
+ } : void 0;
917
885
  }
918
- function dt(t) {
919
- const { organizationId: e, projectId: r } = t;
920
- if (e && r)
921
- return { organizationId: e, projectId: r };
922
- const s = ct();
923
- if (s)
924
- return s;
925
- const n = ut();
926
- if (n)
927
- return n;
928
- throw new Error(
929
- `organizationId and projectId are required. You can provide them via one of:
930
- (1) createClient({ organizationId, projectId }) options;
931
- (2) window.pickedConfig = { organizationId, projectId };
932
- (3) a <script> tag with id 'picked_script', 'picked', or 'cvly' that includes data-organizationid/data-projectid (or organization-id/project-id) attributes.`
933
- );
886
+ function Ze(e) {
887
+ let { organizationId: t, projectId: n } = e;
888
+ if (t && n) return {
889
+ organizationId: t,
890
+ projectId: n
891
+ };
892
+ let r = Ye();
893
+ if (r) return r;
894
+ let i = Xe();
895
+ if (i) return i;
896
+ throw Error("organizationId and projectId are required. You can provide them via one of:\n (1) createClient({ organizationId, projectId }) options;\n (2) window.pickedConfig = { organizationId, projectId };\n (3) a <script> tag with id 'picked_script', 'picked', or 'cvly' that includes data-organizationid/data-projectid (or organization-id/project-id) attributes.");
934
897
  }
935
- function z(t) {
936
- return t == null || t === "" || Number.isNaN(t);
898
+ //#endregion
899
+ //#region src/utils/render-string.ts
900
+ function X(e) {
901
+ return e == null || e === "" || Number.isNaN(e);
937
902
  }
938
- const lt = {
939
- default: (t, e) => z(t) ? String(e ?? "") : String(t),
940
- append: (t, e) => e && !z(t) ? `${t} ${e}` : t,
941
- prepend: (t, e) => e && !z(t) ? `${e} ${t}` : t,
942
- upper: (t) => typeof t == "string" ? t.toUpperCase() : t,
943
- lower: (t) => typeof t == "string" ? t.toLowerCase() : t,
944
- capitalize: (t) => typeof t == "string" ? t.charAt(0).toUpperCase() + t.slice(1) : t,
945
- list: (t) => {
946
- if (!t || typeof t != "object")
947
- return t;
948
- const e = new Intl.ListFormat("en", { style: "long", type: "conjunction" });
949
- if (Array.isArray(t))
950
- return e.format(t.map(String));
951
- if (t instanceof Map) {
952
- const r = Array.from(t.entries()).map(([s, n]) => `${s}: ${n}`);
953
- return e.format(r);
954
- }
955
- return t instanceof Set ? e.format(Array.from(t).map(String)) : t;
956
- }
903
+ var Qe = {
904
+ default: (e, t) => X(e) ? String(t ?? "") : String(e),
905
+ append: (e, t) => t && !X(e) ? `${e} ${t}` : e,
906
+ prepend: (e, t) => t && !X(e) ? `${t} ${e}` : e,
907
+ upper: (e) => typeof e == "string" ? e.toUpperCase() : e,
908
+ lower: (e) => typeof e == "string" ? e.toLowerCase() : e,
909
+ capitalize: (e) => typeof e == "string" ? e.charAt(0).toUpperCase() + e.slice(1) : e,
910
+ list: (e) => {
911
+ if (!e || typeof e != "object") return e;
912
+ let t = new Intl.ListFormat("en", {
913
+ style: "long",
914
+ type: "conjunction"
915
+ });
916
+ if (Array.isArray(e)) return t.format(e.map(String));
917
+ if (e instanceof Map) {
918
+ let n = Array.from(e.entries()).map(([e, t]) => `${e}: ${t}`);
919
+ return t.format(n);
920
+ }
921
+ return e instanceof Set ? t.format(Array.from(e).map(String)) : e;
922
+ }
957
923
  };
958
- function pt(t) {
959
- return typeof t == "object" && t !== null;
960
- }
961
- function ft(t, e) {
962
- let r = e;
963
- for (const s of t.split("."))
964
- if (pt(r) && s in r)
965
- r = r[s];
966
- else
967
- return;
968
- return typeof r == "function" ? r() : r;
924
+ function $e(e) {
925
+ return typeof e == "object" && !!e;
969
926
  }
970
- function gt(t, e) {
971
- const r = t.split("|").map((p) => p.trim()), s = r.shift() ?? "";
972
- let n = ft(s, e);
973
- for (const p of r) {
974
- const [a, ...i] = p.split(":"), c = a?.trim() ?? "", d = i.join(":").trim().replaceAll(/(?:^['"]|['"]$)/g, ""), g = lt[c];
975
- g && (n = g(n, d));
976
- }
977
- return z(n) ? "" : typeof n == "object" ? JSON.stringify(n) : String(n);
927
+ function et(e, t) {
928
+ let n = t;
929
+ for (let t of e.split(".")) if ($e(n) && t in n) n = n[t];
930
+ else return;
931
+ return typeof n == "function" ? n() : n;
978
932
  }
979
- function mt(t, e) {
980
- let r = "", s = 0;
981
- for (; s < t.length; )
982
- if (t[s] === "{" && t[s + 1] === "{") {
983
- s += 2;
984
- let n = "";
985
- for (; s < t.length; ) {
986
- if (t[s] === "}" && t[s + 1] === "}") {
987
- s += 2;
988
- break;
989
- }
990
- n += t[s++];
991
- }
992
- r += gt(n.trim(), e);
993
- } else
994
- r += t[s++];
995
- return r;
933
+ function tt(e, t) {
934
+ let n = e.split("|").map((e) => e.trim()), r = et(n.shift() ?? "", t);
935
+ for (let e of n) {
936
+ let [t, ...n] = e.split(":"), i = t?.trim() ?? "", a = n.join(":").trim().replaceAll(/(?:^['"]|['"]$)/g, ""), o = Qe[i];
937
+ o && (r = o(r, a));
938
+ }
939
+ return X(r) ? "" : typeof r == "object" ? JSON.stringify(r) : String(r);
996
940
  }
997
- class ht {
998
- static logger = w;
999
- options;
1000
- isReady = !1;
1001
- project;
1002
- plugins = {};
1003
- pluginStartHooks = [];
1004
- eventBus = V();
1005
- events;
1006
- organizationId;
1007
- projectId;
1008
- apiUrl;
1009
- dashboardUrl;
1010
- documentationSiteUrl;
1011
- session;
1012
- precondition;
1013
- condition;
1014
- preconditionPassed;
1015
- searchParams = rt();
1016
- history = Ne();
1017
- components = Fe({ client: this });
1018
- experienceInstances = ie({ client: this });
1019
- experiences = ye({ client: this });
1020
- frameworks = Ee({ client: this });
1021
- predicates = Ce({ client: this });
1022
- features = Ue({ client: this });
1023
- responses = et({ client: this });
1024
- fields = Ge();
1025
- actions = We();
1026
- constructor(e) {
1027
- w("info", "Picked client initializing."), this.events = Le(this.eventBus);
1028
- const { organizationId: r, projectId: s } = dt(e);
1029
- this.organizationId = r, this.projectId = s, this.options = Object.freeze(e), this.apiUrl = e.apiUrl || tt(), this.dashboardUrl = e.dashboardUrl ?? "https://dashboard.picked.app", this.documentationSiteUrl = e.documentationSiteUrl ?? "https://docs.picked.app", this.session = ot(), this.setupPlugins(), this.setupFeatures(), this.setupPredicates(), this.setupActions(), this.setupComponents(), this.precondition = e.precondition, this.condition = e.condition, this.eventBus({ type: "load", version: ce() }), w("info", "Picked client initialized.");
1030
- }
1031
- start = () => {
1032
- Be(this), (async () => {
1033
- if (!L())
1034
- return;
1035
- await this.runPluginStartHooks();
1036
- const r = await this.evaluateGate(this.precondition);
1037
- this.preconditionPassed = r;
1038
- let s;
1039
- r && (s = await this.evaluateGate(this.condition));
1040
- const n = r && s === !0;
1041
- if (typeof s == "boolean" && this.session) {
1042
- const p = s;
1043
- this.session.setIsUser(p), this.eventBus({ type: "user_decide", value: p });
1044
- }
1045
- if (this.eventBus({ type: "initialize" }), n && H(this), await oe(), n && H(this), this.eventBus({ type: "ready" }), this.isReady = !0, this.session) {
1046
- const { id: p, isUser: a, identifiers: i } = this.session;
1047
- this.eventBus({ type: "session", id: p, isUser: a, identifiers: i });
1048
- }
1049
- })();
1050
- };
1051
- registerFramework = (e) => {
1052
- Pe(e, this.frameworks);
1053
- };
1054
- registerComponent = (e, r) => {
1055
- G(e, r, this.components);
1056
- };
1057
- registerPlugin = (e) => {
1058
- ve(this, e, this.plugins);
1059
- const r = this.plugins[e.name];
1060
- r?.onStart && this.pluginStartHooks.push(r.onStart);
1061
- };
1062
- registerFeature = (e, r) => {
1063
- De(e, r, this.features);
1064
- };
1065
- registerPredicate = (e, r) => {
1066
- Oe(e, r, this.predicates);
1067
- };
1068
- registerField = (e) => {
1069
- Je(e, this.fields);
1070
- };
1071
- registerAction = (e, r) => {
1072
- Ye(e, r, this.actions);
1073
- };
1074
- registerExperience = (e) => {
1075
- e.component && G(`experience:${e.id}`, e.component, this.components), Se(e, this.experiences);
1076
- };
1077
- getStateSnapshot = () => X(this);
1078
- /** @deprecated Use `client.experiences.launch({ id: experienceId })` instead. */
1079
- launchExperienceById = async (e, r) => {
1080
- this.experiences.launch({ id: e, options: r });
1081
- };
1082
- /** @deprecated Use `client.experiences.open({ id: experienceId })` instead. */
1083
- openExperienceById = async (e) => {
1084
- this.experiences.open({ id: e });
1085
- };
1086
- async runPluginStartHooks() {
1087
- for (const e of this.pluginStartHooks)
1088
- await e();
1089
- }
1090
- async evaluateGate(e) {
1091
- return e === void 0 ? !0 : typeof e == "boolean" ? e : await P(this, e) === !0;
1092
- }
1093
- setupPlugins() {
1094
- this.options.plugins?.forEach((e) => {
1095
- if (Array.isArray(e)) {
1096
- e.forEach((s) => {
1097
- this.registerPlugin(s);
1098
- });
1099
- return;
1100
- }
1101
- const r = typeof e == "function" ? e() : e;
1102
- if (Array.isArray(r)) {
1103
- r.forEach((s) => this.registerPlugin(s));
1104
- return;
1105
- }
1106
- this.registerPlugin(r);
1107
- });
1108
- }
1109
- setupFeatures() {
1110
- const e = this.options.features;
1111
- e && Object.entries(e).forEach(([r, s]) => {
1112
- const n = typeof s == "function" ? s() : s;
1113
- this.registerFeature(r, n);
1114
- }), this.searchParams.feature && this.searchParams.feature.forEach((r) => {
1115
- try {
1116
- const [s, n] = r.split(":");
1117
- if (!s || !n)
1118
- return;
1119
- let p = n;
1120
- p === "true" && (p = !0), p === "false" && (p = !1), this.registerFeature(s, {
1121
- key: `${p}`,
1122
- // use the assigned bucket value as the key
1123
- name: s,
1124
- // eslint-disable-next-line sonarjs/function-return-type -- Return type varies based on URL param value
1125
- decide: () => p,
1126
- storage: "session"
1127
- });
1128
- } catch {
1129
- }
1130
- });
1131
- }
1132
- setupPredicates() {
1133
- const e = this.options.predicates;
1134
- e && Object.entries(e).forEach(([r, s]) => {
1135
- if (typeof s != "function") {
1136
- w("warn", `Predicate "${r}" is not a function.`);
1137
- return;
1138
- }
1139
- this.registerPredicate(r, s);
1140
- });
1141
- }
1142
- setupActions() {
1143
- const e = this.options.actions;
1144
- e && Object.entries(e).forEach(([r, s]) => {
1145
- if (typeof s != "function") {
1146
- w("warn", `Action "${r}" is not a function.`);
1147
- return;
1148
- }
1149
- this.registerAction(r, s);
1150
- });
1151
- }
1152
- setupComponents() {
1153
- const e = this.options.components;
1154
- e && Object.entries(e).forEach(([r, s]) => {
1155
- this.registerComponent(r, s);
1156
- });
1157
- }
1158
- getPluginConfig = (e) => this.plugins[e] ?? {};
1159
- /**
1160
- * Interpolates a template string using values from the client's response store and/or a provided context.
1161
- * Supports dot-notation, modifiers, and executing functions found in the context.
1162
- *
1163
- * Sources:
1164
- * - responses.*: Loaded automatically from the client's internal response store by fieldId (e.g. {{ responses.email }}).
1165
- * - Context: Any non-responses.* key must be supplied in the context; nested dot-notation is supported.
1166
- * - Functions: If a context value is a function, it is invoked and its result is rendered.
1167
- *
1168
- * Placeholders:
1169
- * - {{ key | modifier: arg | modifier2: arg2 }} with modifiers applied left-to-right.
1170
- *
1171
- * Modifiers:
1172
- * - default: Use the argument when the value is null, undefined, NaN, or empty string; otherwise return the value as a string.
1173
- * - append: Append the argument (space-separated) when the value is non-empty.
1174
- * - prepend: Prepend the argument (space-separated) when the value is non-empty.
1175
- * - upper: Uppercase string values.
1176
- * - lower: Lowercase string values.
1177
- * - capitalize: Capitalize the first letter of string values.
1178
- * - list: Format Arrays, Maps, and Sets via Intl.ListFormat (Maps render as key: value pairs).
1179
- *
1180
- * Behavior:
1181
- * - Empty values (null, undefined, empty string, NaN) render as empty. Numeric 0 renders as "0", boolean false renders as "false", and boolean true renders as "true".
1182
- * - Objects and arrays are JSON-stringified; Maps/Sets serialize to {} unless the list modifier is used.
1183
- * - Unknown modifiers are ignored.
1184
- *
1185
- * @param template Template containing placeholders.
1186
- * @param context Optional data for interpolation. Non-responses.* keys belong here; function values are executed.
1187
- * @returns Interpolated string.
1188
- *
1189
- * @example
1190
- * renderString("Hello {{ name | default: 'Guest' | capitalize }}! Email: {{ responses.email | lower }}.", { name: "ALICE" })
1191
- * // => "Hello Alice! Email: alice@example.com."
1192
- *
1193
- * @example
1194
- * renderString("User: {{ getName | capitalize }}", { getName: () => "alice" })
1195
- * // => "User: Alice"
1196
- *
1197
- * @example
1198
- * renderString("Fruits: {{ fruits | list }}", { fruits: ["apple", "banana", "cherry"] })
1199
- * // => "Fruits: apple, banana, and cherry"
1200
- */
1201
- renderString = (e, r) => {
1202
- const s = this.responses.listStore.getState().reduce((n, p) => (n[p.fieldId] = p.value, n), {});
1203
- return mt(e, {
1204
- ...r,
1205
- responses: s
1206
- });
1207
- };
941
+ function Z(e, t) {
942
+ let n = "", r = 0;
943
+ for (; r < e.length;) if (e[r] === "{" && e[r + 1] === "{") {
944
+ r += 2;
945
+ let i = "";
946
+ for (; r < e.length;) {
947
+ if (e[r] === "}" && e[r + 1] === "}") {
948
+ r += 2;
949
+ break;
950
+ }
951
+ i += e[r++];
952
+ }
953
+ n += tt(i.trim(), t);
954
+ } else n += e[r++];
955
+ return n;
1208
956
  }
1209
- function Z(t) {
1210
- return t.options?.some((e) => "condition" in e) ?? !1;
957
+ //#endregion
958
+ //#region src/picked.ts
959
+ var Q = class {
960
+ static logger = u;
961
+ options;
962
+ isReady = !1;
963
+ project;
964
+ plugins = {};
965
+ pluginStartHooks = [];
966
+ eventBus = D();
967
+ events;
968
+ organizationId;
969
+ projectId;
970
+ apiUrl;
971
+ dashboardUrl;
972
+ documentationSiteUrl;
973
+ session;
974
+ precondition;
975
+ condition;
976
+ preconditionPassed;
977
+ searchParams = We();
978
+ history = Oe();
979
+ components = V({ client: this });
980
+ experienceInstances = i({ client: this });
981
+ experiences = de({ client: this });
982
+ frameworks = he({ client: this });
983
+ predicates = Se({ client: this });
984
+ features = Te({ client: this });
985
+ responses = He({ client: this });
986
+ fields = Ne();
987
+ actions = Le();
988
+ constructor(e) {
989
+ this.events = Ae(this.eventBus);
990
+ let { organizationId: t, projectId: n } = Ze(e);
991
+ this.organizationId = t, this.projectId = n, this.options = Object.freeze(e), this.apiUrl = e.apiUrl || Ue(), this.dashboardUrl = e.dashboardUrl ?? "https://dashboard.picked.app", this.documentationSiteUrl = e.documentationSiteUrl ?? "https://docs.picked.app", this.session = Je(), this.setupPlugins(), this.setupFeatures(), this.setupPredicates(), this.setupActions(), this.setupComponents(), this.precondition = e.precondition, this.condition = e.condition, this.eventBus({
992
+ type: "load",
993
+ version: oe()
994
+ });
995
+ }
996
+ start = () => {
997
+ ye(this), (async () => {
998
+ if (!_()) return;
999
+ await this.runPluginStartHooks();
1000
+ let t = await this.evaluateGate(this.precondition);
1001
+ this.preconditionPassed = t;
1002
+ let n;
1003
+ t && (n = await this.evaluateGate(this.condition));
1004
+ let r = t && n === !0;
1005
+ if (typeof n == "boolean" && this.session) {
1006
+ let e = n;
1007
+ this.session.setIsUser(e), this.eventBus({
1008
+ type: "user_decide",
1009
+ value: e
1010
+ });
1011
+ }
1012
+ if (this.eventBus({ type: "initialize" }), r && P(this), await e(), r && P(this), this.eventBus({ type: "ready" }), this.isReady = !0, this.session) {
1013
+ let { id: e, isUser: t, identifiers: n } = this.session;
1014
+ this.eventBus({
1015
+ type: "session",
1016
+ id: e,
1017
+ isUser: t,
1018
+ identifiers: n
1019
+ });
1020
+ }
1021
+ })();
1022
+ };
1023
+ registerFramework = (e) => {
1024
+ ge(e, this.frameworks);
1025
+ };
1026
+ registerComponent = (e, t) => {
1027
+ H(e, t, this.components);
1028
+ };
1029
+ registerPlugin = (e) => {
1030
+ I(this, e, this.plugins);
1031
+ let t = this.plugins[e.name];
1032
+ t?.onStart && this.pluginStartHooks.push(t.onStart);
1033
+ };
1034
+ registerFeature = (e, t) => {
1035
+ Ee(e, t, this.features);
1036
+ };
1037
+ registerPredicate = (e, t) => {
1038
+ Ce(e, t, this.predicates);
1039
+ };
1040
+ registerField = (e) => {
1041
+ Pe(e, this.fields);
1042
+ };
1043
+ registerAction = (e, t) => {
1044
+ Re(e, t, this.actions);
1045
+ };
1046
+ registerExperience = (e) => {
1047
+ e.component && H(`experience:${e.id}`, e.component, this.components), fe(e, this.experiences);
1048
+ };
1049
+ getStateSnapshot = () => U(this);
1050
+ launchExperienceById = async (e, t) => {
1051
+ this.experiences.launch({
1052
+ id: e,
1053
+ options: t
1054
+ });
1055
+ };
1056
+ openExperienceById = async (e) => {
1057
+ this.experiences.open({ id: e });
1058
+ };
1059
+ async runPluginStartHooks() {
1060
+ for (let e of this.pluginStartHooks) await e();
1061
+ }
1062
+ async evaluateGate(e) {
1063
+ return e === void 0 ? !0 : typeof e == "boolean" ? e : await r(this, e) === !0;
1064
+ }
1065
+ setupPlugins() {
1066
+ this.options.plugins?.forEach((e) => {
1067
+ if (Array.isArray(e)) {
1068
+ e.forEach((e) => {
1069
+ this.registerPlugin(e);
1070
+ });
1071
+ return;
1072
+ }
1073
+ let t = typeof e == "function" ? e() : e;
1074
+ if (Array.isArray(t)) {
1075
+ t.forEach((e) => this.registerPlugin(e));
1076
+ return;
1077
+ }
1078
+ this.registerPlugin(t);
1079
+ });
1080
+ }
1081
+ setupFeatures() {
1082
+ let e = this.options.features;
1083
+ e && Object.entries(e).forEach(([e, t]) => {
1084
+ let n = typeof t == "function" ? t() : t;
1085
+ this.registerFeature(e, n);
1086
+ }), this.searchParams.feature && this.searchParams.feature.forEach((e) => {
1087
+ try {
1088
+ let [t, n] = e.split(":");
1089
+ if (!t || !n) return;
1090
+ let r = n;
1091
+ r === "true" && (r = !0), r === "false" && (r = !1), this.registerFeature(t, {
1092
+ key: `${r}`,
1093
+ name: t,
1094
+ decide: () => r,
1095
+ storage: "session"
1096
+ });
1097
+ } catch {}
1098
+ });
1099
+ }
1100
+ setupPredicates() {
1101
+ let e = this.options.predicates;
1102
+ e && Object.entries(e).forEach(([e, t]) => {
1103
+ if (typeof t != "function") {
1104
+ `${e}`;
1105
+ return;
1106
+ }
1107
+ this.registerPredicate(e, t);
1108
+ });
1109
+ }
1110
+ setupActions() {
1111
+ let e = this.options.actions;
1112
+ e && Object.entries(e).forEach(([e, t]) => {
1113
+ if (typeof t != "function") {
1114
+ `${e}`;
1115
+ return;
1116
+ }
1117
+ this.registerAction(e, t);
1118
+ });
1119
+ }
1120
+ setupComponents() {
1121
+ let e = this.options.components;
1122
+ e && Object.entries(e).forEach(([e, t]) => {
1123
+ this.registerComponent(e, t);
1124
+ });
1125
+ }
1126
+ getPluginConfig = (e) => this.plugins[e] ?? {};
1127
+ renderString = (e, t) => {
1128
+ let n = this.responses.listStore.getState().reduce((e, t) => (e[t.fieldId] = t.value, e), {});
1129
+ return Z(e, {
1130
+ ...t,
1131
+ responses: n
1132
+ });
1133
+ };
1134
+ };
1135
+ //#endregion
1136
+ //#region src/utils/get-step-fields.ts
1137
+ function $(e) {
1138
+ return e.options?.some((e) => "condition" in e) ?? !1;
1211
1139
  }
1212
- function yt(t) {
1213
- return t.some((e) => e.condition || Z(e));
1140
+ function nt(e) {
1141
+ return e.some((e) => e.condition || $(e));
1214
1142
  }
1215
- async function Et(t, e) {
1216
- return e.length === 0 || !yt(e) ? e : (await Promise.all(
1217
- e.map(async (s) => {
1218
- if (!(s.condition ? await P(t, s.condition) : !0))
1219
- return null;
1220
- if (!s.options || !Z(s))
1221
- return s;
1222
- const a = (await Promise.all(
1223
- s.options.map(async (c) => c.condition ? await P(t, c.condition) ? c : null : c)
1224
- )).filter((c) => c !== null);
1225
- return a.length === s.options.length ? s : a.length === 0 ? null : { ...s, options: a };
1226
- })
1227
- )).filter((s) => s !== null);
1143
+ async function rt(e, t) {
1144
+ return t.length === 0 || !nt(t) ? t : (await Promise.all(t.map(async (t) => {
1145
+ if (!(!t.condition || await r(e, t.condition))) return null;
1146
+ if (!t.options || !$(t)) return t;
1147
+ let n = (await Promise.all(t.options.map(async (t) => t.condition ? await r(e, t.condition) ? t : null : t))).filter((e) => e !== null);
1148
+ return n.length === t.options.length ? t : n.length === 0 ? null : {
1149
+ ...t,
1150
+ options: n
1151
+ };
1152
+ }))).filter((e) => e !== null);
1228
1153
  }
1229
- function Pt(t) {
1230
- return {
1231
- $$type: "lazy-component",
1232
- load: t
1233
- };
1154
+ //#endregion
1155
+ //#region src/entities/component/lazyComponent.ts
1156
+ function it(e) {
1157
+ return {
1158
+ $$type: "lazy-component",
1159
+ load: e
1160
+ };
1234
1161
  }
1235
- function At(t) {
1236
- return typeof t == "object" && t !== null && "$$type" in t && t.$$type === "lazy-component";
1162
+ function at(e) {
1163
+ return typeof e == "object" && !!e && "$$type" in e && e.$$type === "lazy-component";
1237
1164
  }
1238
- const $t = (t) => {
1239
- const e = new ht(t);
1240
- return fe(e), e;
1241
- };
1242
- export {
1243
- ht as Picked,
1244
- $t as createClient,
1245
- A as createStorageKey,
1246
- P as evaluateRuleLogic,
1247
- at as getPickedConfig,
1248
- Et as getStepFields,
1249
- At as isLazyLoader,
1250
- zt as isPickedRoot,
1251
- Pt as lazyComponent,
1252
- C as localStore,
1253
- O as sessionStore,
1254
- Ot as subscribe,
1255
- Ut as supportsCookies,
1256
- pe as supportsCryptoRandomUUID,
1257
- L as supportsDocument,
1258
- Dt as supportsHistory,
1259
- Vt as supportsIntersectionObserver,
1260
- le as supportsLocalStorage,
1261
- Nt as supportsLocation,
1262
- Tt as supportsSessionStorage,
1263
- Lt as supportsShadowRoot,
1264
- _t as supportsWebComponents,
1265
- _ as supportsWindow
1165
+ //#endregion
1166
+ //#region src/exports.ts
1167
+ var ot = (e) => {
1168
+ let t = new Q(e);
1169
+ return te(t), t;
1266
1170
  };
1171
+ //#endregion
1172
+ export { Q as Picked, ot as createClient, b as createStorageKey, r as evaluateRuleLogic, Y as getPickedConfig, rt as getStepFields, at as isLazyLoader, s as isPickedRoot, it as lazyComponent, v as localStore, y as sessionStore, ae as subscribe, re as supportsCookies, h as supportsCryptoRandomUUID, _ as supportsDocument, ee as supportsHistory, d as supportsIntersectionObserver, g as supportsLocalStorage, ne as supportsLocation, f as supportsSessionStorage, m as supportsShadowRoot, ie as supportsWebComponents, p as supportsWindow };