@braze/web-sdk 6.12.0 → 6.13.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.
Files changed (149) hide show
  1. package/index.d.ts +240 -3
  2. package/package.json +1 -1
  3. package/shared-lib/event-types.js +18 -18
  4. package/shared-lib/indexed-db-adapter.js +58 -58
  5. package/shared-lib/logger.js +7 -7
  6. package/shared-lib/supported-options.js +25 -24
  7. package/src/ConversationalChat/automatically-manage-chat.js +5 -0
  8. package/src/ConversationalChat/chat-message-factory.js +25 -0
  9. package/src/ConversationalChat/chat-message.js +33 -0
  10. package/src/ConversationalChat/constants.js +1 -0
  11. package/src/ConversationalChat/conversational-chat-provider-factory.js +26 -0
  12. package/src/ConversationalChat/conversational-chat-provider.js +372 -0
  13. package/src/ConversationalChat/display/chat-widget-manager.js +113 -0
  14. package/src/ConversationalChat/display/chat-widget-to-html.js +58 -0
  15. package/src/ConversationalChat/get-chat-messages.js +7 -0
  16. package/src/ConversationalChat/index.js +12 -0
  17. package/src/ConversationalChat/is-chat-conversation-ended.js +7 -0
  18. package/src/ConversationalChat/is-chat-turn-errored.js +7 -0
  19. package/src/ConversationalChat/is-chat-turn-in-progress.js +7 -0
  20. package/src/ConversationalChat/register-chat-action-handler.js +10 -0
  21. package/src/ConversationalChat/reset-chat-conversation.js +5 -0
  22. package/src/ConversationalChat/retrieve-chat-config.js +7 -0
  23. package/src/ConversationalChat/send-chat-message.js +17 -0
  24. package/src/ConversationalChat/subscribe-to-chat-action.js +5 -0
  25. package/src/ConversationalChat/subscribe-to-chat-conversation-updated.js +5 -0
  26. package/src/ConversationalChat/subscribe-to-chat-message-received.js +5 -0
  27. package/src/ConversationalChat/trigger-chat-action.js +11 -0
  28. package/src/ConversationalChat/types.js +1 -0
  29. package/src/Core/add-sdk-metadata.js +5 -5
  30. package/src/Core/change-user.js +6 -6
  31. package/src/Core/disable-sdk.js +6 -6
  32. package/src/Core/enable-sdk.js +3 -3
  33. package/src/Core/get-device-id.js +2 -2
  34. package/src/Core/handle-braze-action.js +20 -13
  35. package/src/Core/is-disabled.js +1 -1
  36. package/src/Core/is-initialized.js +1 -1
  37. package/src/Core/log-custom-event.js +9 -9
  38. package/src/Core/log-ecommerce-event.js +4 -4
  39. package/src/Core/log-purchase.js +14 -14
  40. package/src/Core/open-session.js +8 -8
  41. package/src/Core/set-sdk-authentication-signature.js +3 -3
  42. package/src/Core/subscribe-to-sdk-authentication-failures.js +1 -1
  43. package/src/Core/wipe-data.js +1 -1
  44. package/src/DUST/dust-config-store.js +35 -35
  45. package/src/DUST/dust-message-dedup.js +3 -3
  46. package/src/DUST/dust-provider-factory.js +10 -10
  47. package/src/DUST/dust-provider.js +234 -234
  48. package/src/DUST/subscribe-to-dust.js +7 -7
  49. package/src/FeatureFlags/feature-flag-factory.js +2 -2
  50. package/src/FeatureFlags/feature-flag.js +4 -4
  51. package/src/FeatureFlags/feature-flags-provider-factory.js +11 -11
  52. package/src/FeatureFlags/feature-flags-provider.js +38 -38
  53. package/src/FeatureFlags/get-all-feature-flags.js +3 -3
  54. package/src/FeatureFlags/get-feature-flag.js +3 -3
  55. package/src/FeatureFlags/log-feature-flag-impression.js +4 -4
  56. package/src/FeatureFlags/refresh-feature-flags.js +5 -5
  57. package/src/FeatureFlags/subscribe-to-feature-flags-updates.js +3 -3
  58. package/src/InAppMessage/defer-in-app-message.js +1 -1
  59. package/src/InAppMessage/display/html-message-display-utils.js +4 -4
  60. package/src/InAppMessage/display/html-message-to-html.js +13 -13
  61. package/src/InAppMessage/display/in-app-message-to-html.js +20 -20
  62. package/src/InAppMessage/display/modal-utils.js +7 -7
  63. package/src/InAppMessage/in-app-message-factory.js +6 -6
  64. package/src/InAppMessage/in-app-message-manager.js +92 -92
  65. package/src/InAppMessage/log-in-app-message-button-click.js +9 -9
  66. package/src/InAppMessage/log-in-app-message-click.js +7 -7
  67. package/src/InAppMessage/log-in-app-message-html-click.js +6 -6
  68. package/src/InAppMessage/log-in-app-message-impression.js +3 -3
  69. package/src/InAppMessage/models/full-screen-message.js +39 -39
  70. package/src/InAppMessage/models/html-message.js +20 -20
  71. package/src/InAppMessage/models/in-app-message-button.js +2 -2
  72. package/src/InAppMessage/models/in-app-message.js +102 -102
  73. package/src/InAppMessage/models/modal-message.js +38 -38
  74. package/src/InAppMessage/models/slide-up-message.js +34 -34
  75. package/src/InAppMessage/models/templated-in-app-message.js +8 -8
  76. package/src/InAppMessage/subscribe-to-in-app-message.js +1 -1
  77. package/src/InAppMessage/ui/automatically-show-in-app-messages.js +3 -3
  78. package/src/InAppMessage/ui/remove-all-visible-in-app-messages.js +3 -3
  79. package/src/InAppMessage/ui/show-in-app-message.js +27 -24
  80. package/src/Push/is-push-blocked.js +2 -2
  81. package/src/Push/is-push-permission-granted.js +2 -2
  82. package/src/Push/is-push-supported.js +2 -2
  83. package/src/Push/push-manager-factory.js +15 -15
  84. package/src/Push/push-manager.js +13 -13
  85. package/src/Push/register-push.js +2 -2
  86. package/src/Push/request-push-permission.js +2 -2
  87. package/src/Push/unregister-push-token-on-server.js +3 -3
  88. package/src/Push/unregister-push.js +3 -3
  89. package/src/Push/utils/push-utils.js +15 -15
  90. package/src/User/user-manager.js +9 -9
  91. package/src/User/user.js +50 -50
  92. package/src/common/constants.js +1 -1
  93. package/src/common/content-cards-display.js +22 -22
  94. package/src/common/event-logger.js +3 -3
  95. package/src/index.js +2 -9
  96. package/src/l10n/l10n-manager-factory.js +4 -4
  97. package/src/l10n/l10n-manager.js +6 -6
  98. package/src/managers/auth-manager.js +28 -28
  99. package/src/managers/braze-action-manager.js +14 -0
  100. package/src/managers/braze-instance.js +159 -151
  101. package/src/managers/device-manager.js +26 -26
  102. package/src/managers/network-manager.js +120 -120
  103. package/src/managers/server-config-manager.js +95 -86
  104. package/src/managers/session-manager.js +25 -25
  105. package/src/managers/storage-manager-factory.js +12 -12
  106. package/src/managers/storage-manager.js +129 -124
  107. package/src/managers/subscription-manager.js +8 -8
  108. package/src/models/backend-errors.js +5 -5
  109. package/src/models/braze-event.js +5 -5
  110. package/src/models/device.js +2 -2
  111. package/src/models/identifier.js +8 -8
  112. package/src/models/push-token.js +6 -6
  113. package/src/models/request-result.js +2 -2
  114. package/src/models/server-config.js +27 -27
  115. package/src/request-controller.js +117 -117
  116. package/src/standard-index.js +9 -0
  117. package/src/triggers/models/custom-event-data.js +4 -4
  118. package/src/triggers/models/custom-event-property-data.js +5 -5
  119. package/src/triggers/models/filter-set.js +5 -5
  120. package/src/triggers/models/filter.js +31 -31
  121. package/src/triggers/models/in-app-message-click-data.js +9 -9
  122. package/src/triggers/models/purchase-data.js +1 -1
  123. package/src/triggers/models/purchase-property-data.js +5 -5
  124. package/src/triggers/models/push-click-data.js +4 -4
  125. package/src/triggers/models/trigger-condition.js +35 -35
  126. package/src/triggers/models/trigger-events.js +3 -3
  127. package/src/triggers/models/trigger.js +36 -36
  128. package/src/triggers/triggers-provider-factory.js +5 -5
  129. package/src/triggers/triggers-provider.js +128 -128
  130. package/src/types.js +2 -2
  131. package/src/ui/js/attach-css.js +1 -1
  132. package/src/ui/js/chat-widget-css.js +11 -0
  133. package/src/ui/js/load-font-awesome.js +1 -1
  134. package/src/util/base-device-parser.js +7 -7
  135. package/src/util/braze-actions.js +5 -5
  136. package/src/util/browser-detector.js +41 -41
  137. package/src/util/client-hints-parser.js +10 -10
  138. package/src/util/component-utils.js +1 -1
  139. package/src/util/date-utils.js +2 -2
  140. package/src/util/device-constants.js +5 -5
  141. package/src/util/dom-utils.js +8 -8
  142. package/src/util/ecommerce-event-validation.js +13 -13
  143. package/src/util/html-display-utils.js +6 -6
  144. package/src/util/key-codes.js +1 -1
  145. package/src/util/net.js +82 -44
  146. package/src/util/request-header-utils.js +26 -24
  147. package/src/util/user-agent-parser.js +21 -21
  148. package/src/util/validation-utils.js +29 -21
  149. package/src/util/window-utils.js +3 -3
