@telia-ace/widget-conversation 1.0.29-next.4 → 1.0.29-next.6

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