@telia-ace/widget-conversation 0.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.
Files changed (47) hide show
  1. package/agent.d.ts +22 -0
  2. package/components/conversation/conversation.d.ts +7 -0
  3. package/components/conversation/plugin.d.ts +48 -0
  4. package/components/conversation/ui/content.d.ts +3 -0
  5. package/components/conversation/ui/input.d.ts +7 -0
  6. package/components/conversation/ui/language-context.d.ts +3 -0
  7. package/components/conversation/ui/loader.d.ts +10 -0
  8. package/components/conversation/ui/message-list-items/button-list.d.ts +9 -0
  9. package/components/conversation/ui/message-list-items/custom.d.ts +10 -0
  10. package/components/conversation/ui/message-list-items/html.d.ts +6 -0
  11. package/components/conversation/ui/message-list-items/item-list.d.ts +9 -0
  12. package/components/conversation/ui/message-list-items/link-list.d.ts +9 -0
  13. package/components/conversation/ui/message-list-items/separator.d.ts +3 -0
  14. package/components/conversation/ui/message-list-items/text.d.ts +7 -0
  15. package/components/conversation/ui/message-list-items/video-request.d.ts +9 -0
  16. package/components/conversation/ui/message-types/agent.d.ts +12 -0
  17. package/components/conversation/ui/message-types/index.d.ts +4 -0
  18. package/components/conversation/ui/message-types/message-list.d.ts +10 -0
  19. package/components/conversation/ui/message-types/system.d.ts +16 -0
  20. package/components/conversation/ui/message-types/timestamp.d.ts +10 -0
  21. package/components/conversation/ui/message-types/user.d.ts +9 -0
  22. package/components/conversation/ui/message.d.ts +6 -0
  23. package/components/conversation/ui/overlay-outlets.d.ts +14 -0
  24. package/components/conversation/ui/skip-link.d.ts +4 -0
  25. package/components/conversation/ui/styles.d.ts +62 -0
  26. package/components/return-button/plugin.d.ts +19 -0
  27. package/components/return-button/return-button.d.ts +7 -0
  28. package/conversation-3787f6ba.mjs +1619 -0
  29. package/conversation-92e4cb62.js +623 -0
  30. package/conversation-controller.d.ts +122 -0
  31. package/conversation-history.d.ts +27 -0
  32. package/conversation-platform.d.ts +55 -0
  33. package/conversation-provider.d.ts +34 -0
  34. package/index.d.ts +13 -0
  35. package/index.js +3 -0
  36. package/index.mjs +1086 -0
  37. package/package.json +27 -0
  38. package/return-button-3e542ab7.mjs +154 -0
  39. package/return-button-f1f43984.js +97 -0
  40. package/store.d.ts +33 -0
  41. package/types.d.ts +130 -0
  42. package/user.d.ts +10 -0
  43. package/utils/custom-message.d.ts +4 -0
  44. package/utils/index.d.ts +1 -0
  45. package/utils/map-actions.d.ts +1 -0
  46. package/utils/open-chat-widget.d.ts +5 -0
  47. package/utils/swap-provider.d.ts +9 -0
