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