@knocklabs/client 0.19.3 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +1,30 @@
1
- var b = Object.defineProperty;
2
- var I = (a, e, t) => e in a ? b(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
- var d = (a, e, t) => I(a, typeof e != "symbol" ? e + "" : e, t);
4
- import { Store as C } from "@tanstack/store";
5
- import { URLPattern as E } from "urlpattern-polyfill";
6
- import { byKey as w, mockDefaultGroup as R, formatFilters as k, formatState as f, findDefaultGroup as v, DEFAULT_GROUP_KEY as S, checkIfThrottled as P, formatGroupStage as G, SelectionResult as A, newUrl as D, predicateUrlRules as K, predicateUrlPatterns as L } from "./helpers.mjs";
7
- const O = 50, $ = 30 * 1e3, T = 3, g = {
1
+ var _ = Object.defineProperty;
2
+ var b = (u, e, t) => e in u ? _(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
+ var d = (u, e, t) => b(u, typeof e != "symbol" ? e + "" : e, t);
4
+ import { Store as I } from "@tanstack/store";
5
+ import { URLPattern as C } from "urlpattern-polyfill";
6
+ import { byKey as E, mockDefaultGroup as w, formatFilters as k, formatState as f, checkStateIfThrottled as G, formatGroupStage as S, DEFAULT_GROUP_KEY as R, SelectionResult as P, findDefaultGroup as A, newUrl as D, predicateUrlRules as K, predicateUrlPatterns as L } from "./helpers.mjs";
7
+ const $ = 50, T = 30 * 1e3, O = 3, g = {
8
8
  GUIDE_KEY: "knock_guide_key",
9
9
  PREVIEW_SESSION_ID: "knock_preview_session_id"
10
10
  }, p = "knock_guide_debug", l = () => {
11
11
  if (typeof window < "u")
12
12
  return window;
13
- }, U = (a) => `/v1/users/${a}/guides`, y = () => {
14
- const a = l();
15
- if (!a)
13
+ }, U = (u) => `/v1/users/${u}/guides`, y = () => {
14
+ const u = l();
15
+ if (!u)
16
16
  return { forcedGuideKey: null, previewSessionId: null };
17
- const e = new URLSearchParams(a.location.search), t = e.get(g.GUIDE_KEY), s = e.get(
17
+ const e = new URLSearchParams(u.location.search), t = e.get(g.GUIDE_KEY), s = e.get(
18
18
  g.PREVIEW_SESSION_ID
19
19
  );
20
20
  if (t || s) {
21
- if (a.localStorage)
21
+ if (u.localStorage)
22
22
  try {
23
- const r = {
23
+ const i = {
24
24
  forcedGuideKey: t,
25
25
  previewSessionId: s
26
26
  };
27
- a.localStorage.setItem(p, JSON.stringify(r));
27
+ u.localStorage.setItem(p, JSON.stringify(i));
28
28
  } catch {
29
29
  }
30
30
  return {
@@ -32,23 +32,23 @@ const O = 50, $ = 30 * 1e3, T = 3, g = {
32
32
  previewSessionId: s
33
33
  };
34
34
  }
35
- let i = null, n = null;
36
- if (a.localStorage)
35
+ let r = null, n = null;
36
+ if (u.localStorage)
37
37
  try {
38
- const r = a.localStorage.getItem(p);
39
- if (r) {
40
- const o = F(r);
41
- i = o.forcedGuideKey, n = o.previewSessionId;
38
+ const i = u.localStorage.getItem(p);
39
+ if (i) {
40
+ const o = F(i);
41
+ r = o.forcedGuideKey, n = o.previewSessionId;
42
42
  }
43
43
  } catch {
44
44
  }
45
45
  return {
46
- forcedGuideKey: i,
46
+ forcedGuideKey: r,
47
47
  previewSessionId: n
48
48
  };
49
- }, F = (a) => {
49
+ }, F = (u) => {
50
50
  try {
51
- const e = JSON.parse(a);
51
+ const e = JSON.parse(u);
52
52
  return {
53
53
  forcedGuideKey: (e == null ? void 0 : e.forcedGuideKey) ?? null,
54
54
  previewSessionId: (e == null ? void 0 : e.previewSessionId) ?? null
@@ -59,39 +59,39 @@ const O = 50, $ = 30 * 1e3, T = 3, g = {
59
59
  previewSessionId: null
60
60
  };
61
61
  }
62
- }, m = (a, e = {}) => {
63
- const t = new A(), s = v(a.guideGroups);
62
+ }, m = (u, e = {}) => {
63
+ const t = new P(), s = A(u.guideGroups);
64
64
  if (!s) return t;
65
- const i = [...s.display_sequence], n = a.location;
66
- if (a.debug.forcedGuideKey) {
67
- const r = i.indexOf(a.debug.forcedGuideKey);
68
- r > -1 && i.splice(r, 1), i.unshift(a.debug.forcedGuideKey);
69
- }
70
- for (const [r, o] of i.entries()) {
71
- let u = a.guides[o];
72
- a.debug.forcedGuideKey === o && a.previewGuides[o] && (u = a.previewGuides[o]), !(!u || !N(u, {
65
+ const r = [...s.display_sequence], n = u.location;
66
+ if (u.debug.forcedGuideKey) {
67
+ const i = r.indexOf(u.debug.forcedGuideKey);
68
+ i > -1 && r.splice(i, 1), r.unshift(u.debug.forcedGuideKey);
69
+ }
70
+ for (const [i, o] of r.entries()) {
71
+ let a = u.guides[o];
72
+ u.debug.forcedGuideKey === o && u.previewGuides[o] && (a = u.previewGuides[o]), !(!a || !N(a, {
73
73
  location: n,
74
74
  filters: e,
75
- debug: a.debug
76
- })) && t.set(r, u);
75
+ debug: u.debug
76
+ })) && t.set(i, a);
77
77
  }
78
78
  return t.metadata = { guideGroup: s }, t;
79
- }, N = (a, { location: e, filters: t = {}, debug: s = {} }) => {
80
- if (t.type && t.type !== a.type || t.key && t.key !== a.key)
79
+ }, N = (u, { location: e, filters: t = {}, debug: s = {} }) => {
80
+ if (t.type && t.type !== u.type || t.key && t.key !== u.key)
81
81
  return !1;
82
- if (s.forcedGuideKey === a.key)
82
+ if (s.forcedGuideKey === u.key)
83
83
  return !0;
84
- if (!a.active || a.steps.every((o) => !!o.message.archived_at))
84
+ if (!u.active || u.steps.every((o) => !!o.message.archived_at))
85
85
  return !1;
86
- const i = e ? D(e) : void 0, n = a.activation_url_rules || [], r = a.activation_url_patterns || [];
87
- if (i && n.length > 0) {
88
- if (!K(i, n)) return !1;
89
- } else if (i && r.length > 0 && !L(i, r))
86
+ const r = e ? D(e) : void 0, n = u.activation_url_rules || [], i = u.activation_url_patterns || [];
87
+ if (r && n.length > 0) {
88
+ if (!K(r, n)) return !1;
89
+ } else if (r && i.length > 0 && !L(r, i))
90
90
  return !1;
91
91
  return !0;
92
92
  };
93
93
  class B {
94
- constructor(e, t, s = {}, i = {}) {
94
+ constructor(e, t, s = {}, r = {}) {
95
95
  d(this, "store");
96
96
  // Phoenix channels for real time guide updates over websocket
97
97
  d(this, "socket");
@@ -122,32 +122,32 @@ class B {
122
122
  const t = e.location.href;
123
123
  if (this.store.state.location === t) return;
124
124
  this.knock.log(`[Guide] Detected a location change: ${t}`);
125
- const s = this.store.state.debug, i = y();
126
- this.setLocation(t, { debug: i }), this.checkDebugStateChanged(
125
+ const s = this.store.state.debug, r = y();
126
+ this.setLocation(t, { debug: r }), this.checkDebugStateChanged(
127
127
  s,
128
- i
128
+ r
129
129
  ) && (this.knock.log(
130
130
  "[Guide] Debug state changed, refetching guides and resubscribing to the websocket channel"
131
131
  ), this.fetch(), this.subscribe());
132
132
  });
133
- this.knock = e, this.channelId = t, this.targetParams = s, this.options = i;
133
+ this.knock = e, this.channelId = t, this.targetParams = s, this.options = r;
134
134
  const {
135
135
  trackLocationFromWindow: n = !0,
136
- throttleCheckInterval: r = $
137
- } = i, o = l(), u = n ? o == null ? void 0 : o.location.href : void 0, h = y();
138
- this.store = new C({
136
+ throttleCheckInterval: i = T
137
+ } = r, o = l(), a = n ? o == null ? void 0 : o.location.href : void 0, h = y();
138
+ this.store = new I({
139
139
  guideGroups: [],
140
140
  guideGroupDisplayLogs: {},
141
141
  guides: {},
142
142
  previewGuides: {},
143
143
  queries: {},
144
- location: u,
144
+ location: a,
145
145
  // Increment to update the state store and trigger re-selection.
146
146
  counter: 0,
147
147
  debug: h
148
148
  });
149
149
  const { socket: c } = this.knock.client();
150
- this.socket = c, this.socketChannelTopic = `guides:${t}`, n && this.listenForLocationChangesFromWindow(), r && this.startCounterInterval(r), this.knock.log("[Guide] Initialized a guide client");
150
+ this.socket = c, this.socketChannelTopic = `guides:${t}`, n && this.listenForLocationChangesFromWindow(), i && this.startCounterInterval(i), this.knock.log("[Guide] Initialized a guide client");
151
151
  }
152
152
  incrementCounter() {
153
153
  this.knock.log("[Guide] Incrementing the counter"), this.store.setState((e) => ({ ...e, counter: e.counter + 1 }));
@@ -165,28 +165,28 @@ class B {
165
165
  }
166
166
  async fetch(e) {
167
167
  this.knock.log("[Guide] .fetch"), this.knock.failIfNotAuthenticated();
168
- const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), i = this.store.state.queries[s];
169
- if (i)
170
- return i;
171
- this.store.setState((r) => ({
172
- ...r,
173
- queries: { ...r.queries, [s]: { status: "loading" } }
168
+ const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), r = this.store.state.queries[s];
169
+ if (r)
170
+ return r;
171
+ this.store.setState((i) => ({
172
+ ...i,
173
+ queries: { ...i.queries, [s]: { status: "loading" } }
174
174
  }));
175
175
  let n;
176
176
  try {
177
177
  this.knock.log("[Guide] Fetching all eligible guides");
178
- const r = await this.knock.user.getGuides(this.channelId, t);
178
+ const i = await this.knock.user.getGuides(this.channelId, t);
179
179
  n = { status: "ok" };
180
- const { entries: o, guide_groups: u, guide_group_display_logs: h } = r;
180
+ const { entries: o, guide_groups: a, guide_group_display_logs: h } = i;
181
181
  this.knock.log("[Guide] Loading fetched guides"), this.store.setState((c) => ({
182
182
  ...c,
183
- guideGroups: (u == null ? void 0 : u.length) > 0 ? u : [R(o)],
183
+ guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [w(o)],
184
184
  guideGroupDisplayLogs: h || {},
185
- guides: w(o.map((_) => this.localCopy(_))),
185
+ guides: E(o.map((v) => this.localCopy(v))),
186
186
  queries: { ...c.queries, [s]: n }
187
187
  }));
188
- } catch (r) {
189
- n = { status: "error", error: r }, this.store.setState((o) => ({
188
+ } catch (i) {
189
+ n = { status: "error", error: i }, this.store.setState((o) => ({
190
190
  ...o,
191
191
  queries: { ...o.queries, [s]: n }
192
192
  }));
@@ -202,20 +202,20 @@ class B {
202
202
  force_all_guides: e.forcedGuideKey ? !0 : void 0,
203
203
  preview_session_id: e.previewSessionId || void 0
204
204
  }, s = this.socket.channel(this.socketChannelTopic, t);
205
- for (const i of this.socketEventTypes)
206
- s.on(i, (n) => this.handleSocketEvent(n));
205
+ for (const r of this.socketEventTypes)
206
+ s.on(r, (n) => this.handleSocketEvent(n));
207
207
  ["closed", "errored"].includes(s.state) && (this.subscribeRetryCount = 0, s.join().receive("ok", () => {
208
208
  this.knock.log("[Guide] Successfully joined channel");
209
- }).receive("error", (i) => {
209
+ }).receive("error", (r) => {
210
210
  this.knock.log(
211
- `[Guide] Failed to join channel: ${JSON.stringify(i)}`
211
+ `[Guide] Failed to join channel: ${JSON.stringify(r)}`
212
212
  ), this.handleChannelJoinError();
213
213
  }).receive("timeout", () => {
214
214
  this.knock.log("[Guide] Channel join timed out"), this.handleChannelJoinError();
215
215
  })), this.socketChannel = s;
216
216
  }
217
217
  handleChannelJoinError() {
218
- if (this.subscribeRetryCount >= T) {
218
+ if (this.subscribeRetryCount >= O) {
219
219
  this.knock.log(
220
220
  `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`
221
221
  ), this.unsubscribe();
@@ -252,11 +252,11 @@ class B {
252
252
  setLocation(e, t = {}) {
253
253
  this.knock.log(`[Guide] .setLocation (loc=${e})`), this.clearGroupStage(), this.knock.log("[Guide] Updating the tracked location"), this.store.setState((s) => {
254
254
  var n;
255
- const i = (n = t == null ? void 0 : t.debug) != null && n.previewSessionId ? s.previewGuides : {};
255
+ const r = (n = t == null ? void 0 : t.debug) != null && n.previewSessionId ? s.previewGuides : {};
256
256
  return {
257
257
  ...s,
258
258
  ...t,
259
- previewGuides: i,
259
+ previewGuides: r,
260
260
  location: e
261
261
  };
262
262
  });
@@ -282,63 +282,67 @@ class B {
282
282
  //
283
283
  // Store selector
284
284
  //
285
- selectGuides(e, t = {}) {
285
+ selectGuides(e, t = {}, s = {}) {
286
286
  if (this.knock.log(
287
287
  `[Guide] .selectGuides (filters: ${k(t)}; state: ${f(e)})`
288
- ), Object.keys(e.guides).length === 0 && Object.keys(e.previewGuides).length === 0)
289
- return this.knock.log("[Guide] Exiting selection (no guides)"), [];
290
- const s = m(e, t);
291
- return s.size === 0 ? (this.knock.log("[Guide] Selection returned zero result"), []) : [...s.values()];
288
+ ), !this.selectGuide(e, t, s))
289
+ return [];
290
+ const n = [...m(e, t).values()];
291
+ if (!s.includeThrottled && G(e)) {
292
+ const i = n.filter(
293
+ (a) => a.bypass_global_group_limit
294
+ ), o = n.length - i.length;
295
+ return this.knock.log(
296
+ `[Guide] Throttling ${o} guides from selection, and returning ${i.length} guides`
297
+ ), i;
298
+ }
299
+ return this.knock.log(`[Guide] Returning ${n.length} guides from selection`), n;
292
300
  }
293
- selectGuide(e, t = {}) {
301
+ selectGuide(e, t = {}, s = {}) {
294
302
  if (this.knock.log(
295
303
  `[Guide] .selectGuide (filters: ${k(t)}; state: ${f(e)})`
296
304
  ), Object.keys(e.guides).length === 0 && Object.keys(e.previewGuides).length === 0) {
297
305
  this.knock.log("[Guide] Exiting selection (no guides)");
298
306
  return;
299
307
  }
300
- const s = m(e, t);
301
- if (s.size === 0) {
308
+ const r = m(e, t);
309
+ if (r.size === 0) {
302
310
  this.knock.log("[Guide] Selection found zero result");
303
311
  return;
304
312
  }
305
- const [i, n] = [...s][0];
313
+ const [n, i] = [...r][0];
306
314
  if (this.knock.log(
307
- `[Guide] Selection found: \`${n.key}\` (total: ${s.size})`
308
- ), n.bypass_global_group_limit)
309
- return this.knock.log(`[Guide] Returning the unthrottled guide: ${n.key}`), n;
310
- const r = v(e.guideGroups), o = e.guideGroupDisplayLogs[S];
311
- if (r && r.display_interval && o && P(
312
- o,
313
- r.display_interval
314
- )) {
315
- this.knock.log(`[Guide] Throttling the selected guide: ${n.key}`);
315
+ `[Guide] Selection found: \`${i.key}\` (total: ${r.size})`
316
+ ), i.bypass_global_group_limit)
317
+ return this.knock.log(`[Guide] Returning the unthrottled guide: ${i.key}`), i;
318
+ if (!s.includeThrottled && G(e)) {
319
+ this.knock.log(`[Guide] Throttling the selected guide: ${i.key}`);
316
320
  return;
317
321
  }
318
322
  switch (this.stage || (this.stage = this.openGroupStage()), this.stage.status) {
319
323
  case "open": {
320
- this.knock.log(`[Guide] Adding to the group stage: ${n.key}`), this.stage.ordered[i] = n.key;
324
+ this.knock.log(`[Guide] Adding to the group stage: ${i.key}`), this.stage.ordered[n] = i.key;
321
325
  return;
322
326
  }
323
327
  case "patch": {
324
- this.knock.log(`[Guide] Patching the group stage: ${n.key}`), this.stage.ordered[i] = n.key;
325
- const u = this.stage.resolved === n.key ? n : void 0;
328
+ this.knock.log(`[Guide] Patching the group stage: ${i.key}`), this.stage.ordered[n] = i.key;
329
+ const o = this.stage.resolved === i.key ? i : void 0;
326
330
  return this.knock.log(
327
- `[Guide] Returning \`${u == null ? void 0 : u.key}\` (stage: ${G(this.stage)})`
328
- ), u;
331
+ `[Guide] Returning \`${o == null ? void 0 : o.key}\` (stage: ${S(this.stage)})`
332
+ ), o;
329
333
  }
330
334
  case "closed": {
331
- const u = this.stage.resolved === n.key ? n : void 0;
335
+ const o = this.stage.resolved === i.key ? i : void 0;
332
336
  return this.knock.log(
333
- `[Guide] Returning \`${u == null ? void 0 : u.key}\` (stage: ${G(this.stage)})`
334
- ), u;
337
+ `[Guide] Returning \`${o == null ? void 0 : o.key}\` (stage: ${S(this.stage)})`
338
+ ), o;
335
339
  }
336
340
  }
337
341
  }
338
342
  openGroupStage() {
339
343
  this.knock.log("[Guide] Opening a new group stage");
340
344
  const {
341
- orderResolutionDuration: e = O
345
+ orderResolutionDuration: e = $
342
346
  } = this.options, t = setTimeout(() => {
343
347
  this.closePendingGroupStage(), this.incrementCounter();
344
348
  }, e);
@@ -390,10 +394,13 @@ class B {
390
394
  var e;
391
395
  (e = this.stage) != null && e.timeoutId && clearTimeout(this.stage.timeoutId);
392
396
  }
393
- // Test helper that opens and closes the group stage to return the select
394
- // result immediately.
395
- _selectGuide(e, t = {}) {
396
- return this.openGroupStage(), this.selectGuide(e, t), this.closePendingGroupStage(), this.selectGuide(e, t);
397
+ // Test helpers to open and close the group stage to return the select result
398
+ // immediately.
399
+ _selectGuide(e, t = {}, s = {}) {
400
+ return this.openGroupStage(), this.selectGuide(e, t, s), this.closePendingGroupStage(), this.selectGuide(e, t, s);
401
+ }
402
+ _selectGuides(e, t = {}, s = {}) {
403
+ return this.openGroupStage(), this.selectGuides(e, t, s), this.closePendingGroupStage(), this.selectGuides(e, t, s);
397
404
  }
398
405
  //
399
406
  // Engagement event handlers
@@ -410,31 +417,30 @@ class B {
410
417
  seen_at: (/* @__PURE__ */ new Date()).toISOString()
411
418
  });
412
419
  if (!s) return;
413
- const i = {
420
+ const r = {
414
421
  ...this.buildEngagementEventBaseParams(e, s),
415
422
  content: s.content,
416
- data: this.targetParams.data,
417
- tenant: this.targetParams.tenant
423
+ data: this.targetParams.data
418
424
  };
419
425
  return this.knock.user.markGuideStepAs(
420
426
  "seen",
421
- i
427
+ r
422
428
  ), s;
423
429
  }
424
430
  async markAsInteracted(e, t, s) {
425
431
  this.knock.log(
426
432
  `[Guide] Marking as interacted (Guide key: ${e.key}; Step ref:${t.ref})`
427
433
  );
428
- const i = (/* @__PURE__ */ new Date()).toISOString(), n = this.setStepMessageAttrs(e.key, t.ref, {
429
- read_at: i,
430
- interacted_at: i
434
+ const r = (/* @__PURE__ */ new Date()).toISOString(), n = this.setStepMessageAttrs(e.key, t.ref, {
435
+ read_at: r,
436
+ interacted_at: r
431
437
  });
432
438
  if (!n) return;
433
- const r = {
439
+ const i = {
434
440
  ...this.buildEngagementEventBaseParams(e, n),
435
441
  metadata: s
436
442
  };
437
- return this.knock.user.markGuideStepAs("interacted", r), n;
443
+ return this.knock.user.markGuideStepAs("interacted", i), n;
438
444
  }
439
445
  async markAsArchived(e, t) {
440
446
  if (t.message.archived_at) return;
@@ -445,11 +451,11 @@ class B {
445
451
  archived_at: (/* @__PURE__ */ new Date()).toISOString()
446
452
  });
447
453
  if (!s) return;
448
- const i = this.buildEngagementEventBaseParams(e, s);
454
+ const r = this.buildEngagementEventBaseParams(e, s);
449
455
  return this.knock.user.markGuideStepAs(
450
456
  "archived",
451
457
  {
452
- ...i,
458
+ ...r,
453
459
  unthrottled: e.bypass_global_group_limit
454
460
  }
455
461
  ), s;
@@ -462,13 +468,13 @@ class B {
462
468
  ...e,
463
469
  // Get the next unarchived step.
464
470
  getStep() {
465
- return t.store.state.debug.forcedGuideKey === this.key ? this.steps[0] : this.steps.find((i) => !i.message.archived_at);
471
+ return t.store.state.debug.forcedGuideKey === this.key ? this.steps[0] : this.steps.find((r) => !r.message.archived_at);
466
472
  }
467
473
  };
468
- return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: i, ...n }) => {
469
- const r = {
474
+ return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: r, ...n }) => {
475
+ const i = {
470
476
  ...n,
471
- message: { ...i },
477
+ message: { ...r },
472
478
  markAsSeen() {
473
479
  if (!this.message.seen_at)
474
480
  return t.markAsSeen(s, this);
@@ -481,10 +487,10 @@ class B {
481
487
  return t.markAsArchived(s, this);
482
488
  }
483
489
  };
484
- return r.markAsSeen = r.markAsSeen.bind(r), r.markAsInteracted = r.markAsInteracted.bind(r), r.markAsArchived = r.markAsArchived.bind(r), r;
485
- }), s.activation_url_patterns = e.activation_url_patterns.map((i) => ({
486
- ...i,
487
- pattern: new E({ pathname: i.pathname })
490
+ return i.markAsSeen = i.markAsSeen.bind(i), i.markAsInteracted = i.markAsInteracted.bind(i), i.markAsArchived = i.markAsArchived.bind(i), i;
491
+ }), s.activation_url_patterns = e.activation_url_patterns.map((r) => ({
492
+ ...r,
493
+ pattern: new C({ pathname: r.pathname })
488
494
  })), s;
489
495
  }
490
496
  buildQueryParams(e = {}) {
@@ -493,77 +499,79 @@ class B {
493
499
  ...e
494
500
  };
495
501
  this.store.state.debug.forcedGuideKey && (t.force_all_guides = !0);
496
- let i = Object.fromEntries(
502
+ let r = Object.fromEntries(
497
503
  Object.entries(t).filter(
498
- ([n, r]) => r != null
504
+ ([n, i]) => i != null
499
505
  )
500
506
  );
501
- return i = i.data ? { ...i, data: JSON.stringify(i.data) } : i, i;
507
+ return r = r.data ? { ...r, data: JSON.stringify(r.data) } : r, r;
502
508
  }
503
509
  formatQueryKey(e) {
504
510
  const s = Object.keys(e).sort().map(
505
511
  (n) => `${encodeURIComponent(n)}=${encodeURIComponent(e[n])}`
506
- ).join("&"), i = U(this.knock.userId);
507
- return s ? `${i}?${s}` : i;
512
+ ).join("&"), r = U(this.knock.userId);
513
+ return s ? `${r}?${s}` : r;
508
514
  }
509
515
  setStepMessageAttrs(e, t, s) {
510
- let i;
516
+ let r;
511
517
  return s.archived_at && this.clearGroupStage(), this.store.setState((n) => {
512
- let r = n.guides[e];
513
- if (!r) return n;
514
- const o = r.steps.map((c) => (c.ref !== t || (c.message = { ...c.message, ...s }, i = c), c));
515
- r = i ? { ...r, steps: o } : r;
516
- const u = { ...n.guides, [r.key]: r }, h = s.archived_at && !r.bypass_global_group_limit ? {
518
+ let i = n.guides[e];
519
+ if (!i) return n;
520
+ const o = i.steps.map((c) => (c.ref !== t || (c.message = { ...c.message, ...s }, r = c), c));
521
+ i = r ? { ...i, steps: o } : i;
522
+ const a = { ...n.guides, [i.key]: i }, h = s.archived_at && !i.bypass_global_group_limit ? {
517
523
  ...n.guideGroupDisplayLogs,
518
- [S]: s.archived_at
524
+ [R]: s.archived_at
519
525
  } : n.guideGroupDisplayLogs;
520
- return { ...n, guides: u, guideGroupDisplayLogs: h };
521
- }), i;
526
+ return { ...n, guides: a, guideGroupDisplayLogs: h };
527
+ }), r;
522
528
  }
523
529
  buildEngagementEventBaseParams(e, t) {
524
530
  return {
525
531
  channel_id: e.channel_id,
526
532
  guide_key: e.key,
527
533
  guide_id: e.id,
528
- guide_step_ref: t.ref
534
+ guide_step_ref: t.ref,
535
+ // Can be used for scoping guide messages.
536
+ tenant: this.targetParams.tenant
529
537
  };
530
538
  }
531
539
  addOrReplaceGuide({ data: e }) {
532
540
  this.patchClosedGroupStage();
533
541
  const t = this.localCopy(e.guide);
534
542
  this.store.setState((s) => {
535
- const i = { ...s.guides, [t.key]: t };
536
- return { ...s, guides: i };
543
+ const r = { ...s.guides, [t.key]: t };
544
+ return { ...s, guides: r };
537
545
  });
538
546
  }
539
547
  removeGuide({ data: e }) {
540
548
  this.patchClosedGroupStage(), this.store.setState((t) => {
541
- const { [e.guide.key]: s, ...i } = t.guides;
542
- return { ...t, guides: i };
549
+ const { [e.guide.key]: s, ...r } = t.guides;
550
+ return { ...t, guides: r };
543
551
  });
544
552
  }
545
553
  addOrReplaceGuideGroup({
546
554
  data: e
547
555
  }) {
548
556
  this.patchClosedGroupStage(), this.store.setState((t) => {
549
- const s = [e.guide_group], i = e.guide_group.display_sequence_unthrottled || [], n = e.guide_group.display_sequence_throttled || [];
550
- let r = t.guides;
551
- return r = i.reduce((o, u) => {
552
- if (!o[u]) return o;
553
- const h = { ...o[u], bypass_global_group_limit: !0 };
554
- return { ...o, [u]: h };
555
- }, r), r = n.reduce((o, u) => {
556
- if (!o[u]) return o;
557
- const h = { ...o[u], bypass_global_group_limit: !1 };
558
- return { ...o, [u]: h };
559
- }, r), { ...t, guides: r, guideGroups: s };
557
+ const s = [e.guide_group], r = e.guide_group.display_sequence_unthrottled || [], n = e.guide_group.display_sequence_throttled || [];
558
+ let i = t.guides;
559
+ return i = r.reduce((o, a) => {
560
+ if (!o[a]) return o;
561
+ const h = { ...o[a], bypass_global_group_limit: !0 };
562
+ return { ...o, [a]: h };
563
+ }, i), i = n.reduce((o, a) => {
564
+ if (!o[a]) return o;
565
+ const h = { ...o[a], bypass_global_group_limit: !1 };
566
+ return { ...o, [a]: h };
567
+ }, i), { ...t, guides: i, guideGroups: s };
560
568
  });
561
569
  }
562
570
  updatePreviewGuide({ data: e }) {
563
571
  const t = this.localCopy(e.guide);
564
572
  this.store.setState((s) => {
565
- const i = { ...s.previewGuides, [t.key]: t };
566
- return { ...s, previewGuides: i };
573
+ const r = { ...s.previewGuides, [t.key]: t };
574
+ return { ...s, previewGuides: r };
567
575
  });
568
576
  }
569
577
  // Returns whether debug params have changed. For guide key, we only check
@@ -577,14 +585,14 @@ class B {
577
585
  e.addEventListener("popstate", this.handleLocationChange), e.addEventListener("hashchange", this.handleLocationChange);
578
586
  const t = e.history.pushState, s = e.history.replaceState;
579
587
  e.history.pushState = new Proxy(t, {
580
- apply: (i, n, r) => {
581
- Reflect.apply(i, n, r), setTimeout(() => {
588
+ apply: (r, n, i) => {
589
+ Reflect.apply(r, n, i), setTimeout(() => {
582
590
  this.handleLocationChange();
583
591
  }, 0);
584
592
  }
585
593
  }), e.history.replaceState = new Proxy(s, {
586
- apply: (i, n, r) => {
587
- Reflect.apply(i, n, r), setTimeout(() => {
594
+ apply: (r, n, i) => {
595
+ Reflect.apply(r, n, i), setTimeout(() => {
588
596
  this.handleLocationChange();
589
597
  }, 0);
590
598
  }