package/index.mjs ADDED
@@ -0,0 +1,1086 @@
1
+ var B = Object.defineProperty;
2
+ var L = (o, e, t) => e in o ? B(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
3
+ var d = (o, e, t) => (L(o, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { ComponentPlatform as R } from "@telia-ace/widget-core";
5
+ import { createStorageWriter as E, StorageCategory as I, readStorage as f, StorageMedium as y, StorageScope as z } from "@telia-ace/widget-services";
6
+ import { uuid as $, lock as K } from "@telia-ace/widget-utilities";
7
+ import { FormBuilder as Q } from "@telia-ace/widget-forms";
8
+ import { closeAllModals as D } from "@telia-ace/widget-plugins";
9
+ import { createReactComponent as H } from "@telia-ace/widget-ui";
10
+ import { lazy as U } from "react";
11
+ class q {
12
+ constructor(e) {
13
+ d(this, "cache", /* @__PURE__ */ new Map());
14
+ d(this, "subscriptions", []);
15
+ this.controller = e;
16
+ const { events: t } = e.container.get("$widget"), s = t.subscribe("data-client:fetched", (r, n) => {
17
+ n.type === "contact-method" && this.cache.set(n.response.id, n.response);
18
+ });
19
+ this.subscriptions.push(s);
20
+ }
21
+ getConversationLogs() {
22
+ return this.controller.messages.reduce(
23
+ (t, s) => {
24
+ const r = C(s), { key: n, content: a, type: i, sender: c, timestamp: h } = r.content, u = this.parseEntry(a, i, c, h);
25
+ return u && t.push(u), t;
26
+ },
27
+ []
28
+ );
29
+ }
30
+ parseEntry(e, t, s, r) {
31
+ const n = (h) => {
32
+ let u = [], l = [], v;
33
+ if (Array.isArray(h)) {
34
+ const [p, m] = h;
35
+ v = m;
36
+ } else
37
+ v = h;
38
+ return this.createMessageContent(v).length && (l = [
39
+ ...l,
40
+ this.createMessageContent(v)
41
+ ]), u = [...u, ...this.createMessageOptions(v)], {
42
+ messagesAsList: l,
43
+ options: u
44
+ };
45
+ };
46
+ let a = [], i = [];
47
+ if (Array.isArray(e.items)) {
48
+ const { items: h } = e;
49
+ h.forEach((u) => {
50
+ const l = n(u);
51
+ a = [...a, ...l.options], i = [...i, ...l.messagesAsList];
52
+ });
53
+ } else {
54
+ const h = n(e);
55
+ a = [...a, ...h.options], i = [...i, ...h.messagesAsList];
56
+ }
57
+ return {
58
+ options: a,
59
+ message: i.length ? i.join(`
60
+ `) : void 0,
61
+ source: this.createMessageSource(t),
62
+ alias: this.createAlias(t, s),
63
+ timestamp: r
64
+ };
65
+ }
66
+ createMessageSource(e) {
67
+ return e;
68
+ }
69
+ createAlias(e, t) {
70
+ return e === w.System ? "system" : (t == null ? void 0 : t.name) || "Me";
71
+ }
72
+ handleContactMethod(e) {
73
+ if (!this.cache.has(e))
74
+ return;
75
+ const t = this.cache.get(e);
76
+ let s = "";
77
+ return s = this.appendText(s, `${t.clientName}:`), s = this.appendText(s, t.title), t.description && (s = this.appendText(s, t.title)), s;
78
+ }
79
+ getDetails(e, t = !0) {
80
+ if (!e)
81
+ return "";
82
+ const {
83
+ header: s,
84
+ title: r,
85
+ label: n,
86
+ description: a,
87
+ text: i,
88
+ html: c,
89
+ body: h,
90
+ contactMethodId: u
91
+ } = e;
92
+ let l = "";
93
+ if (u) {
94
+ const v = this.handleContactMethod(u.toString());
95
+ v && (l = this.appendText(l, v, t));
96
+ }
97
+ return s && (l = this.appendText(l, s, t)), r && (l = this.appendText(l, r, t)), a && (l = this.appendText(l, a, t)), i && (l = this.appendText(l, i, t)), n && (l = this.appendText(l, n, t)), c && (l = this.appendText(l, c, t)), h && typeof h == "string" && (l = this.appendText(l, h, t)), l;
98
+ }
99
+ createMessageContent(e) {
100
+ let t = "";
101
+ return t = this.getDetails(e), t;
102
+ }
103
+ createMessageOptions(e) {
104
+ let t = [], s = [];
105
+ return Array.isArray(e == null ? void 0 : e.actions) ? s = e.actions : typeof (e == null ? void 0 : e.actions) == "object" && (s = Object.entries(e.actions).map(([r, n]) => ({
106
+ actionKey: r,
107
+ label: n
108
+ }))), s.length && (t = s.map((r) => this.getDetails(r, !1))), t;
109
+ }
110
+ appendText(e, t, s = !0) {
111
+ return s && e.length ? `${e}
112
+ ${t}` : e.length ? `${e} ${t}` : t;
113
+ }
114
+ dispose() {
115
+ this.subscriptions.forEach((e) => {
116
+ e();
117
+ });
118
+ }
119
+ }
120
+ class k {
121
+ constructor(e, t) {
122
+ d(this, "id");
123
+ d(this, "name");
124
+ d(this, "avatar");
125
+ d(this, "print", (e, t, s = null) => {
126
+ var i, c, h;
127
+ const r = {
128
+ name: this.name,
129
+ avatar: this.avatar,
130
+ id: this.id
131
+ }, n = C(e, t);
132
+ s = s ?? n.timestamp;
133
+ const a = (c = (i = this.provider) == null ? void 0 : i.controller) == null ? void 0 : c.container.get(
134
+ "$widget"
135
+ );
136
+ return (h = a == null ? void 0 : a.events) == null || h.dispatch("tracking:conversation-message", {
137
+ sender: "agent"
138
+ }), this.provider.createEntry(
139
+ n.key,
140
+ n.content,
141
+ w.Agent,
142
+ r,
143
+ s
144
+ );
145
+ });
146
+ d(this, "typing", () => this.provider.setTypingState({
147
+ name: this.name,
148
+ avatar: this.avatar
149
+ }));
150
+ this.provider = e, this.id = t.id || $(), this.name = t.name, this.avatar = t.avatar;
151
+ }
152
+ update(e) {
153
+ var r;
154
+ const { name: t, avatar: s } = e;
155
+ s === null ? this.avatar = void 0 : s && (this.avatar = s), t === null ? this.name = void 0 : t && (this.name = t), (r = this.provider.controller) == null || r.events.dispatch("conversation:agent-updated", {
156
+ agent: this
157
+ });
158
+ }
159
+ }
160
+ class T {
161
+ constructor(e, t, s) {
162
+ d(this, "subscriptions", []);
163
+ d(this, "options");
164
+ d(this, "user");
165
+ this.name = e, this.container = t, this.controller = s, this.user = s == null ? void 0 : s.user, this.options = {
166
+ silent: !1
167
+ };
168
+ }
169
+ static create(e, t, s) {
170
+ return new T(e, t, s);
171
+ }
172
+ createAgent(e = {}) {
173
+ var t;
174
+ if (this.controller) {
175
+ const s = this.controller.getAgentById(e.id), r = s ? new k(this, { ...s, ...e }) : new k(this, e);
176
+ return s || (t = this.controller) == null || t.events.dispatch("conversation:agent-created", {
177
+ agent: r
178
+ }), r;
179
+ }
180
+ }
181
+ print(e) {
182
+ var t;
183
+ return (t = this.controller) == null ? void 0 : t.print(e);
184
+ }
185
+ setTypingState(e, t) {
186
+ var s;
187
+ return (s = this.controller) == null ? void 0 : s.setTypingState(e, t);
188
+ }
189
+ loading() {
190
+ return this.controller ? this.controller.loader() : () => () => {
191
+ };
192
+ }
193
+ createEntry(e, t, s, r, n) {
194
+ var a;
195
+ return (a = this.controller) == null ? void 0 : a.createEntry(e, t, s, r, n);
196
+ }
197
+ dispose() {
198
+ var e;
199
+ process.env.NODE_ENV !== "production" && console.log(`conversation-provider: dispose (${this.name})`), (e = this.controller) == null || e.disposeProvider(this), this.controller = null, this.subscriptions.forEach((t) => t());
200
+ }
201
+ isRehydrated() {
202
+ var e;
203
+ return ((e = this.controller) == null ? void 0 : e.isRehydrated()) || Promise.resolve(!1);
204
+ }
205
+ getHistory() {
206
+ var e;
207
+ return ((e = this.controller) == null ? void 0 : e.getHistory()) || [];
208
+ }
209
+ complete() {
210
+ var e;
211
+ (e = this.controller) == null || e.onCompleteProvider();
212
+ }
213
+ setOptions(e = { silent: !1 }) {
214
+ this.options = e;
215
+ }
216
+ onUnreadMessage(e) {
217
+ var t;
218
+ if (this.controller) {
219
+ const s = (t = this.controller) == null ? void 0 : t.onUnreadMessage(e);
220
+ return this.subscriptions.push(s), () => {
221
+ s();
222
+ };
223
+ }
224
+ }
225
+ onMounted(e) {
226
+ var t;
227
+ if (this.controller) {
228
+ const s = (t = this.controller) == null ? void 0 : t.events.subscribe(
229
+ "conversation:component-mount-changed",
230
+ e
231
+ );
232
+ return this.subscriptions.push(s), e({}, this.controller.getComponentMountedState()), () => {
233
+ s();
234
+ };
235
+ }
236
+ }
237
+ }
238
+ const _ = (o) => `conversation-${o}`;
239
+ class j {
240
+ constructor() {
241
+ d(this, "_locking", Promise.resolve());
242
+ d(this, "_locks", 0);
243
+ }
244
+ isLocked() {
245
+ return this._locks > 0;
246
+ }
247
+ lock() {
248
+ this._locks += 1;
249
+ let e;
250
+ const t = new Promise(
251
+ (r) => e = () => {
252
+ this._locks -= 1, r();
253
+ }
254
+ ), s = this._locking.then(() => e);
255
+ return this._locking = this._locking.then(() => t), s;
256
+ }
257
+ }
258
+ function F(o) {
259
+ let e = 0;
260
+ if (o.length === 0)
261
+ return e.toString();
262
+ for (let t = 0; t < o.length; t++) {
263
+ const s = o.charCodeAt(t);
264
+ e = (e << 5) - e + s, e = e & e;
265
+ }
266
+ return e.toString();
267
+ }
268
+ class J {
269
+ constructor(e, t, s) {
270
+ d(this, "sessionStorageKey");
271
+ d(this, "locker", new j());
272
+ d(this, "initialStoredAgents", []);
273
+ this.container = e, this.conversation = t, this.enabled = s, this.sessionStorageKey = _(t.id), t.onStarted(async () => {
274
+ if (t.messages.length > 0)
275
+ return;
276
+ const { agents: r } = await this.read(), n = [];
277
+ r && r.forEach((i) => {
278
+ const c = t.providerIndex(i.providerName), h = c > -1 ? t.registeredProviders[c] : t.createProvider(i.providerName), u = new k(h, {
279
+ name: i.name,
280
+ avatar: i.avatar,
281
+ id: i.id
282
+ });
283
+ n.push(u);
284
+ });
285
+ const { messages: a } = await this.read();
286
+ a.filter((i) => (i == null ? void 0 : i.content) != null).filter(
287
+ (i) => !i.chat || !t.providers.includes("ace.chat")
288
+ ).forEach((i) => {
289
+ switch (i.type) {
290
+ case "agent": {
291
+ const c = n.find((h) => {
292
+ var u;
293
+ return h.id === ((u = i.sender) == null ? void 0 : u.id);
294
+ });
295
+ c && c.print(
296
+ i.content.items || i.content,
297
+ {
298
+ key: i.key
299
+ },
300
+ i.timestamp
301
+ );
302
+ return;
303
+ }
304
+ case "user": {
305
+ t.user.print(
306
+ i.content.items || i.content,
307
+ {
308
+ key: i.key
309
+ },
310
+ i.timestamp
311
+ );
312
+ return;
313
+ }
314
+ case "system":
315
+ default:
316
+ t.print(
317
+ i.content.items || i.content,
318
+ i.timestamp
319
+ );
320
+ }
321
+ });
322
+ }), t.onMessageCreated(async (r, n) => {
323
+ this.write(n.message, "messages");
324
+ }), t.events.subscribe(
325
+ "conversation:agent-created",
326
+ async (r, n) => {
327
+ const a = {
328
+ id: n.agent.id,
329
+ name: n.agent.name,
330
+ avatar: n.agent.avatar,
331
+ providerName: n.agent.provider.name
332
+ };
333
+ await this.write(a, "agents");
334
+ }
335
+ ), t.events.subscribe(
336
+ "conversation:agent-updated",
337
+ async (r, n) => {
338
+ const { agents: a } = await this.read();
339
+ await this.write(
340
+ [
341
+ ...a.map((i) => i.id === n.agent.id ? {
342
+ ...i,
343
+ name: n.agent.name,
344
+ avatar: n.agent.avatar
345
+ } : i)
346
+ ],
347
+ "agents",
348
+ "update"
349
+ );
350
+ }
351
+ );
352
+ }
353
+ setInitialData(e) {
354
+ this.initialStoredAgents = e.agents;
355
+ }
356
+ async isRehydrated() {
357
+ const { messages: e } = await this.read();
358
+ return e.length > 0;
359
+ }
360
+ getStoredAgent(e) {
361
+ if (this.enabled)
362
+ return this.initialStoredAgents.find((t) => t.id === e);
363
+ }
364
+ async write(e, t, s = "add") {
365
+ if (!this.enabled)
366
+ return;
367
+ const r = await this.locker.lock();
368
+ return E(
369
+ this.container,
370
+ this.sessionStorageKey,
371
+ I.Necessary
372
+ ).then(async (n) => {
373
+ if (s === "add") {
374
+ const a = await this.read();
375
+ if (t === "messages" && (e.chat = this.conversation.providers.includes("ace.chat"), e.hash = F(JSON.stringify(e.content) + e.timestamp)), t === "messages" && a.messages.findIndex(
376
+ (c) => c.key === e.key || c.hash === e.hash
377
+ ) > -1) {
378
+ r();
379
+ return;
380
+ }
381
+ const i = {
382
+ ...a
383
+ };
384
+ return t === "messages" ? i.messages = [...i.messages, e] : t === "agents" && (i.agents = [...i.agents, e]), n({
385
+ ...a,
386
+ ...i
387
+ }).then(() => {
388
+ r();
389
+ });
390
+ }
391
+ if (s === "update" && t === "agents") {
392
+ const a = await this.read();
393
+ return n({
394
+ ...a,
395
+ agents: e
396
+ }).then(() => {
397
+ r();
398
+ });
399
+ }
400
+ return n(e).then(() => {
401
+ r();
402
+ });
403
+ });
404
+ }
405
+ async read() {
406
+ return f(this.container, this.sessionStorageKey).then((e) => e || {
407
+ messages: [],
408
+ agents: []
409
+ });
410
+ }
411
+ }
412
+ class G {
413
+ constructor(e) {
414
+ d(this, "print", (e, t, s = null) => {
415
+ var a, i;
416
+ const r = C(e, t);
417
+ return s = s ?? r.timestamp, (i = ((a = this.controller) == null ? void 0 : a.container.get("$widget")).events) == null || i.dispatch("tracking:conversation-message", {
418
+ sender: "user"
419
+ }), this.controller.createEntry(
420
+ r.key,
421
+ r.content,
422
+ w.User,
423
+ void 0,
424
+ s
425
+ );
426
+ });
427
+ this.controller = e;
428
+ }
429
+ }
430
+ const x = async (o, e, t) => {
431
+ const s = await o.getAsync(
432
+ "conversation"
433
+ ), r = await o.getAsync("components"), n = s.controllers.find((a) => a.conversation.id === e);
434
+ if (n) {
435
+ const a = [...n.conversation.providers];
436
+ a.forEach((c) => {
437
+ n.conversation.setProviderOptions(c, { silent: !0 });
438
+ }), n.conversation.queueProviders(a), n.conversation.setProviders([t]);
439
+ const i = r.components().ofType("conversation");
440
+ r.write({
441
+ key: "setConversationProvider",
442
+ attributes: {
443
+ properties: { providers: [t] }
444
+ },
445
+ target: i,
446
+ provider: "localStorage"
447
+ });
448
+ }
449
+ };
450
+ class V {
451
+ constructor(e) {
452
+ this.writer = e;
453
+ }
454
+ update(e) {
455
+ const t = this.getNormalizedSettings(e);
456
+ this.writer(t);
457
+ }
458
+ remove() {
459
+ this.writer(null);
460
+ }
461
+ getNormalizedSettings(e) {
462
+ return typeof e == "string" ? { body: e } : e.length > -1 && typeof e == "object" ? { items: e } : e;
463
+ }
464
+ }
465
+ var w = /* @__PURE__ */ ((o) => (o.User = "user", o.Agent = "agent", o.System = "system", o))(w || {});
466
+ const C = (o, e) => {
467
+ let t, s = (e == null ? void 0 : e.key) || $(), r = Date.now();
468
+ if (Array.isArray(o) && o.length > -1)
469
+ typeof o[0] == "string" ? t = { items: [o] } : t = { items: o };
470
+ else if (typeof o == "string")
471
+ t = { body: o };
472
+ else if (o = o || {}, o.type)
473
+ t = {
474
+ ...o
475
+ };
476
+ else {
477
+ if (t = {
478
+ body: o.body,
479
+ actions: o.actions,
480
+ title: o.title
481
+ }, o.key && (s = o.key), o.timestamp && (r = o.timestamp), typeof o.form == "function") {
482
+ const n = Q.create(s);
483
+ o.form(n);
484
+ const a = n.get();
485
+ t.form = a;
486
+ }
487
+ o.timestamp === null && (r = null);
488
+ }
489
+ return { content: t, key: s, timestamp: r };
490
+ }, M = (o, e) => {
491
+ if (!e)
492
+ return;
493
+ const t = JSON.stringify(e), s = o.findIndex((r) => t === JSON.stringify(r));
494
+ if (s > -1)
495
+ return o.splice(s, 1);
496
+ };
497
+ class Y {
498
+ constructor(e, t, s) {
499
+ d(this, "formHandlers", {});
500
+ /**
501
+ * Represents the current end user.
502
+ */
503
+ d(this, "user", new G(this));
504
+ d(this, "events");
505
+ d(this, "providers", []);
506
+ d(this, "active", !1);
507
+ d(this, "queuedProviders", []);
508
+ d(this, "registeredProviders", []);
509
+ d(this, "loaders", 0);
510
+ d(this, "componentMounted", !1);
511
+ d(this, "store");
512
+ d(this, "messages", []);
513
+ d(this, "typingActors", []);
514
+ d(this, "conversationHistory");
515
+ d(this, "print", (e, t = null) => {
516
+ const s = C(e);
517
+ t = t ?? s.timestamp;
518
+ const r = this.container.get("$widget");
519
+ return r == null || r.events.dispatch("tracking:conversation-message", {
520
+ sender: "system"
521
+ }), this.createEntry(
522
+ s.key,
523
+ s.content,
524
+ "system",
525
+ void 0,
526
+ t
527
+ );
528
+ });
529
+ this.container = e, this.id = t;
530
+ const r = e.get("$widget");
531
+ this.events = r.events.createChild(this), this.store = new J(e, this, s.rehydrate), this.conversationHistory = new q(this);
532
+ }
533
+ setInitialStorage(e) {
534
+ this.store.setInitialData(e);
535
+ }
536
+ setProviders(e) {
537
+ const t = [], s = [];
538
+ this.providers.reduce((r, n) => (e.indexOf(n) === -1 && r.push(n), r), []).forEach((r) => {
539
+ const n = this.providers.indexOf(r);
540
+ n > -1 && (t.push(r), this.providers.splice(n, 1));
541
+ }), e.forEach((r) => {
542
+ this.providers.indexOf(r) === -1 && (s.push(r), this.providers.push(r));
543
+ }), (s.length || t.length) && this.events.dispatch("conversation:providers-changed", {
544
+ added: s,
545
+ removed: t
546
+ });
547
+ }
548
+ createProvider(e) {
549
+ const t = this.providerIndex(e);
550
+ if (t > -1)
551
+ return this.registeredProviders[t];
552
+ const s = T.create(e, this.container, this);
553
+ return this.registeredProviders.push(s), s;
554
+ }
555
+ providerIndex(e) {
556
+ return this.registeredProviders.findIndex((t) => t.name === e);
557
+ }
558
+ getAgentById(e) {
559
+ if (e)
560
+ return this.store.getStoredAgent(e);
561
+ }
562
+ dispose() {
563
+ this.conversationHistory.dispose(), this.registeredProviders.forEach((e) => e.dispose()), this.messages = [], this.events.dispatch("conversation:controller-disposed", this), this.active = !1;
564
+ }
565
+ onDispose(e) {
566
+ return this.events.subscribe("conversation:controller-disposed", e);
567
+ }
568
+ onStarted(e) {
569
+ return this.events.subscribe("conversation:started", e);
570
+ }
571
+ onMessageCreated(e) {
572
+ return this.events.subscribe("conversation:message-created", e);
573
+ }
574
+ isRehydrated() {
575
+ return this.store.isRehydrated();
576
+ }
577
+ disposeProvider(e) {
578
+ const t = this.providerIndex(e.name);
579
+ t > -1 && this.registeredProviders.splice(t, 1), this.events.dispatch("conversation:providers-changed", {
580
+ removed: [e.name]
581
+ }), this.registeredProviders.length || this.dispose();
582
+ }
583
+ onProvidersChange(e) {
584
+ return this.events.subscribe("conversation:providers-changed", e);
585
+ }
586
+ setComponentMountedState(e) {
587
+ this.componentMounted !== e && (this.componentMounted = e, e && (this.events.dispatch("conversation:unread-message", !1), this.events.dispatch("conversation:started", !0), this.active = !0), this.events.dispatch("conversation:component-mount-changed", e));
588
+ }
589
+ setProviderOptions(e, t) {
590
+ const s = this.providerIndex(e);
591
+ s > -1 && this.registeredProviders[s].setOptions(t);
592
+ }
593
+ changeProvider(e) {
594
+ this.registeredProviders.find(
595
+ (s) => s.name === e
596
+ ) && x(this.container, this.id, e);
597
+ }
598
+ queueProviders(e) {
599
+ this.queuedProviders = e;
600
+ }
601
+ onCompleteProvider() {
602
+ const e = this.queuedProviders.pop();
603
+ e && x(this.container, this.id, e);
604
+ }
605
+ getComponentMountedState() {
606
+ return this.componentMounted;
607
+ }
608
+ onUnreadMessage(e) {
609
+ return this.events.subscribe("conversation:unread-message", e);
610
+ }
611
+ createEntry(e, t, s, r, n) {
612
+ const a = {
613
+ key: e,
614
+ content: t,
615
+ type: s,
616
+ sender: r || null,
617
+ timestamp: n || n === null ? n : Date.now()
618
+ };
619
+ this.messages.push(a), a.type === "agent" && M(this.typingActors, r);
620
+ const i = new V((c) => {
621
+ if (c !== null) {
622
+ const { timestamp: h = void 0, ...u } = c;
623
+ a.content = u, h && (a.timestamp = h);
624
+ } else {
625
+ const h = this.messages.indexOf(a);
626
+ a.content = null, h > -1 && this.messages.splice(h, 1);
627
+ }
628
+ this.events.dispatch("conversation:message-updated", { message: a }), this.update();
629
+ });
630
+ return this.events.dispatch("conversation:message-created", { message: a }), this.update(), this.componentMounted || this.events.dispatch("conversation:unread-message", !0), i;
631
+ }
632
+ update() {
633
+ this.events.dispatch("conversation:updated", {
634
+ messages: this.messages,
635
+ loading: this.loaders > 0,
636
+ typingActors: this.typingActors
637
+ });
638
+ }
639
+ getMessageStream() {
640
+ return {
641
+ messages: this.messages,
642
+ loading: this.loaders > 0,
643
+ typingActors: this.typingActors
644
+ };
645
+ }
646
+ setTypingState(e, t) {
647
+ return this.typingActors.indexOf(e) === -1 && (this.typingActors.push(e), this.update()), () => {
648
+ M(this.typingActors, e), this.update();
649
+ };
650
+ }
651
+ /**
652
+ * Displays a loader in the conversation.
653
+ */
654
+ loader() {
655
+ this.loaders = this.loaders + 1;
656
+ let e = !1;
657
+ return this.loaders === 1 && this.update(), () => {
658
+ e || (e = !0, this.loaders = this.loaders - 1, this.loaders === 0 && this.update());
659
+ };
660
+ }
661
+ submitForm(e, t) {
662
+ this.formHandlers[e] = { ...this.formHandlers[e], submit: t };
663
+ }
664
+ validateForm(e, t) {
665
+ this.formHandlers[e] = { ...this.formHandlers[e], validate: t };
666
+ }
667
+ getHistory() {
668
+ return this.conversationHistory.getConversationLogs();
669
+ }
670
+ }
671
+ const X = (o, e) => {
672
+ const t = o.split("_contact");
673
+ return e.widgets.get(t[0]);
674
+ }, N = (o) => {
675
+ const e = o.get("$widget"), { name: t, implementation: s } = o.get("$widget"), r = t.indexOf("_contact") > -1 ? X(t, s) : e, { data: n } = o.get("$settings"), { trigger: a } = r.container.get("$settings"), i = `${t.replace("_contact-method", "").replace("_contact", "")}_contact-method`, c = s.widgets.get(i);
676
+ return c && !a ? { chatWidget: c, mainWidget: r, data: n } : { mainWidget: r, data: n };
677
+ }, Z = (o) => {
678
+ const { chatWidget: e } = N(o);
679
+ return !!e;
680
+ }, O = (o, e, t) => {
681
+ const { mainWidget: s, data: r, chatWidget: n } = N(o);
682
+ return n ? n.state === "activated" ? n.invoke("open").then(() => {
683
+ n.container.getAsync("conversation").then((a) => {
684
+ a.navigateToConversationIfActive(e.id).then(() => D(o));
685
+ });
686
+ }) : n.activate().then(() => {
687
+ const { trigger: a } = n.container.get("$settings");
688
+ return a.action = "hide", n.invoke("setSettings", () => ({
689
+ data: r,
690
+ trigger: a,
691
+ activate: !0
692
+ })).then(() => n.container.getAsync("components").then((i) => {
693
+ const c = i.components().ofType("contact-method");
694
+ i.write({
695
+ key: "setContactMethodId",
696
+ attributes: {
697
+ properties: {
698
+ id: e.id,
699
+ guideId: e.guideId
700
+ }
701
+ },
702
+ target: c,
703
+ provider: "localStorage"
704
+ });
705
+ const h = i.components().ofType("conversation");
706
+ i.write({
707
+ key: "setConversationProvider",
708
+ attributes: {
709
+ properties: { providers: [e.clientName] }
710
+ },
711
+ target: h,
712
+ provider: "localStorage"
713
+ });
714
+ }));
715
+ }).then(() => n.invoke("attach", {
716
+ key: "contact-method",
717
+ triggerDOMElement: t,
718
+ withRenderState: "open"
719
+ })).then(() => (E(
720
+ n.container,
721
+ "activeStickyWidget",
722
+ I.Necessary,
723
+ {
724
+ medium: y.Local,
725
+ scope: z.Implementation
726
+ }
727
+ ).then((a) => a(n.name)), n.container.getAsync("conversation").then((a) => {
728
+ a.setConversationEndedBehavior(
729
+ S.Deactivate
730
+ );
731
+ }).then(() => D(o)).then(() => !1))) : (s.container.getAsync("conversation").then((a) => a.navigateToConversationIfActive(e.id).then(
732
+ () => a.setConversationEndedBehavior(
733
+ S.Navigate
734
+ )
735
+ )), !n);
736
+ }, ee = (o, e, t = "conversation") => {
737
+ const { mainWidget: s, chatWidget: r } = N(o);
738
+ return r ? r.container.getAsync("router").then((n) => {
739
+ n.navigate(t, { id: e.id });
740
+ }) : s.container.getAsync("conversation").then((n) => {
741
+ var a;
742
+ (a = n.setConversationEndedBehavior(S.Navigate)) == null || a.then(() => {
743
+ s.container.getAsync("router").then((i) => {
744
+ s.container.getAsync("components").then((c) => {
745
+ const h = c.components().ofType("conversation");
746
+ c.write({
747
+ key: "setConversationProvider",
748
+ attributes: {
749
+ properties: { providers: [e.clientName] }
750
+ },
751
+ target: h,
752
+ provider: "localStorage"
753
+ });
754
+ }).then(() => {
755
+ i.navigate(t, { id: e.id });
756
+ });
757
+ });
758
+ });
759
+ });
760
+ }, b = "conversations";
761
+ var S = /* @__PURE__ */ ((o) => (o.Deactivate = "deactivate", o.Navigate = "navigate", o.None = "none", o))(S || {});
762
+ class A {
763
+ constructor(e, t) {
764
+ d(this, "events");
765
+ d(this, "controllers", []);
766
+ d(this, "providers", []);
767
+ d(this, "disposeProviders", {});
768
+ d(this, "subscriptions", []);
769
+ d(this, "storageWriter", null);
770
+ d(this, "router", null);
771
+ d(this, "buttonQuery", null);
772
+ d(this, "sessionInfo", null);
773
+ this.container = e, this.components = t;
774
+ const s = e.get("$widget");
775
+ this.events = s.events.createChild(this);
776
+ }
777
+ initialize() {
778
+ E(
779
+ this.container,
780
+ b,
781
+ I.Necessary,
782
+ {
783
+ medium: y.Local
784
+ }
785
+ ).then((e) => this.storageWriter = e), f(
786
+ this.container,
787
+ b,
788
+ y.Local
789
+ ).then((e) => {
790
+ this.buttonQuery = this.components.components().ofType("conversation-return-button"), e && (this.sessionInfo = e, e.id && e.route && this.writeToComponents({ active: !0, alert: { show: !1 } }));
791
+ }), this.container.getAsync("router").then((e) => this.router = e), this.events.subscribe("conversation:ended", (e, t) => {
792
+ var s, r, n;
793
+ switch ((s = this.sessionInfo) == null ? void 0 : s.conversationEndedBehavior) {
794
+ case "deactivate":
795
+ this.container.get("$widget").deactivate();
796
+ break;
797
+ case "navigate":
798
+ const i = (r = this.router) == null ? void 0 : r.getInitialRoute();
799
+ i && ((n = this.router) == null || n.navigate(i.name));
800
+ break;
801
+ }
802
+ this.writeToComponents({ active: !1 }), this.sessionInfo = null, this.storageWriter && this.storageWriter();
803
+ });
804
+ }
805
+ addSubscription(e) {
806
+ this.subscriptions.push(e);
807
+ }
808
+ static getInstance(e, t = "conversation") {
809
+ return K(e)(() => e.getAsync(t).then((s) => {
810
+ let r = s;
811
+ return r || R.getInstance(e).then((n) => (r = new A(e, n), r.initialize(), e.registerAsync(t, () => r), r));
812
+ }));
813
+ }
814
+ navigateToConversationIfActive(e) {
815
+ return f(
816
+ this.container,
817
+ b,
818
+ y.Local
819
+ ).then((t) => {
820
+ var s;
821
+ if (t && t.id === e && t.route) {
822
+ const { name: r, params: n } = t.route;
823
+ (s = this.router) == null || s.navigate(r, n);
824
+ }
825
+ });
826
+ }
827
+ setConversationEndedBehavior(e) {
828
+ return f(
829
+ this.container,
830
+ b,
831
+ y.Local
832
+ ).then((t) => {
833
+ let s = t;
834
+ return s ? s.conversationEndedBehavior = e : s = { conversationEndedBehavior: e }, this.sessionInfo = s, this.storageWriter && this.storageWriter(this.sessionInfo) || Promise.resolve();
835
+ });
836
+ }
837
+ async createController(e, t) {
838
+ const s = _(e), { agents: r } = await f(this.container, s) || { agents: [] }, n = this.controllers.findIndex((i) => i.conversation.id === e);
839
+ let a;
840
+ return n > -1 ? a = this.controllers[n].conversation : a = new Y(this.container, e, t), a.setInitialStorage({ agents: r || [] }), a;
841
+ }
842
+ registerProvider(e, t) {
843
+ const s = {
844
+ name: e,
845
+ handler: t
846
+ };
847
+ this.providers.push(s), this.controllers.filter(
848
+ (r) => r.conversation.providers.indexOf(e) > -1
849
+ ).forEach((r) => {
850
+ this.invokeHandler(s, r);
851
+ });
852
+ }
853
+ registerController(e, t) {
854
+ const s = this.controllers.findIndex(
855
+ (n) => n.conversation.id === e.id
856
+ );
857
+ let r;
858
+ if (s < 0) {
859
+ r = { conversation: e, component: t }, this.controllers.push(r);
860
+ const n = (a) => {
861
+ a && this.providers.filter((i) => a.indexOf(i.name) > -1).forEach((i) => {
862
+ this.invokeHandler(i, r);
863
+ });
864
+ };
865
+ return this.subscriptions.push(
866
+ e.onDispose((a, i) => {
867
+ const c = this.controllers.findIndex(
868
+ (h) => h.conversation === i
869
+ );
870
+ c > -1 && this.controllers.splice(c, 1), this.components.components().ofType("conversation-return-button").select().writeProperties({ active: !1 }), this.events.dispatch("conversation:ended", {}, { bubbles: !0 });
871
+ })
872
+ ), this.subscriptions.push(
873
+ e.onProvidersChange((a, i) => {
874
+ const {
875
+ added: c,
876
+ removed: h = []
877
+ } = i;
878
+ h.forEach((u) => {
879
+ (this.disposeProviders[u] || []).forEach(
880
+ (l) => {
881
+ l();
882
+ }
883
+ );
884
+ }), n(c);
885
+ })
886
+ ), this.subscriptions.push(
887
+ e.onStarted((a, i) => {
888
+ var h, u;
889
+ const c = {
890
+ id: e.id,
891
+ route: (h = this.router) == null ? void 0 : h.getRouteData(),
892
+ conversationEndedBehavior: (u = this.sessionInfo) == null ? void 0 : u.conversationEndedBehavior
893
+ };
894
+ this.sessionInfo = c, this.storageWriter && this.storageWriter(c), this.writeToComponents({ active: i });
895
+ })
896
+ ), this.subscriptions.push(
897
+ e.onUnreadMessage((a, i) => {
898
+ this.events.dispatch("conversation:unread-message", i), this.writeToComponents({ alert: { show: i } });
899
+ })
900
+ ), n(e.providers), !0;
901
+ }
902
+ return r = this.controllers[s], !1;
903
+ }
904
+ writeToComponents(e, t = "conversationReturnButtonState") {
905
+ this.buttonQuery && this.components.write({
906
+ key: "conversationReturnButtonState",
907
+ attributes: { properties: e },
908
+ target: this.buttonQuery,
909
+ provider: "localStorage"
910
+ });
911
+ }
912
+ invokeHandler(e, t) {
913
+ const s = e.handler(
914
+ t.conversation.createProvider(e.name),
915
+ t.component
916
+ );
917
+ s && (this.disposeProviders[e.name] || (this.disposeProviders[e.name] = []), this.disposeProviders[e.name].push(s));
918
+ }
919
+ getComponentRoute() {
920
+ return f(
921
+ this.container,
922
+ b,
923
+ y.Local
924
+ ).then((e) => e == null ? void 0 : e.route);
925
+ }
926
+ dispose() {
927
+ Object.keys(this.disposeProviders).forEach((e) => {
928
+ this.disposeProviders[e].forEach((t) => {
929
+ t();
930
+ });
931
+ }), this.subscriptions.forEach((e) => {
932
+ e();
933
+ }), this.controllers = [], this.providers = [];
934
+ }
935
+ static async handleContactMethodClick(e, t, s, r) {
936
+ if (Z(t)) {
937
+ e.preventDefault();
938
+ let n = document.getElementById("humany-dynamic-anchor");
939
+ n || (n = document.createElement("a"), n.setAttribute("href", "#"), n.id = "humany-dynamic-anchor", document.body.appendChild(n)), O(t, s, n);
940
+ }
941
+ O(t, s);
942
+ }
943
+ static async handleContactMethodSubmit(e, t) {
944
+ var s, r;
945
+ return (r = (s = t == null ? void 0 : t.body.form.meta) == null ? void 0 : s.conversation) != null && r.id ? x(
946
+ e,
947
+ t.body.form.meta.conversation.id,
948
+ t.clientName
949
+ ) : ee(e, t);
950
+ }
951
+ }
952
+ const te = (o, e) => o.reduce((t, s, r) => {
953
+ var a, i;
954
+ const n = {
955
+ ...s,
956
+ content: [{ ...s.content, key: s.key }]
957
+ };
958
+ if (t.length > 0 && s.type !== w.System) {
959
+ const c = t[t.length - 1];
960
+ if ((!e && e !== 0 || Math.abs(c.timestamp - s.timestamp) < e) && ((a = c.sender) == null ? void 0 : a.name) === ((i = s.sender) == null ? void 0 : i.name) && c.type === s.type)
961
+ return c.timestamp = s.timestamp, c.content.push({ ...s.content, key: s.key }), t;
962
+ }
963
+ return t.push(n), t;
964
+ }, []), le = (o) => Promise.all([
965
+ A.getInstance(o),
966
+ o.getAsync("router")
967
+ ]).then(([e, t]) => H(
968
+ o,
969
+ "conversation",
970
+ import("./conversation-3787f6ba.mjs"),
971
+ async (s) => {
972
+ const { rehydrate: r = !1 } = s.properties(), a = t.getRouteData().params.id || s.node.name || "component", i = await e.createController(a, {
973
+ rehydrate: r
974
+ }), c = e.registerController(
975
+ i,
976
+ s
977
+ ), h = 3 * 60 * 1e3, u = (g) => {
978
+ const { providers: p = [] } = g;
979
+ if (p) {
980
+ const m = Array.isArray(p) ? p : [p];
981
+ return i.setProviders(m), m;
982
+ }
983
+ };
984
+ u(
985
+ s.properties(u)
986
+ ), i.setComponentMountedState(!0);
987
+ const l = (g) => {
988
+ const { loading: p, typingActors: m, messages: P } = g;
989
+ s.writeProperties({
990
+ loading: p,
991
+ typingActors: m,
992
+ messages: te(P, h)
993
+ });
994
+ };
995
+ l(i.getMessageStream()), c && (s.writeProperties({
996
+ conversationControllerId: i.id
997
+ }), s.actions.create("user-submit", (g, p) => {
998
+ if (p) {
999
+ const { preventDefault: m, validationPromise: P } = p;
1000
+ if (m) {
1001
+ P && P().then((W) => {
1002
+ W && i.user.print(W);
1003
+ });
1004
+ return;
1005
+ }
1006
+ }
1007
+ i.user.print(g.text);
1008
+ }), s.actions.create("user-typing", (g, p) => {
1009
+ }), s.actions.create("form", (g, p) => {
1010
+ }), s.actions.create("action", (g, p) => {
1011
+ }), s.actions.create("overlay-mounted", (g, p) => {
1012
+ }), s.actions.create("show-overlay", (g, p) => {
1013
+ s.writeProperties({
1014
+ overlay: { conversation: !1, secondary: !1 }
1015
+ });
1016
+ }), s.actions.create("hide-overlay", (g, p) => {
1017
+ s.writeProperties({
1018
+ overlay: null
1019
+ });
1020
+ }), s.actions.create(
1021
+ "show-overlay-conversation",
1022
+ (g, p) => {
1023
+ s.writeProperties({
1024
+ overlay: { conversation: !0, secondary: !1 }
1025
+ });
1026
+ }
1027
+ ), s.actions.create(
1028
+ "hide-overlay-conversation",
1029
+ (g, p) => {
1030
+ s.writeProperties({
1031
+ overlay: { conversation: !1, secondary: !1 }
1032
+ });
1033
+ }
1034
+ ));
1035
+ const v = i.events.subscribe(
1036
+ "conversation:updated",
1037
+ (g, p) => {
1038
+ l(p);
1039
+ }
1040
+ );
1041
+ return () => {
1042
+ i.setComponentMountedState(!1), v(), i.active || l({ messages: [], typingActors: [], loading: !1 });
1043
+ };
1044
+ },
1045
+ { reuseController: !0 }
1046
+ )), ue = (o) => Promise.all([
1047
+ R.getInstance(o),
1048
+ A.getInstance(o),
1049
+ o.getAsync("router")
1050
+ ]).then(
1051
+ ([e, t, s]) => H(
1052
+ o,
1053
+ "conversation-return-button",
1054
+ import("./return-button-3e542ab7.mjs"),
1055
+ (r) => {
1056
+ r.actions.create("toggle", (n = {}) => {
1057
+ var a;
1058
+ (a = t.getComponentRoute()) == null || a.then((i) => {
1059
+ i && s.navigate(i.name, i.params);
1060
+ });
1061
+ });
1062
+ }
1063
+ )
1064
+ );
1065
+ var se = /* @__PURE__ */ ((o) => (o.LinkList = "link-list", o.Separator = "separator", o.ButtonList = "button-list", o.ItemList = "item-list", o.Text = "text", o.HTML = "html", o.VideoRequest = "video-request", o))(se || {});
1066
+ const pe = (o, e, t) => o.getAsync("componentResolver").then(async (s) => {
1067
+ (await A.getInstance(o)).addSubscription(
1068
+ s.registerComponent(
1069
+ e,
1070
+ U(() => t)
1071
+ )
1072
+ );
1073
+ });
1074
+ export {
1075
+ k as Agent,
1076
+ le as ConversationComponent,
1077
+ Y as ConversationController,
1078
+ S as ConversationEndedBehavior,
1079
+ V as ConversationEntry,
1080
+ se as ConversationMessageListItemType,
1081
+ w as ConversationMessageType,
1082
+ A as ConversationPlatform,
1083
+ T as ConversationProvider,
1084
+ ue as ConversationReturnButtonComponent,
1085
+ pe as registerCustomMessageComponent
1086
+ };