@@ -3,11 +3,11 @@ const b = {
3
3
  (void 0 === n && void 0 !== b.zg) || (b.zg = !!n), b.i || (b.i = !0);
4
4
  },
5
5
  destroy: function () {
6
- (b.i = !1), (b.zg = void 0), (b.Bd = void 0);
6
+ (b.i = !1), (b.zg = void 0), (b.df = void 0);
7
7
  },
8
8
  setLogger: function (n) {
9
9
  "function" == typeof n
10
- ? (b.init(), (b.Bd = n))
10
+ ? (b.init(), (b.df = n))
11
11
  : b.info("Ignoring setLogger call since logger is not a function");
12
12
  },
13
13
  toggleLogging: function () {
@@ -19,19 +19,19 @@ const b = {
19
19
  info: function (n) {
20
20
  if (b.zg) {
21
21
  const o = "Braze: " + n;
22
- null != b.Bd ? b.Bd(o) : console.log(o);
22
+ null != b.df ? b.df(o) : console.log(o);
23
23
  }
24
24
  },
25
25
  warn: function (n) {
26
26
  if (b.zg) {
27
- const o = "Braze SDK Warning: " + n + " (v6.12.0)";
28
- null != b.Bd ? b.Bd(o) : console.warn(o);
27
+ const o = "Braze SDK Warning: " + n + " (v6.13.0)";
28
+ null != b.df ? b.df(o) : console.warn(o);
29
29
  }
30
30
  },
31
31
  error: function (n) {
32
32
  if (b.zg) {
33
- const o = "Braze SDK Error: " + n + " (v6.12.0)";
34
- null != b.Bd ? b.Bd(o) : console.error(o);
33
+ const o = "Braze SDK Error: " + n + " (v6.13.0)";
34
+ null != b.df ? b.df(o) : console.error(o);
35
35
  }
36
36
  },
37
37
  };
@@ -1,29 +1,30 @@
1
1
  export default {
2
- _h: "allowCrawlerActivity",
3
- Nh: "baseUrl",
4
- se: "cookieExpiryInDays",
5
- Oh: "noCookies",
6
- Th: "devicePropertyAllowlist",
7
- xa: "disablePushTokenMaintenance",
8
- Rh: "enableLogging",
9
- Ph: "enableSdkAuthentication",
10
- wa: "manageServiceWorkerExternally",
11
- Dh: "minimumIntervalBetweenTriggerActionsInSeconds",
12
- Lh: "sessionTimeoutInSeconds",
13
- yh: "appVersion",
14
- Mh: "appVersionNumber",
15
- za: "serviceWorkerLocation",
16
- ha: "safariWebsitePushId",
17
- Ma: "localization",
2
+ hl: "allowCrawlerActivity",
3
+ cl: "baseUrl",
4
+ xe: "cookieExpiryInDays",
5
+ ml: "noCookies",
6
+ fl: "devicePropertyAllowlist",
7
+ Oa: "disablePushTokenMaintenance",
8
+ El: "enableLogging",
9
+ Il: "enableSdkAuthentication",
10
+ Na: "manageServiceWorkerExternally",
11
+ _l: "minimumIntervalBetweenTriggerActionsInSeconds",
12
+ gl: "sessionTimeoutInSeconds",
13
+ Sl: "appVersion",
14
+ Al: "appVersionNumber",
15
+ Ha: "serviceWorkerLocation",
16
+ La: "safariWebsitePushId",
17
+ al: "localization",
18
18
  sr: "contentSecurityNonce",
19
19
  nr: "allowUserSuppliedJavascript",
20
- ca: "inAppMessageZIndex",
21
- da: "openInAppMessagesInNewTab",
22
- lh: "requireExplicitInAppMessageDismissal",
23
- Uh: "doNotLoadFontAwesome",
24
- Wh: "deviceId",
25
- ba: "serviceWorkerScope",
26
- Oi: "dustHost",
27
- Bh: "sdkFlavor",
20
+ Ee: "inAppMessageZIndex",
21
+ Te: "conversationalChatZIndex",
22
+ Ja: "openInAppMessagesInNewTab",
23
+ Zh: "requireExplicitInAppMessageDismissal",
24
+ vl: "doNotLoadFontAwesome",
25
+ Nl: "deviceId",
26
+ Ka: "serviceWorkerScope",
27
+ _e: "dustHost",
28
+ Ol: "sdkFlavor",
28
29
  tn: "openCardsInNewTab",
29
30
  };
@@ -0,0 +1,5 @@
1
+ import r from "../managers/braze-instance.js";
2
+ import aa from "./conversational-chat-provider-factory.js";
3
+ export function automaticallyManageChat() {
4
+ r.rr() && aa.o().automaticallyManageChat();
5
+ }
@@ -0,0 +1,25 @@
1
+ import ChatMessage from "./chat-message.js";
2
+ export function newChatMessageFromJson(e) {
3
+ return new ChatMessage(
4
+ "assistant",
5
+ e.session_id,
6
+ e.message.id,
7
+ e.actions || [],
8
+ e.message,
9
+ e.event_type,
10
+ );
11
+ }
12
+ export function newUserChatMessage(e, n, t) {
13
+ return new ChatMessage("user", e, n, [], void 0, void 0, t);
14
+ }
15
+ export function newChatActionEvent(e) {
16
+ const n = e.type;
17
+ return "end_conversation" === n || "error" === n
18
+ ? { type: n, data: e.data }
19
+ : "user_action" === n && null != e.name
20
+ ? { type: n, name: e.name, data: e.data }
21
+ : null;
22
+ }
23
+ export function newChatMessageFromSerializedValue(e) {
24
+ return new ChatMessage(e.r, e.sid, e.id, e.a || [], e.m, e.et, e.um);
25
+ }
@@ -0,0 +1,33 @@
1
+ export default class ChatMessage {
2
+ constructor(t, s, i, h = [], r, e, o) {
3
+ (this.role = t),
4
+ (this.sessionId = s),
5
+ (this.messageId = i),
6
+ (this.actions = h),
7
+ (this.message = r),
8
+ (this.eventType = e),
9
+ (this.userMessage = o),
10
+ (this.role = t),
11
+ (this.sessionId = s),
12
+ (this.messageId = i),
13
+ (this.actions = h),
14
+ (this.message = r),
15
+ (this.eventType = e),
16
+ (this.userMessage = o);
17
+ }
18
+ Gi(t) {
19
+ for (const s of this.actions) if (s && s.type === t) return !0;
20
+ return !1;
21
+ }
22
+ qt() {
23
+ return {
24
+ r: this.role,
25
+ sid: this.sessionId,
26
+ id: this.messageId,
27
+ m: this.message,
28
+ et: this.eventType,
29
+ um: this.userMessage,
30
+ a: this.actions,
31
+ };
32
+ }
33
+ }
@@ -0,0 +1 @@
1
+ export const CHAT_BRAZE_ACTION_TYPE = "triggerChatAction";
@@ -0,0 +1,26 @@
1
+ import r from "../managers/braze-instance.js";
2
+ import tr from "./conversational-chat-provider.js";
3
+ import { registerChatActionHandler as er } from "./register-chat-action-handler.js";
4
+ const aa = {
5
+ i: !1,
6
+ provider: null,
7
+ o: () => (
8
+ aa.t(),
9
+ aa.provider ||
10
+ ((aa.provider = new tr(r.m(), r.l(), r.p())), r.v(aa.provider)),
11
+ aa.provider
12
+ ),
13
+ t: () => {
14
+ aa.i || (r.g(aa), (aa.i = !0));
15
+ },
16
+ destroy: () => {
17
+ (aa.provider = null), (aa.i = !1);
18
+ },
19
+ },
20
+ nr = r.initialize.bind(r);
21
+ (r.initialize = function (r, t) {
22
+ const e = nr(r, t);
23
+ return e && er(), e;
24
+ }),
25
+ r.vr() && er();
26
+ export default aa;
@@ -0,0 +1,372 @@
1
+ import t from "../common/base-provider.js";
2
+ import r from "../managers/braze-instance.js";
3
+ import nt from "../DUST/dust-provider-factory.js";
4
+ import f from "../managers/subscription-manager.js";
5
+ import { STORAGE_KEYS as s } from "../managers/storage-manager.js";
6
+ import l from "../util/net.js";
7
+ import h from "../util/request-header-utils.js";
8
+ import { logger as b, Guid as V } from "../../shared-lib/index.js";
9
+ import {
10
+ newChatActionEvent as rt,
11
+ newChatMessageFromJson as ot,
12
+ newChatMessageFromSerializedValue as ht,
13
+ newUserChatMessage as at,
14
+ } from "./chat-message-factory.js";
15
+ import ut from "./display/chat-widget-manager.js";
16
+ export default class tr extends t {
17
+ constructor(t, s, i) {
18
+ super(),
19
+ (this.B = t),
20
+ (this.h = s),
21
+ (this.j = i),
22
+ (this.B = t),
23
+ (this.h = s),
24
+ (this.j = i),
25
+ (this._i = !1),
26
+ (this.Oi = !1),
27
+ (this.Hi = new ut()),
28
+ (this.Wi = null),
29
+ (this.Ki = null),
30
+ (this.Qi = new f()),
31
+ (this.Vi = new f()),
32
+ (this.Yi = new f()),
33
+ r.S(this.Qi),
34
+ r.S(this.Vi),
35
+ r.S(this.Yi);
36
+ }
37
+ isEnabled() {
38
+ return (
39
+ !(!this.h || !this.h.Zi()) ||
40
+ (b.info("Conversational chat is not enabled for this user."), !1)
41
+ );
42
+ }
43
+ sn() {
44
+ return null != this.en();
45
+ }
46
+ en() {
47
+ const t = r.u();
48
+ return null != t ? t.$t() : null;
49
+ }
50
+ on() {
51
+ var t;
52
+ const i = this.en();
53
+ null != i && (null === (t = this.j) || void 0 === t || t.Pt(s.It.hn, i));
54
+ }
55
+ an() {
56
+ var t;
57
+ const i = this.en();
58
+ if (null == i) return !1;
59
+ return (
60
+ (null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.hn)) !== i
61
+ );
62
+ }
63
+ automaticallyManageChat() {
64
+ var t;
65
+ const s = this.h;
66
+ if (this.Oi || !s) return;
67
+ this.Oi = !0;
68
+ const i = () => {
69
+ this.sn() && this.un();
70
+ };
71
+ null === (t = r.nn()) ||
72
+ void 0 === t ||
73
+ t.rn(() => {
74
+ null != this.Wi && this.Wi !== this.en() && this.Hi.destroy(), i();
75
+ }),
76
+ s.ln(() => {
77
+ s.Zi() ? i() : this.Hi.destroy();
78
+ }),
79
+ i();
80
+ }
81
+ un() {
82
+ if (!this.h || !this.h.Zi()) return;
83
+ const t = this.h.dn();
84
+ t ? ((this.Wi = this.en()), this.Hi.mount(t)) : this.Hi.destroy();
85
+ }
86
+ cn() {
87
+ var t;
88
+ let i = this.vn();
89
+ return (
90
+ null == i &&
91
+ ((i = V.de()),
92
+ null === (t = this.j) || void 0 === t || t.Pt(s.It.fn, i),
93
+ this.on()),
94
+ i
95
+ );
96
+ }
97
+ vn() {
98
+ var t;
99
+ return (
100
+ (null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.fn)) || null
101
+ );
102
+ }
103
+ gn() {
104
+ return this.pn().map(ht);
105
+ }
106
+ pn() {
107
+ var t;
108
+ const i = this.vn();
109
+ if (null == i) return [];
110
+ const n = null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.mn);
111
+ return (null == n ? void 0 : n[i]) || [];
112
+ }
113
+ Cn() {
114
+ return this.pn().length > 0;
115
+ }
116
+ yn() {
117
+ var t;
118
+ return (
119
+ "in_progress" ===
120
+ (null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.wn))
121
+ );
122
+ }
123
+ Sn() {
124
+ var t;
125
+ return (
126
+ "ended" ===
127
+ (null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.wn))
128
+ );
129
+ }
130
+ isChatTurnErrored() {
131
+ var t;
132
+ return (
133
+ !0 === (null === (t = this.j) || void 0 === t ? void 0 : t.St(s.It.bn))
134
+ );
135
+ }
136
+ jn() {
137
+ var t;
138
+ this.Mn(),
139
+ null === (t = this.j) || void 0 === t || t.Pt(s.It.wn, "in_progress"),
140
+ this.Vi.A();
141
+ }
142
+ Tn() {
143
+ var t;
144
+ this.yn() && (null === (t = this.j) || void 0 === t || t.Vt(s.It.wn));
145
+ }
146
+ zn() {
147
+ var t;
148
+ null === (t = this.j) || void 0 === t || t.Pt(s.It.bn, !0), this.Vi.A();
149
+ }
150
+ Mn() {
151
+ var t;
152
+ null === (t = this.j) || void 0 === t || t.Vt(s.It.bn);
153
+ }
154
+ En(t, s) {
155
+ const i = this.B;
156
+ if (!this.isEnabled() || !i) return void ("function" == typeof s && s());
157
+ if ((this.Rn(), this.Sn()))
158
+ return void b.info(
159
+ "Conversational chat: conversation has ended; call resetChatConversation to start a new one.",
160
+ );
161
+ if (this.Cn())
162
+ return void b.info(
163
+ "Conversational chat: conversation already has history; skipping retrieveChatConfig.",
164
+ );
165
+ if (this.yn())
166
+ return void b.info(
167
+ "Conversational chat: a turn is already in progress; ignoring retrieveChatConfig.",
168
+ );
169
+ if (!this.sn())
170
+ return (
171
+ b.info(
172
+ "Conversational chat: no open session yet; skipping opening message request until a session opens.",
173
+ ),
174
+ void ("function" == typeof s && s())
175
+ );
176
+ const n = i.Z({}, !0);
177
+ (n.conversation_session_id = this.cn()), (n.dust_individual = nt.o().qn());
178
+ const e = h.it.Bn,
179
+ r = i.tt(n, e);
180
+ this.jn();
181
+ const o = () => {
182
+ this.Tn(), this.zn(), "function" == typeof s && s();
183
+ };
184
+ i.et(
185
+ n,
186
+ (s = -1) => {
187
+ -1 !== s && r.push(["X-Braze-Req-Tokens-Remaining", s.toString()]),
188
+ l.ot({
189
+ url: `${i.ht()}/${e}`,
190
+ data: n,
191
+ headers: r,
192
+ lt: (s) => {
193
+ i.ut(n, s, r) ? (i.ct(), "function" == typeof t && t()) : o();
194
+ },
195
+ error: (t) => {
196
+ i.dt(t, "requesting conversational chat opening message"), o();
197
+ },
198
+ });
199
+ },
200
+ e,
201
+ o,
202
+ );
203
+ }
204
+ In(t, s, i, n) {
205
+ const e = this.B;
206
+ if (!this.isEnabled() || !e) return "function" == typeof n && n(), !1;
207
+ if ((this.Rn(), this.Sn()))
208
+ return (
209
+ b.info(
210
+ "Conversational chat: conversation has ended; call resetChatConversation to start a new one.",
211
+ ),
212
+ !1
213
+ );
214
+ if (this.yn())
215
+ return (
216
+ b.info(
217
+ "Conversational chat: a turn is already in progress; ignoring sendChatMessage.",
218
+ ),
219
+ !1
220
+ );
221
+ if (!this.sn())
222
+ return (
223
+ b.info(
224
+ "Conversational chat: no open session yet; ignoring sendChatMessage until a session opens.",
225
+ ),
226
+ "function" == typeof n && n(),
227
+ !1
228
+ );
229
+ const r = this.cn(),
230
+ o = e.Z({}, !0);
231
+ (o.conversation_session_id = r),
232
+ (o.user_message = t),
233
+ (o.dust_individual = nt.o().qn()),
234
+ null != s &&
235
+ null != s.completedActions &&
236
+ (o.completed_actions = s.completedActions);
237
+ const a = h.it.$n,
238
+ u = e.tt(o, a);
239
+ this.jn();
240
+ let d = !1;
241
+ const c = () => {
242
+ (d = !0), this.Tn(), this.zn(), "function" == typeof n && n();
243
+ };
244
+ return (
245
+ e.et(
246
+ o,
247
+ (s = -1) => {
248
+ -1 !== s && u.push(["X-Braze-Req-Tokens-Remaining", s.toString()]),
249
+ l.ot({
250
+ url: `${e.ht()}/${a}`,
251
+ data: o,
252
+ headers: u,
253
+ lt: (s) => {
254
+ if (!e.ut(o, s, u)) return void c();
255
+ e.ct();
256
+ const n = s.message_id;
257
+ this.kn(at(r, n, t)),
258
+ this.Vi.A(),
259
+ "function" == typeof i && i();
260
+ },
261
+ error: (t) => {
262
+ e.dt(t, "sending conversational chat message"), c();
263
+ },
264
+ });
265
+ },
266
+ a,
267
+ c,
268
+ ),
269
+ !d
270
+ );
271
+ }
272
+ xn(t) {
273
+ return this.Rn(), this.Qi.Ut(t);
274
+ }
275
+ Dn(t) {
276
+ return this.Rn(), this.Vi.Ut(t);
277
+ }
278
+ subscribeToChatAction(t) {
279
+ return this.Rn(), this.Yi.Ut(t);
280
+ }
281
+ Un(t) {
282
+ this.Yi.A(t);
283
+ }
284
+ Rn() {
285
+ var t;
286
+ this._i ||
287
+ (r.ar("ctr", (t) => {
288
+ this._n(t);
289
+ }),
290
+ null === (t = r.nn()) ||
291
+ void 0 === t ||
292
+ t.rn(() => {
293
+ this.an() && this.An(), this.on();
294
+ }),
295
+ this.Gn(),
296
+ (this._i = !0));
297
+ }
298
+ Gn() {
299
+ "undefined" != typeof window &&
300
+ window.addEventListener &&
301
+ !this.Ki &&
302
+ ((this.Ki = (t) => this.Nn(t)),
303
+ window.addEventListener("storage", this.Ki),
304
+ r.On(() => {
305
+ this.Ki &&
306
+ (window.removeEventListener("storage", this.Ki), (this.Ki = null));
307
+ }));
308
+ }
309
+ Nn(t) {
310
+ const i = t.key;
311
+ (null == i ||
312
+ 0 === i.indexOf(s.It.mn) ||
313
+ 0 === i.indexOf(s.It.wn) ||
314
+ 0 === i.indexOf(s.It.bn)) &&
315
+ this.Vi.A();
316
+ }
317
+ _n(t) {
318
+ var i;
319
+ if ("ctr" !== t.type || null == t.body) return;
320
+ const n = ot(t.body);
321
+ if (n.sessionId === this.vn() && !this.Xn(n.messageId)) {
322
+ this.Tn(),
323
+ n.Gi("end_conversation")
324
+ ? (this.kn(n),
325
+ this.Mn(),
326
+ null === (i = this.j) || void 0 === i || i.Pt(s.It.wn, "ended"),
327
+ this.Vi.A())
328
+ : n.Gi("error")
329
+ ? this.zn()
330
+ : (this.kn(n), this.Mn(), this.Vi.A()),
331
+ this.Qi.A(n);
332
+ for (const t of n.actions) {
333
+ const s = rt(t);
334
+ null != s
335
+ ? this.Un(s)
336
+ : b.info(`Ignoring unrecognized chat action: ${t.type}`);
337
+ }
338
+ }
339
+ }
340
+ Xn(t) {
341
+ if (null == t) return !1;
342
+ for (const s of this.pn()) if (s.id === t) return !0;
343
+ return !1;
344
+ }
345
+ kn(t) {
346
+ if (!this.j) return;
347
+ const i = t.sessionId,
348
+ n = this.j.St(s.It.mn) || {},
349
+ e = n[i] || [];
350
+ for (e.push(t.qt()); e.length > 1 && JSON.stringify(e).length > 5e4; )
351
+ e.shift();
352
+ (n[i] = e), this.j.Pt(s.It.mn, n);
353
+ }
354
+ changeUser() {
355
+ this.Hn();
356
+ }
357
+ clearData(t = !1) {
358
+ t && this.Hn(), this.Hi.destroy();
359
+ }
360
+ Hn() {
361
+ this.An();
362
+ }
363
+ An() {
364
+ if (!this.j) return;
365
+ const t = this.vn();
366
+ if ((this.j.Vt(s.It.fn), null != t)) {
367
+ const i = this.j.St(s.It.mn) || {};
368
+ delete i[t], this.j.Pt(s.It.mn, i);
369
+ }
370
+ this.j.Vt(s.It.wn), this.j.Vt(s.It.bn), this.j.Vt(s.It.hn), this.Vi.A();
371
+ }
372
+ }
@@ -0,0 +1,113 @@
1
+ import r, { OPTIONS as U } from "../../managers/braze-instance.js";
2
+ import { removeSubscription } from "../../Core/remove-subscription.js";
3
+ import b from "../../../shared-lib/logger.js";
4
+ import l from "../../util/net.js";
5
+ import { setupChatWidgetUI as lt } from "../../ui/js/chat-widget-css.js";
6
+ import {
7
+ CHAT_WIDGET_IFRAME_ID as dt,
8
+ chatWidgetToHtml as ct,
9
+ } from "./chat-widget-to-html.js";
10
+ export default class ut {
11
+ constructor() {
12
+ (this.se = 0),
13
+ (this.he = 0),
14
+ (this.ue = null),
15
+ (this.le = []),
16
+ (this.ce = null),
17
+ (this.me = null),
18
+ (this.fe = 0),
19
+ (this.pe = null);
20
+ }
21
+ mount(t) {
22
+ if ("undefined" == typeof document) return;
23
+ if (this.pe === t) return;
24
+ if ((null != this.pe && this.destroy(), document.getElementById(dt)))
25
+ return;
26
+ if (!r.er(U.nr))
27
+ return void b.error(
28
+ "Conversational chat is disabled. Use the 'allowUserSuppliedJavascript' option for braze.initialize to enable this feature.",
29
+ );
30
+ lt(), (this.pe = t);
31
+ const i = ++this.fe;
32
+ l.get({
33
+ url: t,
34
+ lt: (t) => {
35
+ if (i !== this.fe) return;
36
+ const s = r.er(U.sr);
37
+ this.we(t, s, i);
38
+ },
39
+ error: (t) => {
40
+ i === this.fe &&
41
+ (b.error(`Conversational chat: failed to fetch widget (${t})`),
42
+ (this.pe = null));
43
+ },
44
+ });
45
+ }
46
+ destroy() {
47
+ if ((this.fe++, this.ve(), this.be(), "undefined" != typeof document)) {
48
+ this.ce &&
49
+ (document.removeEventListener("DOMContentLoaded", this.ce),
50
+ (this.ce = null));
51
+ const t = document.getElementById(dt) || this.me;
52
+ t && ((t.onload = null), t.parentNode && t.parentNode.removeChild(t)),
53
+ (this.me = null);
54
+ }
55
+ this.pe = null;
56
+ }
57
+ ge(t, i) {
58
+ this.ve();
59
+ const s = ct(
60
+ t,
61
+ i,
62
+ (t) => this.je(t),
63
+ (t, i, s) => this.ye(t, i, s),
64
+ );
65
+ return (
66
+ (this.me = s),
67
+ (this.ue = () => this.Ce(s)),
68
+ window.addEventListener("resize", this.ue),
69
+ s
70
+ );
71
+ }
72
+ ve() {
73
+ this.ue &&
74
+ (window.removeEventListener("resize", this.ue), (this.ue = null)),
75
+ (this.se = 0),
76
+ (this.he = 0);
77
+ }
78
+ we(t, i, s) {
79
+ const e = () => {
80
+ if (
81
+ (this.ce === e && (this.ce = null),
82
+ s === this.fe && null != this.pe && !document.getElementById(dt))
83
+ )
84
+ return document.body
85
+ ? void document.body.appendChild(this.ge(t, i))
86
+ : (b.error(
87
+ "Conversational chat: document body is unavailable; not mounting the widget.",
88
+ ),
89
+ void (this.pe = null));
90
+ };
91
+ document.body
92
+ ? e()
93
+ : ((this.ce = e), document.addEventListener("DOMContentLoaded", e));
94
+ }
95
+ je(t) {
96
+ return t && this.le.push(t), t;
97
+ }
98
+ be() {
99
+ for (const t of this.le) removeSubscription(t);
100
+ this.le = [];
101
+ }
102
+ Ce(t) {
103
+ const i = window.innerWidth || this.se,
104
+ s = window.innerHeight || this.he;
105
+ (t.style.width = `${Math.min(this.se, i)}px`),
106
+ (t.style.height = `${Math.min(this.he, s)}px`);
107
+ }
108
+ ye(t, i, s) {
109
+ isNaN(i) || isNaN(s) || !isFinite(i) || !isFinite(s) || i < 0 || s < 0
110
+ ? b.warn(`Conversational chat: invalid widget bounds ${i}x${s}`)
111
+ : ((this.se = i), (this.he = s), this.Ce(t));
112
+ }
113
+ }