@picked/client 0.0.14-next.3 → 0.0.14

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