@picked/client 0.0.1-dev.1763966324978 → 0.0.1-dev.1764090824933

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