@suprsend/web-sdk 3.1.0 → 4.0.1

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.
package/dist/es/index.js CHANGED
@@ -1,11 +1,11 @@
1
- var K = Object.defineProperty;
2
- var W = (a, e, t) => e in a ? K(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
- var f = (a, e, t) => W(a, typeof e != "symbol" ? e + "" : e, t);
4
- import V from "mitt";
5
- import x from "jwt-decode";
1
+ var W = Object.defineProperty;
2
+ var H = (a, e, t) => e in a ? W(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
+ var f = (a, e, t) => H(a, typeof e != "symbol" ? e + "" : e, t);
4
+ import j from "mitt";
5
+ import F from "jwt-decode";
6
6
  import { createStore as z } from "zustand/vanilla";
7
7
  import { io as B } from "socket.io-client";
8
- var p = /* @__PURE__ */ ((a) => (a.OPT_IN = "opt_in", a.OPT_OUT = "opt_out", a))(p || {}), w = /* @__PURE__ */ ((a) => (a.ALL = "all", a.REQUIRED = "required", a))(w || {}), d = /* @__PURE__ */ ((a) => (a.VALIDATION_ERROR = "VALIDATION_ERROR", a.NETWORK_ERROR = "NETWORK_ERROR", a.UNKNOWN_ERROR = "UNKNOWN_ERROR", a.PERMISSION_DENIED = "PERMISSION_DENIED", a.UNSUPPORTED_ACTION = "UNSUPPORTED_ACTION", a.NOT_FOUND = "NOT_FOUND", a))(d || {}), n = /* @__PURE__ */ ((a) => (a.SUCCESS = "success", a.ERROR = "error", a))(n || {}), _ = /* @__PURE__ */ ((a) => (a.INITIAL = "INITIAL", a.LOADING = "LOADING", a.SUCCESS = "SUCCESS", a.ERROR = "ERROR", a.FETCHING_MORE = "FETCHING_MORE", a))(_ || {});
8
+ var p = /* @__PURE__ */ ((a) => (a.OPT_IN = "opt_in", a.OPT_OUT = "opt_out", a))(p || {}), w = /* @__PURE__ */ ((a) => (a.ALL = "all", a.REQUIRED = "required", a))(w || {}), c = /* @__PURE__ */ ((a) => (a.VALIDATION_ERROR = "VALIDATION_ERROR", a.NETWORK_ERROR = "NETWORK_ERROR", a.UNKNOWN_ERROR = "UNKNOWN_ERROR", a.PERMISSION_DENIED = "PERMISSION_DENIED", a.UNSUPPORTED_ACTION = "UNSUPPORTED_ACTION", a.NOT_FOUND = "NOT_FOUND", a))(c || {}), n = /* @__PURE__ */ ((a) => (a.SUCCESS = "success", a.ERROR = "error", a))(n || {}), _ = /* @__PURE__ */ ((a) => (a.INITIAL = "INITIAL", a.LOADING = "LOADING", a.SUCCESS = "SUCCESS", a.ERROR = "ERROR", a.FETCHING_MORE = "FETCHING_MORE", a))(_ || {});
9
9
  function C() {
10
10
  let a = (/* @__PURE__ */ new Date()).getTime();
11
11
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
@@ -16,32 +16,32 @@ function C() {
16
16
  }
17
17
  );
18
18
  }
19
- function U() {
19
+ function P() {
20
20
  return Math.round(Date.now());
21
21
  }
22
22
  function I(a) {
23
23
  return Object.keys(a).length === 0;
24
24
  }
25
- function H(a) {
25
+ function Q(a) {
26
26
  return (a == null ? void 0 : a.length) <= 0;
27
27
  }
28
- function Q(a) {
28
+ function G(a) {
29
29
  const e = "=".repeat((4 - a.length % 4) % 4), t = (a + e).replace(/-/g, "+").replace(/_/g, "/"), s = atob(t), r = new Uint8Array(s.length);
30
30
  for (let i = 0; i < s.length; ++i)
31
31
  r[i] = s.charCodeAt(i);
32
32
  return r;
33
33
  }
34
- function G(a, e) {
34
+ function J(a, e) {
35
35
  let t;
36
36
  return (...s) => (clearTimeout(t), new Promise((r) => {
37
37
  t = setTimeout(() => r(a(...s)), e);
38
38
  }));
39
39
  }
40
- function k(a, e) {
40
+ function x(a, e) {
41
41
  const t = {};
42
42
  return (...s) => {
43
43
  const [r] = s, i = s.slice(1);
44
- return typeof t[r] == "function" ? t[r](...i) : (t[r] = G(a, e), t[r](...i));
44
+ return typeof t[r] == "function" ? t[r](...i) : (t[r] = J(a, e), t[r](...i));
45
45
  };
46
46
  }
47
47
  function o(a) {
@@ -54,10 +54,10 @@ function o(a) {
54
54
  function A() {
55
55
  return typeof window < "u";
56
56
  }
57
- function $(a, e) {
57
+ function M(a, e) {
58
58
  A() && (typeof e == "object" && (e = JSON.stringify(e)), localStorage.setItem(a, e));
59
59
  }
60
- function j(a) {
60
+ function L(a) {
61
61
  if (!A()) return;
62
62
  const e = localStorage.getItem(a);
63
63
  if (e)
@@ -67,6 +67,13 @@ function j(a) {
67
67
  return e;
68
68
  }
69
69
  }
70
+ function Z(a) {
71
+ A() && localStorage.removeItem(a);
72
+ }
73
+ async function X(a) {
74
+ const t = new TextEncoder().encode(a), s = await crypto.subtle.digest("SHA-256", t);
75
+ return Array.from(new Uint8Array(s)).map((d) => d.toString(16).padStart(2, "0")).join("");
76
+ }
70
77
  class N {
71
78
  constructor(e) {
72
79
  f(this, "config");
@@ -116,12 +123,12 @@ class N {
116
123
  if (!this.config.distinctId)
117
124
  return o({
118
125
  status: n.ERROR,
119
- errorType: d.VALIDATION_ERROR,
126
+ errorType: c.VALIDATION_ERROR,
120
127
  errorMessage: "User isn't authenticated. Call identify method before performing any action"
121
128
  });
122
129
  if ((t = this.config.authenticateOptions) != null && t.refreshUserToken && this.config.userToken) {
123
- const i = x(this.config.userToken), c = (i.exp || 0) * 1e3, u = Date.now();
124
- if (c <= u)
130
+ const i = F(this.config.userToken), d = (i.exp || 0) * 1e3, u = Date.now();
131
+ if (d <= u)
125
132
  try {
126
133
  const h = await this.config.authenticateOptions.refreshUserToken(
127
134
  this.config.userToken,
@@ -136,25 +143,25 @@ class N {
136
143
  }
137
144
  }
138
145
  try {
139
- const i = await this.requestApiInstance(e), c = await i.json(), u = (c == null ? void 0 : c.status) || (i.ok ? n.SUCCESS : n.ERROR);
146
+ const i = await this.requestApiInstance(e), d = await i.json(), u = (d == null ? void 0 : d.status) || (i.ok ? n.SUCCESS : n.ERROR);
140
147
  return o({
141
148
  status: u,
142
- body: c,
149
+ body: d,
143
150
  statusCode: i.status,
144
- errorMessage: (s = c == null ? void 0 : c.error) == null ? void 0 : s.message,
145
- errorType: (r = c == null ? void 0 : c.error) == null ? void 0 : r.type
151
+ errorMessage: (s = d == null ? void 0 : d.error) == null ? void 0 : s.message,
152
+ errorType: (r = d == null ? void 0 : d.error) == null ? void 0 : r.type
146
153
  });
147
154
  } catch (i) {
148
155
  return console.error(i), o({
149
156
  status: n.ERROR,
150
157
  statusCode: 500,
151
158
  errorMessage: (i == null ? void 0 : i.message) || "network error",
152
- errorType: d.NETWORK_ERROR
159
+ errorType: c.NETWORK_ERROR
153
160
  });
154
161
  }
155
162
  }
156
163
  }
157
- class J {
164
+ class Y {
158
165
  constructor(e) {
159
166
  f(this, "config");
160
167
  f(this, "preferenceData");
@@ -162,10 +169,10 @@ class J {
162
169
  f(this, "debouncedUpdateCategoryPreferences");
163
170
  f(this, "debouncedUpdateChannelPreferences");
164
171
  f(this, "debounceTime", 1e3);
165
- this.config = e, this.debouncedUpdateCategoryPreferences = k(
172
+ this.config = e, this.debouncedUpdateCategoryPreferences = x(
166
173
  this._updateCategoryPreferences.bind(this),
167
174
  this.debounceTime
168
- ), this.debouncedUpdateChannelPreferences = k(
175
+ ), this.debouncedUpdateChannelPreferences = x(
169
176
  this._updateChannelPreferences.bind(this),
170
177
  this.debounceTime
171
178
  );
@@ -225,7 +232,7 @@ class J {
225
232
  if (!e)
226
233
  return o({
227
234
  status: n.ERROR,
228
- errorType: d.VALIDATION_ERROR,
235
+ errorType: c.VALIDATION_ERROR,
229
236
  errorMessage: "Category parameter is missing"
230
237
  });
231
238
  const s = {
@@ -242,16 +249,16 @@ class J {
242
249
  return await this.config.client().request({ type: "get", url: e });
243
250
  }
244
251
  async _updateCategoryPreferences(e, t, s, r) {
245
- const i = this.getUrl(`category/${e}`, r), c = await this.config.client().request({
252
+ const i = this.getUrl(`category/${e}`, r), d = await this.config.client().request({
246
253
  type: "patch",
247
254
  url: i,
248
255
  payload: t
249
256
  });
250
- return c != null && c.error ? this.config.emitter.emit("preferences_error", c) : (Object.assign(s, c.body), this.config.emitter.emit("preferences_updated", {
257
+ return d != null && d.error ? this.config.emitter.emit("preferences_error", d) : (Object.assign(s, d.body), this.config.emitter.emit("preferences_updated", {
251
258
  status: n.SUCCESS,
252
259
  statusCode: 200,
253
260
  body: this.data
254
- })), c;
261
+ })), d;
255
262
  }
256
263
  async _updateChannelPreferences(e) {
257
264
  const t = this.getUrl("channel_preference"), s = await this.config.client().request({
@@ -269,51 +276,51 @@ class J {
269
276
  * Used to update user's category level preference.
270
277
  */
271
278
  async updateCategoryPreference(e, t, s) {
272
- var h, T, b, S;
279
+ var h, S, v, T;
273
280
  if (!e || ![p.OPT_IN, p.OPT_OUT].includes(
274
281
  t
275
282
  ))
276
283
  return o({
277
284
  status: n.ERROR,
278
- errorType: d.VALIDATION_ERROR,
285
+ errorType: c.VALIDATION_ERROR,
279
286
  errorMessage: e ? "Preference parameter is invalid" : "Category parameter is missing"
280
287
  });
281
288
  if (!this.data)
282
289
  return o({
283
290
  status: n.ERROR,
284
- errorType: d.VALIDATION_ERROR,
291
+ errorType: c.VALIDATION_ERROR,
285
292
  errorMessage: "Call getPreferences method before performing action"
286
293
  });
287
294
  if (!this.data.sections)
288
295
  return o({
289
296
  status: n.ERROR,
290
- errorType: d.VALIDATION_ERROR,
297
+ errorType: c.VALIDATION_ERROR,
291
298
  errorMessage: "Sections doesn't exist"
292
299
  });
293
300
  let r = null, i = !1;
294
301
  for (const R of this.data.sections) {
295
- let O = !1;
302
+ let g = !1;
296
303
  if (R.subcategories) {
297
- for (const g of R.subcategories)
298
- if (g.category === e)
299
- if (r = g, g.is_editable) {
300
- if (g.preference !== t) {
301
- g.preference = t, i = !0, O = !0;
304
+ for (const O of R.subcategories)
305
+ if (O.category === e)
306
+ if (r = O, O.is_editable) {
307
+ if (O.preference !== t) {
308
+ O.preference = t, i = !0, g = !0;
302
309
  break;
303
310
  }
304
311
  } else
305
312
  return o({
306
313
  status: n.ERROR,
307
- errorType: d.VALIDATION_ERROR,
314
+ errorType: c.VALIDATION_ERROR,
308
315
  errorMessage: "Category preference is not editable"
309
316
  });
310
- if (O) break;
317
+ if (g) break;
311
318
  }
312
319
  }
313
320
  if (!r)
314
321
  return o({
315
322
  status: n.ERROR,
316
- errorType: d.VALIDATION_ERROR,
323
+ errorType: c.VALIDATION_ERROR,
317
324
  errorMessage: "Category not found"
318
325
  });
319
326
  if (!i)
@@ -321,15 +328,15 @@ class J {
321
328
  status: n.SUCCESS,
322
329
  body: this.data
323
330
  });
324
- const c = [];
331
+ const d = [];
325
332
  (h = r == null ? void 0 : r.channels) == null || h.forEach((R) => {
326
- R.preference === p.OPT_OUT && c.push(R.channel);
333
+ R.preference === p.OPT_OUT && d.push(R.channel);
327
334
  });
328
335
  let u = !0;
329
- typeof (s == null ? void 0 : s.showOptOutChannels) == "boolean" ? u = s == null ? void 0 : s.showOptOutChannels : typeof ((T = this.preferenceArgs) == null ? void 0 : T.showOptOutChannels) == "boolean" && (u = this.preferenceArgs.showOptOutChannels);
336
+ typeof (s == null ? void 0 : s.showOptOutChannels) == "boolean" ? u = s == null ? void 0 : s.showOptOutChannels : typeof ((S = this.preferenceArgs) == null ? void 0 : S.showOptOutChannels) == "boolean" && (u = this.preferenceArgs.showOptOutChannels);
330
337
  const l = {
331
338
  preference: r.preference,
332
- opt_out_channels: u && t === p.OPT_IN ? null : c
339
+ opt_out_channels: u && t === p.OPT_IN ? null : d
333
340
  };
334
341
  return this.debouncedUpdateCategoryPreferences(
335
342
  e,
@@ -337,9 +344,9 @@ class J {
337
344
  l,
338
345
  r,
339
346
  {
340
- tenant_id: (s == null ? void 0 : s.tenantId) || ((b = this.preferenceArgs) == null ? void 0 : b.tenantId),
347
+ tenant_id: (s == null ? void 0 : s.tenantId) || ((v = this.preferenceArgs) == null ? void 0 : v.tenantId),
341
348
  show_opt_out_channels: u,
342
- tags: (s == null ? void 0 : s.tags) || ((S = this.preferenceArgs) == null ? void 0 : S.tags)
349
+ tags: (s == null ? void 0 : s.tags) || ((T = this.preferenceArgs) == null ? void 0 : T.tags)
343
350
  }
344
351
  ), o({
345
352
  status: n.SUCCESS,
@@ -350,11 +357,11 @@ class J {
350
357
  * Used to update user's category level channel preference.
351
358
  */
352
359
  async updateChannelPreferenceInCategory(e, t, s, r) {
353
- var S, R, O, g;
360
+ var T, R, g, O;
354
361
  if (!e || !s)
355
362
  return o({
356
363
  status: n.ERROR,
357
- errorType: d.VALIDATION_ERROR,
364
+ errorType: c.VALIDATION_ERROR,
358
365
  errorMessage: e ? "Category parameter is missing" : "Channel parameter is missing"
359
366
  });
360
367
  if (![p.OPT_IN, p.OPT_OUT].includes(
@@ -362,57 +369,57 @@ class J {
362
369
  ))
363
370
  return o({
364
371
  status: n.ERROR,
365
- errorType: d.VALIDATION_ERROR,
372
+ errorType: c.VALIDATION_ERROR,
366
373
  errorMessage: "Preference parameter is invalid"
367
374
  });
368
375
  if (!this.data)
369
376
  return o({
370
377
  status: n.ERROR,
371
- errorType: d.VALIDATION_ERROR,
378
+ errorType: c.VALIDATION_ERROR,
372
379
  errorMessage: "Call getPreferences method before performing action"
373
380
  });
374
381
  if (!this.data.sections)
375
382
  return o({
376
383
  status: n.ERROR,
377
- errorType: d.VALIDATION_ERROR,
384
+ errorType: c.VALIDATION_ERROR,
378
385
  errorMessage: "Sections doesn't exist"
379
386
  });
380
- let i = null, c = null, u = !1;
381
- for (const m of this.data.sections) {
382
- let v = !1;
383
- if (m.subcategories) {
384
- for (const y of m.subcategories) {
385
- if (y.category === s) {
386
- if (i = y, !y.channels) continue;
387
- for (const E of y.channels)
387
+ let i = null, d = null, u = !1;
388
+ for (const y of this.data.sections) {
389
+ let b = !1;
390
+ if (y.subcategories) {
391
+ for (const m of y.subcategories) {
392
+ if (m.category === s) {
393
+ if (i = m, !m.channels) continue;
394
+ for (const E of m.channels)
388
395
  if (E.channel === e)
389
- if (c = E, E.is_editable) {
396
+ if (d = E, E.is_editable) {
390
397
  if (E.preference !== t) {
391
- E.preference = t, t === p.OPT_IN && (y.preference = p.OPT_IN), u = !0, v = !0;
398
+ E.preference = t, t === p.OPT_IN && (m.preference = p.OPT_IN), u = !0, b = !0;
392
399
  break;
393
400
  }
394
401
  } else
395
402
  return o({
396
403
  status: n.ERROR,
397
- errorType: d.VALIDATION_ERROR,
404
+ errorType: c.VALIDATION_ERROR,
398
405
  errorMessage: "Channel preference is not editable"
399
406
  });
400
407
  }
401
- if (v) break;
408
+ if (b) break;
402
409
  }
403
- if (v) break;
410
+ if (b) break;
404
411
  }
405
412
  }
406
413
  if (!i)
407
414
  return o({
408
415
  status: n.ERROR,
409
- errorType: d.VALIDATION_ERROR,
416
+ errorType: c.VALIDATION_ERROR,
410
417
  errorMessage: "Category not found"
411
418
  });
412
- if (!c)
419
+ if (!d)
413
420
  return o({
414
421
  status: n.ERROR,
415
- errorType: d.VALIDATION_ERROR,
422
+ errorType: c.VALIDATION_ERROR,
416
423
  errorMessage: "Category's channel not found"
417
424
  });
418
425
  if (!u)
@@ -421,24 +428,24 @@ class J {
421
428
  body: this.data
422
429
  });
423
430
  const l = [];
424
- (S = i == null ? void 0 : i.channels) == null || S.forEach((m) => {
425
- m.preference === p.OPT_OUT && l.push(m.channel);
431
+ (T = i == null ? void 0 : i.channels) == null || T.forEach((y) => {
432
+ y.preference === p.OPT_OUT && l.push(y.channel);
426
433
  });
427
434
  let h = !0;
428
435
  typeof (r == null ? void 0 : r.showOptOutChannels) == "boolean" ? h = r == null ? void 0 : r.showOptOutChannels : typeof ((R = this.preferenceArgs) == null ? void 0 : R.showOptOutChannels) == "boolean" && (h = this.preferenceArgs.showOptOutChannels);
429
- const b = {
436
+ const v = {
430
437
  preference: h && i.preference === p.OPT_OUT && t === p.OPT_IN ? p.OPT_IN : i.preference,
431
438
  opt_out_channels: l
432
439
  };
433
440
  return this.debouncedUpdateCategoryPreferences(
434
441
  s,
435
442
  s,
436
- b,
443
+ v,
437
444
  i,
438
445
  {
439
- tenant_id: (r == null ? void 0 : r.tenantId) || ((O = this.preferenceArgs) == null ? void 0 : O.tenantId),
446
+ tenant_id: (r == null ? void 0 : r.tenantId) || ((g = this.preferenceArgs) == null ? void 0 : g.tenantId),
440
447
  show_opt_out_channels: h,
441
- tags: (r == null ? void 0 : r.tags) || ((g = this.preferenceArgs) == null ? void 0 : g.tags)
448
+ tags: (r == null ? void 0 : r.tags) || ((O = this.preferenceArgs) == null ? void 0 : O.tags)
442
449
  }
443
450
  ), o({
444
451
  status: n.SUCCESS,
@@ -455,26 +462,26 @@ class J {
455
462
  ].includes(t))
456
463
  return o({
457
464
  status: n.ERROR,
458
- errorType: d.VALIDATION_ERROR,
465
+ errorType: c.VALIDATION_ERROR,
459
466
  errorMessage: e ? "Preference parameter is invalid" : "Channel parameter is missing"
460
467
  });
461
468
  if (!this.data)
462
469
  return o({
463
470
  status: n.ERROR,
464
- errorType: d.VALIDATION_ERROR,
471
+ errorType: c.VALIDATION_ERROR,
465
472
  errorMessage: "Call getPreferences method before performing action"
466
473
  });
467
474
  if (!this.data.channel_preferences)
468
475
  return o({
469
476
  status: n.ERROR,
470
- errorType: d.VALIDATION_ERROR,
477
+ errorType: c.VALIDATION_ERROR,
471
478
  errorMessage: "Channel preferences doesn't exist"
472
479
  });
473
480
  let s = null, r = !1;
474
481
  const i = t === w.REQUIRED;
475
- for (const c of this.data.channel_preferences)
476
- if (c.channel === e && (s = c, c.is_restricted !== i)) {
477
- c.is_restricted = i, r = !0;
482
+ for (const d of this.data.channel_preferences)
483
+ if (d.channel === e && (s = d, d.is_restricted !== i)) {
484
+ d.is_restricted = i, r = !0;
478
485
  break;
479
486
  }
480
487
  return s ? r ? (this.debouncedUpdateChannelPreferences(s.channel, {
@@ -487,17 +494,17 @@ class J {
487
494
  body: this.data
488
495
  }) : o({
489
496
  status: n.ERROR,
490
- errorType: d.VALIDATION_ERROR,
497
+ errorType: c.VALIDATION_ERROR,
491
498
  errorMessage: "Channel data not found"
492
499
  });
493
500
  }
494
501
  }
495
- const M = "ss_device_id";
496
- class Z {
502
+ const V = "ss_device_id";
503
+ class q {
497
504
  constructor(e) {
498
505
  f(this, "config");
499
506
  f(this, "preferences");
500
- this.config = e, this.preferences = new J(e);
507
+ this.config = e, this.preferences = new Y(e);
501
508
  }
502
509
  isReservedKey(e) {
503
510
  var t;
@@ -513,14 +520,14 @@ class Z {
513
520
  }
514
521
  validateObjData(e, t) {
515
522
  const s = {}, r = (t == null ? void 0 : t.allowReservedKeys) || !1, i = (t == null ? void 0 : t.valueType) || "";
516
- for (const c in e) {
517
- let u = e[c];
518
- if (!(c && u === void 0)) {
519
- if (!r && this.isReservedKey(c)) {
523
+ for (const d in e) {
524
+ let u = e[d];
525
+ if (!(d && u === void 0)) {
526
+ if (!r && this.isReservedKey(d)) {
520
527
  console.warn("[SuprSend]: key cannot start with $ or ss_");
521
528
  continue;
522
529
  }
523
- i === "number" ? u = Number(u) : i === "boolean" && (u = !!u), s[c] = u;
530
+ i === "number" ? u = Number(u) : i === "boolean" && (u = !!u), s[d] = u;
524
531
  }
525
532
  }
526
533
  return s;
@@ -541,7 +548,7 @@ class Z {
541
548
  return this.config.eventApi({
542
549
  distinct_id: this.config.distinctId,
543
550
  $insert_id: C(),
544
- $time: U(),
551
+ $time: P(),
545
552
  ...e
546
553
  });
547
554
  }
@@ -553,13 +560,13 @@ class Z {
553
560
  if (!s)
554
561
  return o({
555
562
  status: n.ERROR,
556
- errorType: d.VALIDATION_ERROR,
563
+ errorType: c.VALIDATION_ERROR,
557
564
  errorMessage: "data provided is empty"
558
565
  });
559
566
  const r = this.validateObjData(s);
560
567
  return I(r) ? o({
561
568
  status: n.ERROR,
562
- errorType: d.VALIDATION_ERROR,
569
+ errorType: c.VALIDATION_ERROR,
563
570
  errorMessage: "data provided is empty"
564
571
  }) : this.triggerUserEvent({ $set: r });
565
572
  }
@@ -572,13 +579,13 @@ class Z {
572
579
  if (!s)
573
580
  return o({
574
581
  status: n.ERROR,
575
- errorType: d.VALIDATION_ERROR,
582
+ errorType: c.VALIDATION_ERROR,
576
583
  errorMessage: "data provided is empty"
577
584
  });
578
585
  const r = this.validateObjData(s);
579
586
  return I(r) ? o({
580
587
  status: n.ERROR,
581
- errorType: d.VALIDATION_ERROR,
588
+ errorType: c.VALIDATION_ERROR,
582
589
  errorMessage: "data provided is empty"
583
590
  }) : this.triggerUserEvent({ $set_once: r });
584
591
  }
@@ -591,13 +598,13 @@ class Z {
591
598
  if (!s)
592
599
  return o({
593
600
  status: n.ERROR,
594
- errorType: d.VALIDATION_ERROR,
601
+ errorType: c.VALIDATION_ERROR,
595
602
  errorMessage: "data provided is empty"
596
603
  });
597
604
  const r = this.validateObjData(s, { valueType: "number" });
598
605
  return I(r) ? o({
599
606
  status: n.ERROR,
600
- errorType: d.VALIDATION_ERROR,
607
+ errorType: c.VALIDATION_ERROR,
601
608
  errorMessage: "data provided is empty"
602
609
  }) : this.triggerUserEvent({ $add: r });
603
610
  }
@@ -610,13 +617,13 @@ class Z {
610
617
  if (!s)
611
618
  return o({
612
619
  status: n.ERROR,
613
- errorType: d.VALIDATION_ERROR,
620
+ errorType: c.VALIDATION_ERROR,
614
621
  errorMessage: "data provided is empty"
615
622
  });
616
623
  const r = this.validateObjData(s);
617
624
  return I(r) ? o({
618
625
  status: n.ERROR,
619
- errorType: d.VALIDATION_ERROR,
626
+ errorType: c.VALIDATION_ERROR,
620
627
  errorMessage: "data provided is empty"
621
628
  }) : this.triggerUserEvent({ $append: r });
622
629
  }
@@ -629,13 +636,13 @@ class Z {
629
636
  if (!s)
630
637
  return o({
631
638
  status: n.ERROR,
632
- errorType: d.VALIDATION_ERROR,
639
+ errorType: c.VALIDATION_ERROR,
633
640
  errorMessage: "data provided is empty"
634
641
  });
635
642
  const r = this.validateObjData(s);
636
643
  return I(r) ? o({
637
644
  status: n.ERROR,
638
- errorType: d.VALIDATION_ERROR,
645
+ errorType: c.VALIDATION_ERROR,
639
646
  errorMessage: "data provided is empty"
640
647
  }) : this.triggerUserEvent({ $remove: r });
641
648
  }
@@ -647,13 +654,13 @@ class Z {
647
654
  if (!t)
648
655
  return o({
649
656
  status: n.ERROR,
650
- errorType: d.VALIDATION_ERROR,
657
+ errorType: c.VALIDATION_ERROR,
651
658
  errorMessage: "data provided is empty"
652
659
  });
653
660
  const s = this.validateArrayData(t);
654
- return H(s) ? o({
661
+ return Q(s) ? o({
655
662
  status: n.ERROR,
656
- errorType: d.VALIDATION_ERROR,
663
+ errorType: c.VALIDATION_ERROR,
657
664
  errorMessage: "data provided is empty"
658
665
  }) : this.triggerUserEvent({ $unset: s });
659
666
  }
@@ -663,7 +670,7 @@ class Z {
663
670
  if (!s)
664
671
  return o({
665
672
  status: n.ERROR,
666
- errorType: d.VALIDATION_ERROR,
673
+ errorType: c.VALIDATION_ERROR,
667
674
  errorMessage: "data provided is empty"
668
675
  });
669
676
  const r = this.validateObjData(s, {
@@ -671,7 +678,7 @@ class Z {
671
678
  });
672
679
  return I(r) ? o({
673
680
  status: n.ERROR,
674
- errorType: d.VALIDATION_ERROR,
681
+ errorType: c.VALIDATION_ERROR,
675
682
  errorMessage: "data provided is empty"
676
683
  }) : this.triggerUserEvent({ $append: r });
677
684
  }
@@ -681,7 +688,7 @@ class Z {
681
688
  if (!s)
682
689
  return o({
683
690
  status: n.ERROR,
684
- errorType: d.VALIDATION_ERROR,
691
+ errorType: c.VALIDATION_ERROR,
685
692
  errorMessage: "data provided is empty"
686
693
  });
687
694
  const r = this.validateObjData(s, {
@@ -689,7 +696,7 @@ class Z {
689
696
  });
690
697
  return I(r) ? o({
691
698
  status: n.ERROR,
692
- errorType: d.VALIDATION_ERROR,
699
+ errorType: c.VALIDATION_ERROR,
693
700
  errorMessage: "data provided is empty"
694
701
  }) : this.triggerUserEvent({ $remove: r });
695
702
  }
@@ -698,7 +705,7 @@ class Z {
698
705
  if (!s)
699
706
  return o({
700
707
  status: n.ERROR,
701
- errorType: d.VALIDATION_ERROR,
708
+ errorType: c.VALIDATION_ERROR,
702
709
  errorMessage: "data provided is empty"
703
710
  });
704
711
  const r = this.validateObjData(s, {
@@ -706,7 +713,7 @@ class Z {
706
713
  });
707
714
  return I(r) ? o({
708
715
  status: n.ERROR,
709
- errorType: d.VALIDATION_ERROR,
716
+ errorType: c.VALIDATION_ERROR,
710
717
  errorMessage: "data provided is empty"
711
718
  }) : this.triggerUserEvent({ $set: r });
712
719
  }
@@ -719,14 +726,14 @@ class Z {
719
726
  async addEmail(e) {
720
727
  return this.validateEmail(e) ? this.appendInternal({ $email: e }) : o({
721
728
  status: n.ERROR,
722
- errorType: d.VALIDATION_ERROR,
729
+ errorType: c.VALIDATION_ERROR,
723
730
  errorMessage: "provided email is invalid"
724
731
  });
725
732
  }
726
733
  async removeEmail(e) {
727
734
  return this.validateEmail(e) ? this.removeInternal({ $email: e }) : o({
728
735
  status: n.ERROR,
729
- errorType: d.VALIDATION_ERROR,
736
+ errorType: c.VALIDATION_ERROR,
730
737
  errorMessage: "provided email is invalid"
731
738
  });
732
739
  }
@@ -736,7 +743,7 @@ class Z {
736
743
  async addSms(e) {
737
744
  return this.validateMobile(e) ? this.appendInternal({ $sms: e }) : o({
738
745
  status: n.ERROR,
739
- errorType: d.VALIDATION_ERROR,
746
+ errorType: c.VALIDATION_ERROR,
740
747
  errorMessage: "provided mobile number is invalid, must be as per E.164 standard"
741
748
  });
742
749
  }
@@ -746,7 +753,7 @@ class Z {
746
753
  async removeSms(e) {
747
754
  return this.validateMobile(e) ? this.removeInternal({ $sms: e }) : o({
748
755
  status: n.ERROR,
749
- errorType: d.VALIDATION_ERROR,
756
+ errorType: c.VALIDATION_ERROR,
750
757
  errorMessage: "provided mobile number is invalid, must be as per E.164 standard"
751
758
  });
752
759
  }
@@ -756,7 +763,7 @@ class Z {
756
763
  async addWhatsapp(e) {
757
764
  return this.validateMobile(e) ? this.appendInternal({ $whatsapp: e }) : o({
758
765
  status: n.ERROR,
759
- errorType: d.VALIDATION_ERROR,
766
+ errorType: c.VALIDATION_ERROR,
760
767
  errorMessage: "provided mobile number is invalid, must be as per E.164 standard"
761
768
  });
762
769
  }
@@ -766,19 +773,19 @@ class Z {
766
773
  async removeWhatsapp(e) {
767
774
  return this.validateMobile(e) ? this.removeInternal({ $whatsapp: e }) : o({
768
775
  status: n.ERROR,
769
- errorType: d.VALIDATION_ERROR,
776
+ errorType: c.VALIDATION_ERROR,
770
777
  errorMessage: "provided mobile number is invalid, must be as per E.164 standard"
771
778
  });
772
779
  }
773
780
  getDeviceId() {
774
- let e = j(M);
775
- return e || (e = C(), $(M, e)), e;
781
+ let e = L(V);
782
+ return e || (e = C(), M(V, e)), e;
776
783
  }
777
784
  async addWebPush(e) {
778
785
  if (typeof e != "object")
779
786
  return o({
780
787
  status: n.ERROR,
781
- errorType: d.VALIDATION_ERROR,
788
+ errorType: c.VALIDATION_ERROR,
782
789
  errorMessage: "provided push subscription is invalid, must be an object"
783
790
  });
784
791
  const t = this.getDeviceId();
@@ -792,7 +799,7 @@ class Z {
792
799
  if (typeof e != "object")
793
800
  return o({
794
801
  status: n.ERROR,
795
- errorType: d.VALIDATION_ERROR,
802
+ errorType: c.VALIDATION_ERROR,
796
803
  errorMessage: "provided push subscription is invalid, must be an object"
797
804
  });
798
805
  const t = this.getDeviceId();
@@ -805,28 +812,28 @@ class Z {
805
812
  async addSlack(e) {
806
813
  return typeof e != "object" ? o({
807
814
  status: n.ERROR,
808
- errorType: d.VALIDATION_ERROR,
815
+ errorType: c.VALIDATION_ERROR,
809
816
  errorMessage: "provided slack data is invalid, must be an object"
810
817
  }) : this.appendInternal({ $slack: e });
811
818
  }
812
819
  async removeSlack(e) {
813
820
  return typeof e != "object" ? o({
814
821
  status: n.ERROR,
815
- errorType: d.VALIDATION_ERROR,
822
+ errorType: c.VALIDATION_ERROR,
816
823
  errorMessage: "provided slack data is invalid, must be an object"
817
824
  }) : this.removeInternal({ $slack: e });
818
825
  }
819
826
  async addMSTeams(e) {
820
827
  return typeof e != "object" ? o({
821
828
  status: n.ERROR,
822
- errorType: d.VALIDATION_ERROR,
829
+ errorType: c.VALIDATION_ERROR,
823
830
  errorMessage: "provided ms_teams data is invalid, must be object"
824
831
  }) : this.appendInternal({ $ms_teams: e });
825
832
  }
826
833
  async removeMSTeams(e) {
827
834
  return typeof e != "object" ? o({
828
835
  status: n.ERROR,
829
- errorType: d.VALIDATION_ERROR,
836
+ errorType: c.VALIDATION_ERROR,
830
837
  errorMessage: "provided ms_teams data is invalid, must be object"
831
838
  }) : this.removeInternal({ $ms_teams: e });
832
839
  }
@@ -837,7 +844,7 @@ class Z {
837
844
  async setPreferredLanguage(e) {
838
845
  return typeof e != "string" ? o({
839
846
  status: n.ERROR,
840
- errorType: d.VALIDATION_ERROR,
847
+ errorType: c.VALIDATION_ERROR,
841
848
  errorMessage: "provided language is invalid, must be string"
842
849
  }) : this.setInternal({ $preferred_language: e });
843
850
  }
@@ -847,12 +854,13 @@ class Z {
847
854
  async setTimezone(e) {
848
855
  return typeof e != "string" ? o({
849
856
  status: n.ERROR,
850
- errorType: d.VALIDATION_ERROR,
857
+ errorType: c.VALIDATION_ERROR,
851
858
  errorMessage: "provided timezone is invalid, must be string"
852
859
  }) : this.setInternal({ $timezone: e });
853
860
  }
854
861
  }
855
- class X {
862
+ const k = "ss_wp_hash";
863
+ class ee {
856
864
  constructor(e) {
857
865
  f(this, "config");
858
866
  this.config = e;
@@ -865,34 +873,48 @@ class X {
865
873
  if (t)
866
874
  return t;
867
875
  }
876
+ async checkAndUpdateOnServer(e) {
877
+ const t = e.endpoint;
878
+ let s = null;
879
+ try {
880
+ s = await X(t);
881
+ } catch {
882
+ }
883
+ if (s) {
884
+ if (s === L(k))
885
+ return o({ status: n.SUCCESS });
886
+ M(k, s);
887
+ }
888
+ return await this.config.user.addWebPush(e);
889
+ }
868
890
  async handleRegisterPush() {
869
891
  try {
870
892
  if (await navigator.serviceWorker.register(`/${this.config.swFileName}`), await Notification.requestPermission() !== "granted")
871
893
  return console.warn("[SuprSend]: Notification permission isnt granted"), o({
872
894
  status: n.ERROR,
873
- errorType: d.PERMISSION_DENIED,
895
+ errorType: c.PERMISSION_DENIED,
874
896
  errorMessage: "Notification permission isn't granted"
875
897
  });
876
898
  const t = await navigator.serviceWorker.ready, s = await t.pushManager.getSubscription();
877
899
  if (s)
878
- return this.config.user.addWebPush(s);
900
+ return this.checkAndUpdateOnServer(s);
879
901
  if (!this.config.vapidKey)
880
902
  return console.warn(
881
903
  "[SuprSend]: Vapid key is missing. Add it while creating SuprSend instance"
882
904
  ), o({
883
905
  status: n.ERROR,
884
- errorType: d.VALIDATION_ERROR,
906
+ errorType: c.VALIDATION_ERROR,
885
907
  errorMessage: "Vapid key is missing. Add it while creating SuprSend instance"
886
908
  });
887
909
  const r = await t.pushManager.subscribe({
888
910
  userVisibleOnly: !0,
889
- applicationServerKey: Q(this.config.vapidKey)
911
+ applicationServerKey: G(this.config.vapidKey)
890
912
  });
891
- return this.config.user.addWebPush(r);
913
+ return this.checkAndUpdateOnServer(r);
892
914
  } catch (e) {
893
915
  return console.warn("SuprSend: Error getting push subscription", e), o({
894
916
  status: n.ERROR,
895
- errorType: d.UNKNOWN_ERROR,
917
+ errorType: c.UNKNOWN_ERROR,
896
918
  errorMessage: (e == null ? void 0 : e.message) || "Unknown error occured while registering for push"
897
919
  });
898
920
  }
@@ -906,14 +928,14 @@ class X {
906
928
  async registerPush() {
907
929
  return A() && "serviceWorker" in navigator && "PushManager" in window ? this.handleRegisterPush() : (console.warn("[SuprSend]: Webpush isn't supported"), o({
908
930
  status: n.ERROR,
909
- errorType: d.UNSUPPORTED_ACTION,
931
+ errorType: c.UNSUPPORTED_ACTION,
910
932
  errorMessage: "Webpush isn't supported"
911
933
  }));
912
934
  }
913
935
  async updatePushSubscription() {
914
936
  const e = await this.getPushSubscription();
915
937
  if (e)
916
- return this.config.user.addWebPush(e);
938
+ return this.checkAndUpdateOnServer(e);
917
939
  }
918
940
  async removePushSubscription() {
919
941
  const e = await this.getPushSubscription();
@@ -933,38 +955,37 @@ class X {
933
955
  return !!await this.getPushSubscription();
934
956
  }
935
957
  }
936
- const F = 20, Y = "default", q = 100, D = {
958
+ const K = 20, te = "default", se = 100, D = {
937
959
  storeId: "$suprsend_default_store",
938
960
  label: ""
939
- }, ee = {
940
- tenantId: Y,
941
- pageSize: F,
961
+ }, re = {
962
+ tenantId: te,
963
+ pageSize: K,
942
964
  stores: null,
943
965
  host: {
944
966
  apiHost: "https://inboxs.live",
945
967
  socketHost: "https://betainbox.suprsend.com"
946
968
  }
947
- }, P = {
969
+ }, U = {
948
970
  notifications: [],
949
971
  store: D,
950
972
  pageInfo: {
951
973
  total: 0,
952
- currentPage: 0,
953
- totalPages: 0,
954
- pageSize: F
974
+ pageSize: K,
975
+ hasMore: !1
955
976
  },
956
977
  meta: { badge: 0 },
957
978
  apiStatus: _.INITIAL,
958
- _firstFetchedTimeStamp: null
979
+ isFirstFetch: !0
959
980
  };
960
- class te {
981
+ class ie {
961
982
  constructor(e) {
962
983
  f(this, "config");
963
984
  f(this, "feedInstances", []);
964
985
  this.config = e;
965
986
  }
966
987
  initialize(e = {}) {
967
- const t = new se(this.config, e);
988
+ const t = new ne(this.config, e);
968
989
  return this.feedInstances.push(t), t;
969
990
  }
970
991
  removeInstance(e) {
@@ -978,18 +999,18 @@ class te {
978
999
  this.feedInstances = [];
979
1000
  }
980
1001
  }
981
- class se {
1002
+ class ne {
982
1003
  constructor(e, t) {
983
1004
  f(this, "feedOptions");
984
1005
  f(this, "config");
985
1006
  f(this, "store");
986
1007
  f(this, "socket");
987
1008
  f(this, "expiryTimerId");
988
- f(this, "emitter", V());
1009
+ f(this, "emitter", j());
989
1010
  this.config = e, this.setOptions(t), this.store = this.createFeedStore();
990
1011
  }
991
1012
  setOptions(e) {
992
- this.feedOptions = { ...ee }, e != null && e.tenantId && (this.feedOptions.tenantId = e.tenantId), e != null && e.host && (this.feedOptions.host = e.host), typeof (e == null ? void 0 : e.pageSize) == "number" && e.pageSize > 0 && (this.feedOptions.pageSize = Math.min(e.pageSize, q)), e != null && e.stores && (this.feedOptions.stores = e.stores), this.validateStore();
1013
+ this.feedOptions = { ...re }, e != null && e.tenantId && (this.feedOptions.tenantId = e.tenantId), e != null && e.host && (this.feedOptions.host = e.host), typeof (e == null ? void 0 : e.pageSize) == "number" && e.pageSize > 0 && (this.feedOptions.pageSize = Math.min(e.pageSize, se)), e != null && e.stores && (this.feedOptions.stores = e.stores), this.validateStore();
993
1014
  }
994
1015
  validateStore() {
995
1016
  const e = this.feedOptions.stores;
@@ -1007,12 +1028,12 @@ class se {
1007
1028
  return;
1008
1029
  }
1009
1030
  const r = s == null ? void 0 : s.query;
1010
- let i, c, u = [], l = [];
1011
- typeof (r == null ? void 0 : r.read) == "boolean" && (i = r.read), typeof (r == null ? void 0 : r.archived) == "boolean" && (c = r.archived), typeof (r == null ? void 0 : r.tags) == "string" ? u = [r.tags] : Array.isArray(r == null ? void 0 : r.tags) && (u = r == null ? void 0 : r.tags.filter((h) => typeof h == "string")), typeof (r == null ? void 0 : r.categories) == "string" ? l = [r.categories] : Array.isArray(r == null ? void 0 : r.categories) && (l = r == null ? void 0 : r.categories.filter((h) => typeof h == "string")), t.push({
1031
+ let i, d, u = [], l = [];
1032
+ typeof (r == null ? void 0 : r.read) == "boolean" && (i = r.read), typeof (r == null ? void 0 : r.archived) == "boolean" && (d = r.archived), typeof (r == null ? void 0 : r.tags) == "string" ? u = [r.tags] : Array.isArray(r == null ? void 0 : r.tags) && (u = r == null ? void 0 : r.tags.filter((h) => typeof h == "string")), typeof (r == null ? void 0 : r.categories) == "string" ? l = [r.categories] : Array.isArray(r == null ? void 0 : r.categories) && (l = r == null ? void 0 : r.categories.filter((h) => typeof h == "string")), t.push({
1012
1033
  storeId: s.storeId,
1013
1034
  label: s.label || s.storeId,
1014
1035
  query: {
1015
- archived: c,
1036
+ archived: d,
1016
1037
  read: i,
1017
1038
  tags: u,
1018
1039
  categories: l
@@ -1024,7 +1045,7 @@ class se {
1024
1045
  return z()(() => {
1025
1046
  var e;
1026
1047
  return {
1027
- ...P,
1048
+ ...U,
1028
1049
  store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || D
1029
1050
  };
1030
1051
  });
@@ -1054,18 +1075,18 @@ class se {
1054
1075
  return;
1055
1076
  const s = t.body, r = this.store.getState();
1056
1077
  let i = !1;
1057
- const c = { ...r.meta };
1078
+ const d = { ...r.meta };
1058
1079
  this.notificationBelongToStore(s, r.store) && (i = !0, this.store.setState({
1059
1080
  notifications: this.orderNotificationsBasedOnPinFlag(
1060
1081
  s,
1061
1082
  r.notifications
1062
1083
  )
1063
1084
  })), (l = (u = this.feedOptions.stores) == null ? void 0 : u.map) == null || l.call(u, (h) => {
1064
- this.notificationBelongToStore(s, h) && (i = !0, c[h.storeId] = (r.meta[h.storeId] || 0) + 1);
1085
+ this.notificationBelongToStore(s, h) && (i = !0, d[h.storeId] = (r.meta[h.storeId] || 0) + 1);
1065
1086
  }), this.store.setState({
1066
1087
  meta: {
1067
- ...c,
1068
- badge: i ? c.badge + 1 : c.badge
1088
+ ...d,
1089
+ badge: i ? d.badge + 1 : d.badge
1069
1090
  }
1070
1091
  }), i && this.emitter.emit("feed.new_notification", s), this.emitter.emit("feed.store_update", this.data);
1071
1092
  }
@@ -1079,13 +1100,13 @@ class se {
1079
1100
  if (t[0].status !== "fulfilled") return;
1080
1101
  const r = t[0].value;
1081
1102
  if (r.status === n.ERROR) return;
1082
- const i = r.body, c = (l = s.notifications) == null ? void 0 : l.some(
1103
+ const i = r.body, d = (l = s.notifications) == null ? void 0 : l.some(
1083
1104
  (h) => h.n_id === i.n_id
1084
1105
  );
1085
1106
  this.notificationBelongToStore(
1086
1107
  i,
1087
1108
  s.store
1088
- ) ? c ? this.store.setState({
1109
+ ) ? d ? this.store.setState({
1089
1110
  notifications: s.notifications.map((h) => h.n_id === i.n_id ? i : h)
1090
1111
  }) : this.store.setState({
1091
1112
  notifications: this.orderNotificationsBasedOnPinFlag(
@@ -1113,12 +1134,12 @@ class se {
1113
1134
  }), this.emitter.emit("feed.store_update", this.data);
1114
1135
  }
1115
1136
  notificationBelongToStore(e, t) {
1116
- var g, m, v, y;
1117
- const s = !!e.read_on, r = e.archived, i = e.tags, c = e.n_category, u = (g = t == null ? void 0 : t.query) == null ? void 0 : g.read, l = (m = t == null ? void 0 : t.query) == null ? void 0 : m.archived, h = (v = t == null ? void 0 : t.query) == null ? void 0 : v.tags, T = (y = t == null ? void 0 : t.query) == null ? void 0 : y.categories, b = u == null || s === u, S = !!r == !!l;
1118
- let R = !1, O = !1;
1137
+ var O, y, b, m;
1138
+ const s = !!e.read_on, r = e.archived, i = e.tags, d = e.n_category, u = (O = t == null ? void 0 : t.query) == null ? void 0 : O.read, l = (y = t == null ? void 0 : t.query) == null ? void 0 : y.archived, h = (b = t == null ? void 0 : t.query) == null ? void 0 : b.tags, S = (m = t == null ? void 0 : t.query) == null ? void 0 : m.categories, v = u == null || s === u, T = !!r == !!l;
1139
+ let R = !1, g = !1;
1119
1140
  return Array.isArray(h) && h.length > 0 ? h.forEach((E) => {
1120
1141
  i != null && i.includes(E) && (R = !0);
1121
- }) : R = !0, Array.isArray(T) && T.length > 0 ? T.includes(c) && (O = !0) : O = !0, b && R && O && S;
1142
+ }) : R = !0, Array.isArray(S) && S.length > 0 ? S.includes(d) && (g = !0) : g = !0, v && R && g && T;
1122
1143
  }
1123
1144
  orderNotificationsBasedOnPinFlag(e, t) {
1124
1145
  if (e.is_pinned)
@@ -1143,8 +1164,8 @@ class se {
1143
1164
  t && (this.store.setState({ notifications: s }), await this.fetchCount(), this.emitter.emit("feed.store_update", this.data));
1144
1165
  }
1145
1166
  getUrl(e, t) {
1146
- var c;
1147
- const s = `${(c = this.feedOptions.host) == null ? void 0 : c.apiHost}/v1/feed/${e}`, r = this.validateQueryParams(t), i = new URLSearchParams(
1167
+ var d;
1168
+ const s = `${(d = this.feedOptions.host) == null ? void 0 : d.apiHost}/v1/feed/${e}`, r = this.validateQueryParams(t), i = new URLSearchParams(
1148
1169
  r
1149
1170
  ).toString();
1150
1171
  return i ? `${s}?${i}` : s;
@@ -1168,12 +1189,12 @@ class se {
1168
1189
  return e == null ? void 0 : e.map((s) => this.storeQueryParamObj(s));
1169
1190
  }
1170
1191
  storeQueryParamObj(e) {
1171
- const t = e == null ? void 0 : e.query, s = (t == null ? void 0 : t.tags) || [], r = (t == null ? void 0 : t.categories) || [], i = t == null ? void 0 : t.read, c = t == null ? void 0 : t.archived;
1192
+ const t = e == null ? void 0 : e.query, s = (t == null ? void 0 : t.tags) || [], r = (t == null ? void 0 : t.categories) || [], i = t == null ? void 0 : t.read, d = t == null ? void 0 : t.archived;
1172
1193
  return {
1173
1194
  store_id: e.storeId,
1174
1195
  query: {
1175
1196
  read: i,
1176
- archived: c,
1197
+ archived: d,
1177
1198
  tags: { or: s },
1178
1199
  categories: { or: r }
1179
1200
  }
@@ -1187,13 +1208,13 @@ class se {
1187
1208
  (i) => i.storeId === e
1188
1209
  );
1189
1210
  return s ? (this.store.setState({
1190
- ...P,
1211
+ ...U,
1191
1212
  store: s,
1192
1213
  meta: t.meta
1193
1214
  }), await this.fetch()) : {
1194
1215
  status: n.ERROR,
1195
1216
  error: {
1196
- type: d.NOT_FOUND,
1217
+ type: c.NOT_FOUND,
1197
1218
  message: `store with storeId ${e} doesnt exist`
1198
1219
  }
1199
1220
  };
@@ -1228,45 +1249,47 @@ class se {
1228
1249
  async fetch(e = {}) {
1229
1250
  const t = this.store.getState();
1230
1251
  if (this.requestInprogress()) return;
1231
- const s = (e == null ? void 0 : e.page) || 1, r = (e == null ? void 0 : e.pageSize) || this.feedOptions.pageSize, i = t._firstFetchedTimeStamp || Date.now();
1232
- s > 1 ? this.store.setState({
1233
- apiStatus: _.FETCHING_MORE
1234
- }) : (this.store.setState({
1252
+ const s = (e == null ? void 0 : e.pageSize) || this.feedOptions.pageSize;
1253
+ t.isFirstFetch ? (this.store.setState({
1235
1254
  apiStatus: _.LOADING
1236
- }), this.fetchCount()), this.emitter.emit("feed.store_update", this.data);
1237
- const c = {
1255
+ }), this.fetchCount()) : this.store.setState({
1256
+ apiStatus: _.FETCHING_MORE
1257
+ }), this.emitter.emit("feed.store_update", this.data);
1258
+ const r = {
1238
1259
  distinct_id: this.config.distinctId,
1239
1260
  tenant_id: this.feedOptions.tenantId,
1240
- page_size: r,
1241
- page_no: s,
1242
- before: i,
1261
+ page_size: s,
1243
1262
  store: t.store.storeId !== D.storeId ? this.storeQueryParamObj(t.store) : null
1244
- }, u = this.getUrl("notifications", c), l = await this.config.client().request({ type: "get", url: u });
1245
- if (l.status === n.ERROR)
1246
- return this.store.setState({ apiStatus: _.ERROR }), this.emitter.emit("feed.store_update", this.data), l;
1247
- const h = l.body.meta.current_page === 1;
1248
- return this.store.setState({
1263
+ };
1264
+ if (t.notifications.length > 0) {
1265
+ const u = t.notifications[t.notifications.length - 1];
1266
+ r.search_after = [
1267
+ u.is_pinned,
1268
+ u.created_on
1269
+ ];
1270
+ } else
1271
+ r.search_after = [];
1272
+ const i = this.getUrl("notifications", r), d = await this.config.client().request({ type: "get", url: i });
1273
+ return d.status === n.ERROR ? (this.store.setState({ apiStatus: _.ERROR }), this.emitter.emit("feed.store_update", this.data), d) : (this.store.setState({
1249
1274
  apiStatus: _.SUCCESS,
1250
- notifications: h ? l.body.results : [...t.notifications, ...l.body.results],
1275
+ notifications: t.isFirstFetch ? d.body.results : [...t.notifications, ...d.body.results],
1251
1276
  pageInfo: {
1252
1277
  ...t.pageInfo,
1253
- total: l.body.meta.total_count,
1254
- currentPage: l.body.meta.current_page,
1255
- totalPages: l.body.meta.total_pages
1278
+ total: d.body.meta.total_count,
1279
+ hasMore: d.body.meta.has_more
1256
1280
  },
1257
- _firstFetchedTimeStamp: i
1258
- }), this.emitter.emit("feed.store_update", this.data), this.startExpiryTimer(), l;
1281
+ isFirstFetch: !1
1282
+ }), this.emitter.emit("feed.store_update", this.data), this.startExpiryTimer(), d);
1259
1283
  }
1260
1284
  // TODO: support other stores
1261
1285
  async fetchNextPage() {
1262
- const e = this.store.getState();
1263
- return e.pageInfo.currentPage >= e.pageInfo.totalPages ? {
1286
+ return this.store.getState().pageInfo.hasMore === !1 ? {
1264
1287
  status: n.ERROR,
1265
1288
  error: {
1266
- type: d.VALIDATION_ERROR,
1289
+ type: c.VALIDATION_ERROR,
1267
1290
  message: "No more pages to fetch"
1268
1291
  }
1269
- } : this.fetch({ page: e.pageInfo.currentPage + 1 });
1292
+ } : this.fetch();
1270
1293
  }
1271
1294
  async fetchCount() {
1272
1295
  const e = {
@@ -1382,7 +1405,7 @@ class se {
1382
1405
  reset() {
1383
1406
  var e;
1384
1407
  this.store.setState({
1385
- ...P,
1408
+ ...U,
1386
1409
  store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || D
1387
1410
  }), this.emitter.emit("feed.store_update", this.data), this.expiryTimerId && (clearInterval(this.expiryTimerId), this.expiryTimerId = void 0);
1388
1411
  }
@@ -1391,8 +1414,8 @@ class se {
1391
1414
  this.reset(), this.emitter.off("*"), (e = this.socket) == null || e.disconnect(), this.config.feeds.removeInstance(this);
1392
1415
  }
1393
1416
  }
1394
- const re = "https://hub.suprsend.com", ie = "serviceworker.js", L = "ss_distinct_id";
1395
- class ue {
1417
+ const ae = "https://hub.suprsend.com", oe = "serviceworker.js", $ = "ss_distinct_id";
1418
+ class le {
1396
1419
  constructor(e, t) {
1397
1420
  f(this, "host");
1398
1421
  f(this, "publicApiKey");
@@ -1403,19 +1426,19 @@ class ue {
1403
1426
  f(this, "apiClient", null);
1404
1427
  f(this, "userTokenExpirationTimer", null);
1405
1428
  f(this, "authenticateOptions");
1406
- f(this, "user", new Z(this));
1407
- f(this, "webpush", new X(this));
1408
- f(this, "feeds", new te(this));
1409
- f(this, "emitter", V());
1429
+ f(this, "user", new q(this));
1430
+ f(this, "webpush", new ee(this));
1431
+ f(this, "feeds", new ie(this));
1432
+ f(this, "emitter", j());
1410
1433
  if (!e)
1411
1434
  throw new Error("[SuprSend]: publicApiKey is missing");
1412
- this.publicApiKey = e, this.host = (t == null ? void 0 : t.host) || re, this.vapidKey = (t == null ? void 0 : t.vapidKey) || "", this.swFileName = (t == null ? void 0 : t.swFileName) || ie;
1435
+ this.publicApiKey = e, this.host = (t == null ? void 0 : t.host) || ae, this.vapidKey = (t == null ? void 0 : t.vapidKey) || "", this.swFileName = (t == null ? void 0 : t.swFileName) || oe;
1413
1436
  }
1414
1437
  handleRefreshUserToken(e) {
1415
1438
  if (!this.userToken || !A()) return;
1416
- const t = x(this.userToken), s = (t.exp || 0) * 1e3, r = Date.now(), i = 1e3 * 30;
1439
+ const t = F(this.userToken), s = (t.exp || 0) * 1e3, r = Date.now(), i = 1e3 * 30;
1417
1440
  if (s && s > r) {
1418
- const c = s - r - i;
1441
+ const d = s - r - i;
1419
1442
  this.userTokenExpirationTimer && clearTimeout(this.userTokenExpirationTimer), this.userTokenExpirationTimer = setTimeout(async () => {
1420
1443
  let u = "";
1421
1444
  try {
@@ -1434,7 +1457,7 @@ class ue {
1434
1457
  }
1435
1458
  }
1436
1459
  u && typeof u == "string" && this.identify(this.distinctId, u, this.authenticateOptions);
1437
- }, c);
1460
+ }, d);
1438
1461
  }
1439
1462
  }
1440
1463
  client() {
@@ -1457,13 +1480,13 @@ class ue {
1457
1480
  if (!e)
1458
1481
  return o({
1459
1482
  status: n.ERROR,
1460
- errorType: d.VALIDATION_ERROR,
1483
+ errorType: c.VALIDATION_ERROR,
1461
1484
  errorMessage: "distinctId is missing"
1462
1485
  });
1463
1486
  if (this.apiClient && this.distinctId && this.distinctId !== e)
1464
1487
  return o({
1465
1488
  status: n.ERROR,
1466
- errorType: d.VALIDATION_ERROR,
1489
+ errorType: c.VALIDATION_ERROR,
1467
1490
  errorMessage: "User already loggedin, reset current user to login new user"
1468
1491
  });
1469
1492
  if (this.apiClient && this.distinctId === e && this.userToken !== t)
@@ -1471,20 +1494,20 @@ class ue {
1471
1494
  if (this.distinctId && this.apiClient)
1472
1495
  return o({ status: n.SUCCESS });
1473
1496
  this.distinctId = e, this.userToken = t, this.apiClient = new N(this), this.authenticateOptions = s;
1474
- const r = j(
1475
- L
1497
+ const r = L(
1498
+ $
1476
1499
  );
1477
1500
  if (s != null && s.refreshUserToken && this.handleRefreshUserToken(s.refreshUserToken), r == this.distinctId)
1478
1501
  return this.webpush.updatePushSubscription(), o({ status: n.SUCCESS });
1479
1502
  const i = await this.eventApi({
1480
1503
  event: "$identify",
1481
1504
  $insert_id: C(),
1482
- $time: U(),
1505
+ $time: P(),
1483
1506
  properties: {
1484
1507
  $identified_id: e
1485
1508
  }
1486
1509
  });
1487
- return i.status === n.SUCCESS ? (this.webpush.updatePushSubscription(), $(L, this.distinctId)) : this.reset({ unsubscribePush: !1 }), i;
1510
+ return i.status === n.SUCCESS ? (this.webpush.updatePushSubscription(), M($, this.distinctId)) : this.reset({ unsubscribePush: !1 }), i;
1488
1511
  }
1489
1512
  /**
1490
1513
  * Check's if SuprSend instance is authenticated. To check if userToken is also present pass true.
@@ -1500,12 +1523,12 @@ class ue {
1500
1523
  return e ? (typeof t == "object" && (s = { ...s, ...t }), this.eventApi({
1501
1524
  event: String(e),
1502
1525
  $insert_id: C(),
1503
- $time: U(),
1526
+ $time: P(),
1504
1527
  distinct_id: this.distinctId,
1505
1528
  properties: s
1506
1529
  })) : o({
1507
1530
  status: n.ERROR,
1508
- errorType: d.VALIDATION_ERROR,
1531
+ errorType: c.VALIDATION_ERROR,
1509
1532
  errorMessage: "event name is missing"
1510
1533
  });
1511
1534
  }
@@ -1514,16 +1537,16 @@ class ue {
1514
1537
  */
1515
1538
  async reset(e) {
1516
1539
  var s, r;
1517
- return (e == null ? void 0 : e.unsubscribePush) !== !1 && await ((s = this.webpush) == null ? void 0 : s.removePushSubscription()), this.apiClient = null, this.distinctId = null, this.userToken = "", this.userTokenExpirationTimer && clearTimeout(this.userTokenExpirationTimer), ((r = this.feeds.feedInstances) == null ? void 0 : r.length) > 0 && this.feeds.removeAll(), o({ status: n.SUCCESS });
1540
+ return (e == null ? void 0 : e.unsubscribePush) !== !1 && (await ((s = this.webpush) == null ? void 0 : s.removePushSubscription()), Z(k)), this.apiClient = null, this.distinctId = null, this.userToken = "", this.userTokenExpirationTimer && clearTimeout(this.userTokenExpirationTimer), ((r = this.feeds.feedInstances) == null ? void 0 : r.length) > 0 && this.feeds.removeAll(), o({ status: n.SUCCESS });
1518
1541
  }
1519
1542
  }
1520
1543
  export {
1521
1544
  _ as ApiResponseStatus,
1522
1545
  w as ChannelLevelPreferenceOptions,
1523
- d as ERROR_TYPE,
1524
- se as Feed,
1546
+ c as ERROR_TYPE,
1547
+ ne as Feed,
1525
1548
  p as PreferenceOptions,
1526
1549
  n as RESPONSE_STATUS,
1527
- ue as SuprSend
1550
+ le as SuprSend
1528
1551
  };
1529
1552
  //# sourceMappingURL=index.js.map