@picked/client 0.0.1-dev.1765246229329 → 0.0.1-dev.1765328754832

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.
@@ -1,650 +0,0 @@
1
- import { createDomain as w, createApi as F, createEffect as A, attach as L, sample as b, combine as j, createEvent as O } from "effector";
2
- import { createFactory as k, invoke as v } from "@withease/factories";
3
- import { g as N, t as _, b as H, f as q, e as U, h as z } from "./experience-instance-Du9C8zgX.js";
4
- import { l as E } from "./logger-C9a75KZn.js";
5
- import { c as D, l as B, s as M } from "./storage-BLmAhfhl.js";
6
- import { g as G } from "./id-BW-g3y32.js";
7
- import { i as Y } from "./object-JwZ58NOH.js";
8
- import { e as J, k as K } from "./supports-DBMT17KZ.js";
9
- const C = w("experience"), Q = k(
10
- ({ client: t, experience: e }) => {
11
- const o = C.store(e), l = F(o, {
12
- merge(c, m) {
13
- return {
14
- ...c,
15
- ...m.experience
16
- };
17
- }
18
- }), p = o.map((c) => ({
19
- name: c.name,
20
- decisionLogic: c.condition ?? c.decide,
21
- targetSelectors: c.targets,
22
- initializeOpenLogic: e.initializeOpen
23
- })), d = t.components.listStore.map(
24
- (c) => c.find((m) => m.name === `experience:${e.id}`) || c.find((m) => m.name === "experience"),
25
- { skipVoid: !1 }
26
- ), r = o.map((c) => ({
27
- steps: c.steps,
28
- nodes: c.nodes,
29
- edges: c.edges,
30
- conditionals: c.conditionals
31
- })), n = e.actions ?? [];
32
- return {
33
- id: e.id,
34
- configStore: p,
35
- treeStore: r,
36
- componentStore: d,
37
- actions: n,
38
- merge: l.merge
39
- };
40
- }
41
- );
42
- function W(t) {
43
- return v(Q, t);
44
- }
45
- const X = k(({ client: t }) => {
46
- const e = C.store({}), o = F(e, {
47
- register(r, n) {
48
- return {
49
- ...r,
50
- [n.id]: n
51
- };
52
- }
53
- });
54
- function l(r) {
55
- const n = e.getState()[r.id];
56
- if (n) {
57
- n.merge({ experience: r, strategy: "override" });
58
- return;
59
- }
60
- const c = W({ client: t, experience: r });
61
- o.register(c);
62
- }
63
- const p = e.map((r) => Object.values(r));
64
- function d(r) {
65
- return e.map((n) => n[r], { skipVoid: !1 });
66
- }
67
- return {
68
- register: l,
69
- getById: d,
70
- listStore: p
71
- };
72
- });
73
- function je(t) {
74
- return v(X, t);
75
- }
76
- const Ae = (t, e) => {
77
- e.register(t);
78
- }, Z = (t, e) => {
79
- const { targetSelectors: o } = e.configStore.getState(), l = N(o, e.id);
80
- l.forEach(
81
- ({ elements: d }) => d.forEach((r) => _(r, e.id))
82
- ), H(l, e.id).forEach((d) => {
83
- t.experienceInstances.spawn(e, d);
84
- });
85
- }, Oe = async (t) => {
86
- await Promise.all(
87
- t.experiences.listStore.getState().map(async (e) => {
88
- await ee(t, e) && Z(t, e);
89
- })
90
- );
91
- }, ee = async (t, e) => {
92
- const { decisionLogic: o, targetSelectors: l } = e.configStore.getState();
93
- return N(l, e.id).length < 1 || l.length === 0 ? !1 : o && q(o) ? await U(t, o) === !0 : !0;
94
- }, te = w("component"), re = k(
95
- ({ client: t, component: e, name: o }) => {
96
- const l = t.frameworks.getByName(e.frameworkName);
97
- return {
98
- name: o,
99
- component: e,
100
- frameworkStore: l
101
- };
102
- }
103
- );
104
- function ne(t) {
105
- return v(re, t);
106
- }
107
- const se = k(({ client: t }) => {
108
- const e = te.store({}), o = F(e, {
109
- register(r, n) {
110
- return {
111
- ...r,
112
- [n.name]: n
113
- };
114
- }
115
- });
116
- function l(r, n) {
117
- e.getState()[r] && E(
118
- "warn",
119
- `Component '${r}' already registered. Registering again will override the previous registration.`
120
- );
121
- const m = ne({ client: t, name: r, component: n });
122
- o.register(m);
123
- }
124
- const p = e.map((r) => Object.values(r));
125
- function d(r) {
126
- return e.map((n) => n[r], { skipVoid: !1 });
127
- }
128
- return {
129
- register: l,
130
- getByName: d,
131
- listStore: p
132
- };
133
- });
134
- function Me(t) {
135
- return v(se, t);
136
- }
137
- const Pe = (t, e, o) => {
138
- o.register(t, e);
139
- }, oe = w("framework"), ie = k(({ client: t }) => {
140
- const e = oe.store({}), o = F(e, {
141
- register(r, n) {
142
- return {
143
- ...r,
144
- [n.name]: n.framework
145
- };
146
- }
147
- });
148
- function l(r, n) {
149
- e.getState()[r] && E(
150
- "warn",
151
- `Framework '${r}' already registered. Registering again will override the previous registration.`
152
- ), o.register({ name: r, framework: n });
153
- }
154
- const p = e.map((r) => Object.values(r));
155
- function d(r) {
156
- return e.map((n) => n[r], { skipVoid: !1 });
157
- }
158
- return {
159
- register: l,
160
- getByName: d,
161
- listStore: p
162
- };
163
- });
164
- function Ve(t) {
165
- return v(ie, t);
166
- }
167
- const Te = (t, e) => {
168
- e.register(t.name, t);
169
- }, ae = 6e3, ce = D("last_ping", "global"), ue = (t) => `${t}/api/v1/ping`, Ne = async (t) => {
170
- try {
171
- const e = Date.now(), o = B.get(ce);
172
- let l = 0;
173
- if (o.isSet() && (l = o.json() ?? 0), l && e - l < ae)
174
- return !0;
175
- const p = ue(t.apiUrl), d = {
176
- projectId: t.projectId,
177
- path: window.location.pathname
178
- }, r = JSON.stringify(d), n = {
179
- "Content-Type": "application/json"
180
- }, c = document.getElementById("picked_script"), m = c == null ? void 0 : c.dataset.verceltoken;
181
- m && (n["x-vercel-protection-bypass"] = m);
182
- const a = (await fetch(p, {
183
- method: "POST",
184
- headers: n,
185
- body: r
186
- })).ok;
187
- return a && o.set(e), a;
188
- } catch (e) {
189
- return E("error", e), !1;
190
- }
191
- }, le = w("predicate"), ge = k(({ client: t }) => {
192
- const e = le.store({}), o = F(e, {
193
- register(r, n) {
194
- return {
195
- ...r,
196
- [n.name]: {
197
- name: n.name,
198
- predicate: n.predicate
199
- }
200
- };
201
- }
202
- });
203
- function l(r, n) {
204
- e.getState()[r] && E(
205
- "warn",
206
- `Predicate '${r}' already registered. Registering again will override the previous registration.`
207
- ), o.register(Object.freeze({ name: r, predicate: n }));
208
- }
209
- const p = e.map((r) => Object.values(r));
210
- function d(r) {
211
- return e.map((n) => n[r], { skipVoid: !1 });
212
- }
213
- return {
214
- register: l,
215
- getByName: d,
216
- listStore: p
217
- };
218
- });
219
- function Ue(t) {
220
- return v(ge, t);
221
- }
222
- const Ce = (t, e, o) => {
223
- o.register(t, e);
224
- }, P = w("feature"), de = k(({ client: t }) => {
225
- const e = P.store({}), o = P.store([]), l = F(e, {
226
- register(a, i) {
227
- return {
228
- ...a,
229
- [i.id]: i.feature
230
- };
231
- }
232
- });
233
- function p(a, i) {
234
- e.getState()[a] && E(
235
- "warn",
236
- `Feature '${a}' already registered. Registering again will override the previous registration.`
237
- );
238
- const u = {
239
- id: a,
240
- feature: {
241
- ...i,
242
- id: a
243
- }
244
- };
245
- l.register(
246
- Object.freeze({
247
- id: a,
248
- feature: u
249
- })
250
- );
251
- }
252
- const d = A(
253
- async ({
254
- id: a,
255
- key: i,
256
- storage: s = "local"
257
- }) => {
258
- const u = D(`feature:${a}:${i}`, "global"), g = (s === "session" ? M : B).get(u).boolean();
259
- return g === null ? null : { id: a, key: i, bucket: g, storage: s };
260
- }
261
- ), r = A((a) => {
262
- if (!a)
263
- return null;
264
- const i = D(`feature:${a.id}:${a.key}`, "global");
265
- (a.storage === "session" ? M : B).set(i, a.bucket);
266
- }), n = /* @__PURE__ */ new Map(), c = L({
267
- source: { allFeaturesStore: e, resolvedFeaturesStore: o },
268
- effect: async ({ allFeaturesStore: a, resolvedFeaturesStore: i }, { id: s, decide: u, key: f }) => {
269
- const g = i.find(
270
- ($) => $.id === s && $.key === f
271
- );
272
- if (g)
273
- return g;
274
- const y = a[s];
275
- if (!y)
276
- throw new Error(`Feature with id '${s}' not found`);
277
- const h = n.get(`${s}:${f}`);
278
- if (h !== void 0) {
279
- const $ = await h;
280
- return { id: y.id, bucket: $, key: f, storage: y.feature.storage };
281
- }
282
- const R = u();
283
- n.set(`${s}:${f}`, R);
284
- const x = await R;
285
- return { id: y.id, bucket: x, key: f, storage: y.feature.storage };
286
- }
287
- });
288
- b({
289
- clock: l.register,
290
- fn: (a) => {
291
- const { id: i, feature: s } = a;
292
- return { id: i, key: s.feature.key, storage: s.feature.storage };
293
- },
294
- target: d
295
- }), b({
296
- source: e,
297
- clock: c.doneData,
298
- fn: (a, i) => {
299
- var y;
300
- const { id: s, bucket: u, storage: f } = i, g = (y = a[s]) == null ? void 0 : y.feature.key;
301
- return g ? { key: g, id: s, bucket: u, storage: f } : null;
302
- },
303
- target: r
304
- });
305
- const m = j({ allFeaturesStore: e, resolvedFeaturesStore: o }).map(
306
- ({
307
- allFeaturesStore: a,
308
- resolvedFeaturesStore: i
309
- }) => Object.values(a).map((s) => {
310
- const u = i.find(
311
- (f) => f.id === s.id && f.key === s.feature.key
312
- );
313
- return u ? {
314
- id: s.id,
315
- name: s.feature.name,
316
- key: s.feature.key,
317
- getBucket: () => ({ id: s.id, bucket: u.bucket }),
318
- isResolved: !0
319
- } : {
320
- id: s.id,
321
- name: s.feature.name,
322
- key: s.feature.key,
323
- getBucket: async () => await c(s.feature),
324
- isResolved: !1
325
- };
326
- })
327
- );
328
- b({
329
- clock: c.doneData,
330
- source: { resolvedFeatures: o },
331
- filter: ({ resolvedFeatures: a }, { id: i, key: s }) => !a.some(
332
- (f) => f.id === i && f.key === s
333
- ),
334
- fn: (a, i) => i,
335
- target: t.eventBus.prepend((a) => ({
336
- type: "feature_decide",
337
- feature: a
338
- }))
339
- }), b({
340
- clock: [d.doneData, c.doneData],
341
- source: o,
342
- fn: (a, i) => !i || a.some(
343
- (u) => u.id === i.id && u.key === i.key
344
- ) ? a : [...a, i],
345
- target: o
346
- });
347
- function S(a) {
348
- return e.map((i) => i[a], { skipVoid: !1 });
349
- }
350
- return {
351
- register: p,
352
- getByName: S,
353
- listStore: m
354
- };
355
- });
356
- function Le(t) {
357
- return v(de, t);
358
- }
359
- const _e = (t, e, o) => {
360
- o.register(t, e);
361
- }, I = w("history");
362
- function V(t, e) {
363
- return {
364
- // eslint-disable-next-line sonarjs/pseudo-random
365
- id: G(`history-entry-${Date.now()}-${Math.random()}}`),
366
- node: e,
367
- at: Date.now(),
368
- experienceId: t
369
- };
370
- }
371
- const pe = k(() => {
372
- const t = I.store({ entries: [] }), e = I.event(), o = I.event(), l = I.event(), p = I.event();
373
- t.on(e, (r, n) => ({
374
- entries: [...r.entries, V(n.experienceId, n.node)]
375
- })), t.on(o, (r, n) => ({
376
- entries: [
377
- ...r.entries.slice(0, -1),
378
- V(n.experienceId, n.node)
379
- ]
380
- })), t.on(p, (r, n) => n);
381
- function d(r, n) {
382
- const c = { lastStepIndex: -1, secondLastStepIndex: -1 };
383
- for (let m = r.length - 1; m >= 0; m--) {
384
- const S = r[m], a = S == null ? void 0 : S.node;
385
- if (!(!a || !z(a)) && !(n && (S == null ? void 0 : S.experienceId) !== n))
386
- if (c.lastStepIndex === -1)
387
- c.lastStepIndex = m;
388
- else {
389
- c.secondLastStepIndex = m;
390
- break;
391
- }
392
- }
393
- return c;
394
- }
395
- return t.on(l, (r, n) => {
396
- const { secondLastStepIndex: c } = d(r.entries, n.experienceId), m = c !== -1 ? r.entries.slice(0, c + 1) : r.entries;
397
- return {
398
- ...r,
399
- entries: m
400
- };
401
- }), {
402
- push: e,
403
- replace: o,
404
- back: l,
405
- restore: p,
406
- entriesStore: t.map((r) => r.entries)
407
- };
408
- });
409
- function He() {
410
- return v(pe);
411
- }
412
- const fe = w("field"), me = k(() => {
413
- const t = fe.store({}), e = F(t, {
414
- register(r, n) {
415
- return {
416
- ...r,
417
- [n.field.id]: n.field
418
- };
419
- }
420
- });
421
- function o(r) {
422
- e.register({ field: r });
423
- }
424
- const l = t.map((r) => Object.values(r));
425
- function p(r) {
426
- return t.map((n) => n[r] || null);
427
- }
428
- function d(r) {
429
- return t.map((n) => Object.values(n).find((c) => c.name === r) || null);
430
- }
431
- return {
432
- register: o,
433
- getById: p,
434
- listStore: l,
435
- getByName: d
436
- };
437
- });
438
- function qe() {
439
- return v(me);
440
- }
441
- const ze = (t, e) => {
442
- e.register(t);
443
- }, ye = w("action"), Se = k(() => {
444
- const t = ye.store({}), e = F(t, {
445
- register(d, r) {
446
- return {
447
- ...d,
448
- [r.name]: {
449
- name: r.name,
450
- action: r.action
451
- }
452
- };
453
- }
454
- });
455
- function o(d, r) {
456
- e.register({ name: d, action: r });
457
- }
458
- const l = t.map((d) => Object.values(d));
459
- function p(d) {
460
- return t.map((r) => r[d], { skipVoid: !1 });
461
- }
462
- return {
463
- register: o,
464
- getByName: p,
465
- listStore: l
466
- };
467
- });
468
- function Ge() {
469
- return v(Se);
470
- }
471
- const Ye = (t, e, o) => {
472
- o.register(t, e);
473
- };
474
- function T(t) {
475
- return typeof t > "u" || t === null || t === "" || Array.isArray(t) && !t.length;
476
- }
477
- const ke = w("response"), ve = k(({ client: t }) => {
478
- const e = ke.store({}), o = O(), l = O(), p = F(e, {
479
- register(i, s) {
480
- return {
481
- ...i,
482
- [s.fieldId]: s.response
483
- };
484
- },
485
- registerBatch(i, s) {
486
- return {
487
- ...i,
488
- ...s
489
- };
490
- },
491
- clear(i, s) {
492
- const { [s]: u, ...f } = i;
493
- return f;
494
- },
495
- clearAll() {
496
- return {};
497
- }
498
- });
499
- function d(i, s) {
500
- p.register({ fieldId: i, response: s });
501
- }
502
- async function r(i) {
503
- const s = [];
504
- return await Promise.allSettled(
505
- Object.entries(i).map(async ([u, f]) => {
506
- let g = f;
507
- const y = t.fields.getById(u).getState();
508
- if (!y) {
509
- s.push({
510
- fieldId: u,
511
- userMessage: `Field '${u}' not found`,
512
- systemMessage: `No field with ID '${u}' exists. Available field IDs are: ${Object.keys(t.fields.listStore.getState()).join(",")}.`,
513
- value: g
514
- });
515
- return;
516
- }
517
- if (y.multiple && (typeof g == "string" || typeof g == "number" || typeof g == "boolean") && (g = [g]), y.validation && !Y(y.validation) && !T(g)) {
518
- const h = await U(t, y.validation, {
519
- $input: g
520
- });
521
- if (typeof h == "string") {
522
- s.push({
523
- fieldId: u,
524
- userMessage: h,
525
- systemMessage: `Field validation resulted in an error: ${h}.`,
526
- value: g
527
- });
528
- return;
529
- }
530
- if (h === !1) {
531
- s.push({
532
- fieldId: u,
533
- userMessage: "This field is invalid",
534
- systemMessage: "Field validation resulted in an unspecified error.",
535
- value: g
536
- });
537
- return;
538
- }
539
- }
540
- if (y.required && T(g)) {
541
- s.push({
542
- fieldId: u,
543
- userMessage: "This field is required",
544
- systemMessage: "Field is required.",
545
- value: g
546
- });
547
- return;
548
- }
549
- if (!y.multiple && Array.isArray(g) && g.length > 1) {
550
- s.push({
551
- fieldId: u,
552
- userMessage: "This field expects a single response",
553
- systemMessage: "Field expects a single response.",
554
- value: g
555
- });
556
- return;
557
- }
558
- if (y.options) {
559
- const h = y.options.map((x) => x.value), R = Array.isArray(g) ? g : [g];
560
- Array.isArray(g) && (R.every((x) => h.includes(x)) || s.push({
561
- fieldId: u,
562
- userMessage: "This field expects a valid option",
563
- systemMessage: `Field expects a valid option. Valid options are: ${h.join(", ")}`,
564
- value: g
565
- }));
566
- }
567
- })
568
- ), { responsesToCommit: i, validationErrors: s };
569
- }
570
- async function n(i) {
571
- const { responsesToCommit: s, validationErrors: u } = await r(i);
572
- return u.length ? { errors: u } : (p.registerBatch(s), { errors: [] });
573
- }
574
- const c = e.map((i) => i), m = e.map((i) => Object.keys(i).map((u) => ({
575
- fieldId: u,
576
- value: i[u]
577
- })));
578
- function S(i) {
579
- const s = t.fields.getById(i), u = e.map((f) => f[i] || null);
580
- return j({ f: s, r: u }, ({ f, r: g }) => g || (f != null && f.defaultValue ? f.defaultValue : null));
581
- }
582
- function a(i) {
583
- const s = t.fields.getByName(i);
584
- return j({ f: s, s: e }, ({ f: u, s: f }) => {
585
- const g = u && f[u.id] || null;
586
- return g || (u != null && u.defaultValue ? u.defaultValue : null);
587
- });
588
- }
589
- return b({
590
- clock: o,
591
- target: p.clearAll
592
- }), b({
593
- clock: l,
594
- target: p.clear
595
- }), {
596
- register: d,
597
- clearAll: o,
598
- clear: l,
599
- commit: n,
600
- getResponsesToCommit: r,
601
- listStore: m,
602
- getAllStore: c,
603
- getByFieldId: S,
604
- getByFieldName: a
605
- };
606
- });
607
- function Je(t) {
608
- return v(ve, t);
609
- }
610
- function he() {
611
- if (K())
612
- return crypto.randomUUID();
613
- const t = Date.now().toString(36), e = Math.random().toString(36).substring(2, 15), o = Math.random().toString(36).substring(2, 15), l = Math.random().toString(36).substring(2, 15);
614
- return `${t}-${e}-${o}-${l}`;
615
- }
616
- const we = D("session", "global"), Fe = () => {
617
- const t = B.get(we);
618
- let e;
619
- t.isSet() && (e = t.json());
620
- const o = (p) => t.set({ id: p.id, isUser: p.isUser }), l = {
621
- id: (e == null ? void 0 : e.id) ?? he(),
622
- isUser: (e == null ? void 0 : e.isUser) ?? void 0,
623
- setIsUser(p) {
624
- this.isUser = p, o(this);
625
- }
626
- };
627
- return o(l), l;
628
- }, Ke = () => J() ? Fe() : null;
629
- export {
630
- Me as a,
631
- je as b,
632
- He as c,
633
- Ve as d,
634
- Ue as e,
635
- Le as f,
636
- Je as g,
637
- qe as h,
638
- Ge as i,
639
- Ke as j,
640
- Pe as k,
641
- _e as l,
642
- Ce as m,
643
- ze as n,
644
- Ye as o,
645
- Ne as p,
646
- Ae as q,
647
- Z as r,
648
- Te as s,
649
- Oe as t
650
- };