@telia-ace/conversational-hub-bot-provider 1.3.11 → 1.3.12

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.
@@ -6,7 +6,7 @@ import { useContainer as R, useDispatch as T, useConversation as _ } from "@teli
6
6
  import { FormBuilder as j } from "@telia-ace/widget-forms";
7
7
  import { appendClassNames as k } from "@telia-ace/widget-utilities";
8
8
  import { useState as G, useRef as I, useEffect as S, useCallback as V } from "react";
9
- import { u as $ } from "./index-69c78022.mjs";
9
+ import { u as $ } from "./index-06b06645.mjs";
10
10
  import "@telia-ace/widget-conversation";
11
11
  import "@telia-ace/widget-services";
12
12
  import "@webprovisions/platform";
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react/jsx-runtime"),A=require("@emotion/styled"),F=require("@telia-ace/knowledge-data-client"),B=require("@telia-ace/knowledge-widget-adapters"),p=require("@telia-ace/knowledge-widget-ui"),R=require("@telia-ace/widget-forms"),T=require("@telia-ace/widget-utilities"),u=require("react"),k=require("./index-eb780812.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@webprovisions/platform");const $=({contactMethodId:n,guideId:o,groupId:m,className:x})=>{var C,v,w;const[e,s]=u.useState(),d=p.useContainer(),D=p.useDispatch(),b=u.useRef(!0),l=p.useConversation();u.useEffect(()=>{const{events:c}=d.get("$widget"),q=c.subscribe("conversational-hub-provider:form-submitted",async(f,r)=>{var M;if(((M=r.form.meta)==null?void 0:M.groupId)!==m||r.id!==(e==null?void 0:e.id))return;l&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=l);const y=await d.getAsync("contactMethods"),t={...e,body:{...e.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(t);if(!h){const S=new R.FormBuilder(e.body.form);s(void 0),S.setValidationErrors(i),t.body={...t.body,form:S.get()},s({...t});return}const E=await y.submit(t);t.confirmation=E,s(t)}),a=c.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const t=await(await d.getAsync("contactMethods")).evaluate(e);let i={...e,body:{...e.body,form:t}};const h=k.updateForm(i,{submit:"Submit",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,e,s,l]);const g=u.useCallback(async c=>{const a=await(await F.DataClient.getInstance(d)).fetch(F.DataType.ContactMethod,{contactMethodId:c,guideId:o},{noCache:!0,isBot:!0}),f=k.updateForm(a,{submit:"Submit",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!b.current)return null;s(a)},[b]);return u.useEffect(()=>(g(n),()=>{b.current=!1}),[n,g]),e?j.jsx(z,{...e,body:{...e.body,form:e.body.form?{...e.body.form,data:(C=e.body.form)==null?void 0:C.data,layout:{...e.body.form.layout},meta:(v=e.body.form)==null?void 0:v.meta,schema:(w=e.body.form)==null?void 0:w.schema}:void 0,guideId:e.body.guideId},inline:!0,expanded:!0,dispatch:D,className:T.appendClassNames("humany-bot-contact-method",x)}):null},z=A(B.ContactMethod)`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react/jsx-runtime"),A=require("@emotion/styled"),F=require("@telia-ace/knowledge-data-client"),B=require("@telia-ace/knowledge-widget-adapters"),p=require("@telia-ace/knowledge-widget-ui"),R=require("@telia-ace/widget-forms"),T=require("@telia-ace/widget-utilities"),u=require("react"),k=require("./index-bc0e69f0.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@webprovisions/platform");const $=({contactMethodId:n,guideId:o,groupId:m,className:x})=>{var C,v,w;const[e,s]=u.useState(),d=p.useContainer(),D=p.useDispatch(),b=u.useRef(!0),l=p.useConversation();u.useEffect(()=>{const{events:c}=d.get("$widget"),q=c.subscribe("conversational-hub-provider:form-submitted",async(f,r)=>{var M;if(((M=r.form.meta)==null?void 0:M.groupId)!==m||r.id!==(e==null?void 0:e.id))return;l&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=l);const y=await d.getAsync("contactMethods"),t={...e,body:{...e.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(t);if(!h){const S=new R.FormBuilder(e.body.form);s(void 0),S.setValidationErrors(i),t.body={...t.body,form:S.get()},s({...t});return}const E=await y.submit(t);t.confirmation=E,s(t)}),a=c.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const t=await(await d.getAsync("contactMethods")).evaluate(e);let i={...e,body:{...e.body,form:t}};const h=k.updateForm(i,{submit:"Submit",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,e,s,l]);const g=u.useCallback(async c=>{const a=await(await F.DataClient.getInstance(d)).fetch(F.DataType.ContactMethod,{contactMethodId:c,guideId:o},{noCache:!0,isBot:!0}),f=k.updateForm(a,{submit:"Submit",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!b.current)return null;s(a)},[b]);return u.useEffect(()=>(g(n),()=>{b.current=!1}),[n,g]),e?j.jsx(z,{...e,body:{...e.body,form:e.body.form?{...e.body.form,data:(C=e.body.form)==null?void 0:C.data,layout:{...e.body.form.layout},meta:(v=e.body.form)==null?void 0:v.meta,schema:(w=e.body.form)==null?void 0:w.schema}:void 0,guideId:e.body.guideId},inline:!0,expanded:!0,dispatch:D,className:T.appendClassNames("humany-bot-contact-method",x)}):null},z=A(B.ContactMethod)`
2
2
  padding: ${n=>{var o;return(o=n.theme.sizes)==null?void 0:o.normal}} ${n=>{var o;return(o=n.theme.sizes)==null?void 0:o.medium}};
3
3
  `;exports.default=$;
@@ -3,11 +3,11 @@ import m from "@emotion/styled";
3
3
  import { ContactList as M, ContactMethod as b } from "@telia-ace/knowledge-widget-adapters";
4
4
  import { useDispatch as $, useContainer as k } from "@telia-ace/knowledge-widget-ui";
5
5
  import { useState as f, useEffect as E } from "react";
6
- import S from "./contact-method-93470880.mjs";
6
+ import S from "./contact-method-e0b87a2a.mjs";
7
7
  import "@telia-ace/knowledge-data-client";
8
8
  import "@telia-ace/widget-forms";
9
9
  import "@telia-ace/widget-utilities";
10
- import "./index-69c78022.mjs";
10
+ import "./index-06b06645.mjs";
11
11
  import "@telia-ace/widget-conversation";
12
12
  import "@telia-ace/widget-services";
13
13
  import "@webprovisions/platform";
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),u=require("@emotion/styled"),y=require("@telia-ace/knowledge-widget-adapters"),g=require("@telia-ace/knowledge-widget-ui"),l=require("react"),b=require("./contact-method-5c975b10.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-eb780812.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@webprovisions/platform");const k=({actions:e,entryId:t,groupId:x})=>{const q=g.useDispatch(),o=g.useContainer(),[h,C]=l.useState([]),[d,M]=l.useState([]);return l.useEffect(()=>{o.getAsync("contactMethods").then(n=>{if(!n)return;const c=e.map(async i=>{var m;if(!((m=i.data)!=null&&m.clientName))return null;const s=await n.handleAdapters(i.data);return{...s,...i.data,description:i.data.description?i.data.description:s.description,onClick:(p,r)=>{if(typeof s.onClick=="function"&&s.onClick(p,r),!p.defaultPrevented)if(r.inline&&!r.expanded)M([...d,r.id]);else{const{events:f}=o.get("$widget");f.dispatch("conversational-hub-provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey}),f.dispatch("tracking:contact-method-list-item-clicked",r)}}}});Promise.all(c).then(i=>{C(i.filter(s=>!!s))})})},[e,o,t,d]),h.length?a.jsx(j,{className:"humany-bot-contact-list",contactMethods:h,dispatch:q,itemRenderer:(n,c)=>(d.includes(n.id)||n.expanded)&&n.inline||n.clientName==="custom.link"?a.jsx(S,{contactMethodId:n.id,groupId:x,guideId:n.body.guideId}):a.jsx($,{...n,...c})}):null},j=u(y.ContactList)`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),u=require("@emotion/styled"),y=require("@telia-ace/knowledge-widget-adapters"),g=require("@telia-ace/knowledge-widget-ui"),l=require("react"),b=require("./contact-method-f1b2fed9.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-bc0e69f0.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@webprovisions/platform");const k=({actions:e,entryId:t,groupId:x})=>{const q=g.useDispatch(),o=g.useContainer(),[h,C]=l.useState([]),[d,M]=l.useState([]);return l.useEffect(()=>{o.getAsync("contactMethods").then(n=>{if(!n)return;const c=e.map(async i=>{var m;if(!((m=i.data)!=null&&m.clientName))return null;const s=await n.handleAdapters(i.data);return{...s,...i.data,description:i.data.description?i.data.description:s.description,onClick:(p,r)=>{if(typeof s.onClick=="function"&&s.onClick(p,r),!p.defaultPrevented)if(r.inline&&!r.expanded)M([...d,r.id]);else{const{events:f}=o.get("$widget");f.dispatch("conversational-hub-provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey}),f.dispatch("tracking:contact-method-list-item-clicked",r)}}}});Promise.all(c).then(i=>{C(i.filter(s=>!!s))})})},[e,o,t,d]),h.length?a.jsx(j,{className:"humany-bot-contact-list",contactMethods:h,dispatch:q,itemRenderer:(n,c)=>(d.includes(n.id)||n.expanded)&&n.inline||n.clientName==="custom.link"?a.jsx(S,{contactMethodId:n.id,groupId:x,guideId:n.body.guideId}):a.jsx($,{...n,...c})}):null},j=u(y.ContactList)`
2
2
  > ul > li {
3
3
  a {
4
4
  display: flex;
@@ -0,0 +1,648 @@
1
+ var tt = Object.defineProperty;
2
+ var et = (i, e, n) => e in i ? tt(i, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[e] = n;
3
+ var M = (i, e, n) => (et(i, typeof e != "symbol" ? e + "" : e, n), n);
4
+ import { ConversationMessageListItemType as A, registerCustomMessageComponent as q, ConversationPlatform as nt } from "@telia-ace/widget-conversation";
5
+ import { readStorage as W, createStorageWriter as Q, StorageCategory as X } from "@telia-ace/widget-services";
6
+ import { uuid as j, Logger as V } from "@telia-ace/widget-utilities";
7
+ import { FormBuilder as G, submitButton as it } from "@telia-ace/widget-forms";
8
+ import { createConfigurationApi as ot } from "@webprovisions/platform";
9
+ class st {
10
+ constructor(e) {
11
+ M(this, "enabled", !0);
12
+ // set whether or not to store data
13
+ M(this, "botProviderGlobalStateKey", "bot-provider-global-state");
14
+ M(this, "botProviderLocalStateKey", "bot-provider-local-state");
15
+ M(this, "botProviderEntriesKey", "bot-provider-entries");
16
+ M(this, "botProviderMetaStateKey", "bot-provider-conversation-meta");
17
+ this.container = e;
18
+ }
19
+ getKey(e) {
20
+ let n;
21
+ switch (e) {
22
+ case "localState":
23
+ n = this.botProviderLocalStateKey;
24
+ break;
25
+ case "globalState":
26
+ n = this.botProviderGlobalStateKey;
27
+ break;
28
+ case "entries":
29
+ n = this.botProviderEntriesKey;
30
+ break;
31
+ case "meta":
32
+ n = this.botProviderMetaStateKey;
33
+ break;
34
+ }
35
+ return n;
36
+ }
37
+ async read(e) {
38
+ const n = this.getKey(e);
39
+ if (!(!this.enabled || !n))
40
+ return W(this.container, n);
41
+ }
42
+ async write(e, n) {
43
+ const t = this.getKey(n);
44
+ if (!this.enabled || !t)
45
+ return;
46
+ (await Q(
47
+ this.container,
48
+ t,
49
+ X.Necessary
50
+ ))(e);
51
+ }
52
+ }
53
+ var K = /* @__PURE__ */ ((i) => (i.ContactMethod = "ace-conversational-hub-contact-method", i.ContactList = "ace-conversational-hub-contact-list", i))(K || {});
54
+ const at = (i) => {
55
+ if (i) {
56
+ const { type: e, content: n } = i;
57
+ if (e && n)
58
+ return { type: e, content: n };
59
+ }
60
+ }, rt = ({
61
+ isUser: i,
62
+ items: e = [],
63
+ meta: n = {}
64
+ }) => {
65
+ const t = j();
66
+ return {
67
+ id: t,
68
+ isUser: i,
69
+ items: e.map((p) => ({
70
+ id: p.id || j(),
71
+ isUser: i,
72
+ groupId: t,
73
+ message: p.message || []
74
+ })),
75
+ meta: n
76
+ };
77
+ }, ct = ({
78
+ id: i,
79
+ title: e,
80
+ defaultIcon: n,
81
+ exits: [t],
82
+ exitType: d,
83
+ confirmationText: p,
84
+ expandFormAutomatically: f,
85
+ form: k,
86
+ description: P
87
+ }, a = {}) => {
88
+ const { adapter: l } = t || { Adapter: {} }, {
89
+ clientName: u = "",
90
+ settings: b = {},
91
+ customClientName: m = ""
92
+ } = l, { validation: y } = a, g = {
93
+ id: i.toString(),
94
+ title: e,
95
+ description: P,
96
+ symbol: at(n),
97
+ clientName: u === "custom.adapter" ? m : u,
98
+ expanded: !0,
99
+ inline: !0,
100
+ body: {}
101
+ };
102
+ if (u.indexOf("phone") > -1 || u.indexOf("freetext") > -1) {
103
+ const { displayTextBeforeClick: h, textBeforeClick: x } = b;
104
+ h === "true" && (g.expanded = !1, g.title = x), u.indexOf("phone.text") > -1 ? g.body.phoneNumber = b.phoneNumber : g.body.freetext = b.freetext;
105
+ }
106
+ return (u.indexOf("chat.popup") > -1 || u.indexOf("link") > -1 || u === "custom.adapter" && d === "custom") && (g.body = b, g.body.confirmationFallback = p), u.indexOf("ace") > -1 && (g.body = b), k && k.hasValueComponents && (y ? g.body.form = G.fromLegacyForm(
107
+ i.toString(),
108
+ k,
109
+ y
110
+ ).get() : g.body.form = G.fromLegacyForm(
111
+ i.toString(),
112
+ k
113
+ ).get(), f || (g.expanded = g.inline = !1), g.body = dt(g, { submit: "Submit" })), a.guideId && (g.body.guideId = a.guideId), g;
114
+ }, lt = (i) => {
115
+ const { localState: e } = i;
116
+ return i.tags.includes("category") && i.tags.includes("contact") ? A.ItemList : i.tags.includes("suggestion") ? A.LinkList : (e == null ? void 0 : e.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching" ? K.ContactList : A.ButtonList;
117
+ }, dt = (i, {
118
+ submit: e,
119
+ errors: n,
120
+ meta: t
121
+ }) => {
122
+ var d;
123
+ if ((d = i == null ? void 0 : i.body) != null && d.form) {
124
+ const { body: p } = i, f = new G(p.form);
125
+ return p.form.layout.submit || f.createComponent(it(e, "submit")), n && f.setValidationErrors(n), t && f.setMeta(t), {
126
+ ...p,
127
+ form: f.get()
128
+ };
129
+ }
130
+ return (i == null ? void 0 : i.body) || {};
131
+ }, ut = async (i, e, n) => {
132
+ (await (await i.getAsync("dataClient")).getClient()).parameters.set(e, n);
133
+ }, $ = ({
134
+ messageId: i,
135
+ message: e,
136
+ group: n
137
+ }) => ({
138
+ id: i,
139
+ message: e,
140
+ groupId: n.id,
141
+ isUser: n.isUser
142
+ }), gt = async (i, e, n) => {
143
+ const t = (a, l, u, b = {
144
+ actions: [],
145
+ entryId: l.id,
146
+ groupId: u
147
+ }) => {
148
+ const m = [];
149
+ return b.actions = l.content.items.map((y) => {
150
+ var h;
151
+ const g = {
152
+ label: y.title,
153
+ actionKey: y.actionKey,
154
+ symbol: y.symbol ? y.symbol : void 0,
155
+ description: y.description || void 0
156
+ };
157
+ if (y.data) {
158
+ const x = l.actions[y.actionKey];
159
+ a === K.ContactList ? (m.push(y.data.id), g.data = ct(y.data, {
160
+ guideId: (h = x == null ? void 0 : x.guideId) == null ? void 0 : h.toString()
161
+ })) : g.data = y.data;
162
+ }
163
+ return g;
164
+ }), a === K.ContactList && (b.contactMethodIds = m), b;
165
+ }, d = async (a, l, u = [], b) => {
166
+ var y, g;
167
+ const m = j();
168
+ switch (a.type !== "List" && a.tags.includes("guide") && ((g = (y = a.content.hints) == null ? void 0 : y[0]) != null && g.id) && await ut(
169
+ i,
170
+ "LastGuideId",
171
+ a.content.hints[0].id
172
+ ), a.type) {
173
+ case "List": {
174
+ if (!a.content)
175
+ return [];
176
+ const { items: h, html: x } = a.content, O = [];
177
+ let C = {
178
+ actions: [],
179
+ entryId: a.id,
180
+ groupId: l.id
181
+ };
182
+ if ((h == null ? void 0 : h.length) > 0) {
183
+ const N = lt(a);
184
+ if (C = t(
185
+ N,
186
+ a,
187
+ l.id,
188
+ C
189
+ ), u.length) {
190
+ const L = u.find(
191
+ (T) => T.tags.includes("paging")
192
+ );
193
+ L != null && L.content && (l.meta.paging = {
194
+ actions: L.actions,
195
+ items: L.content.items,
196
+ localState: L.localState
197
+ }, L.content.items.forEach((T) => {
198
+ C.actions.push({
199
+ label: T.title,
200
+ actionKey: T.actionKey,
201
+ data: {
202
+ groupId: b ? b.id : l.id
203
+ }
204
+ });
205
+ }));
206
+ }
207
+ O.unshift(
208
+ $({
209
+ messageId: m,
210
+ message: [N, C],
211
+ group: l
212
+ })
213
+ );
214
+ }
215
+ return x && O.unshift(
216
+ $({
217
+ messageId: m,
218
+ message: [A.HTML, { body: x }],
219
+ group: l
220
+ })
221
+ ), O;
222
+ }
223
+ case "Text": {
224
+ const h = [];
225
+ if (h.push(
226
+ $({
227
+ messageId: m,
228
+ message: [
229
+ A.HTML,
230
+ { body: a.content.html }
231
+ ],
232
+ group: l
233
+ })
234
+ ), u.length) {
235
+ const x = u.find(
236
+ (C) => {
237
+ var N;
238
+ return C.type === "List" && ((N = C.localState) == null ? void 0 : N.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
239
+ }
240
+ ), O = u.find(
241
+ (C) => C.tags.includes("contact") && C.type === "Contact"
242
+ );
243
+ if (O && h.push(
244
+ $({
245
+ messageId: m,
246
+ message: [
247
+ K.ContactMethod,
248
+ {
249
+ contactMethodId: O.content.id,
250
+ groupId: l.id,
251
+ guideId: O.content.guideId
252
+ }
253
+ ],
254
+ group: l
255
+ })
256
+ ), x) {
257
+ const C = t(
258
+ K.ContactList,
259
+ x,
260
+ l.id
261
+ );
262
+ h.push(
263
+ $({
264
+ messageId: m,
265
+ message: [
266
+ A.Separator,
267
+ void 0
268
+ ],
269
+ group: l
270
+ })
271
+ ), h.push(
272
+ $({
273
+ messageId: m,
274
+ message: [K.ContactList, C],
275
+ group: l
276
+ })
277
+ );
278
+ }
279
+ }
280
+ return h;
281
+ }
282
+ case "Contact":
283
+ return [
284
+ $({
285
+ messageId: m,
286
+ message: [
287
+ K.ContactMethod,
288
+ {
289
+ contactMethodId: a.content.id,
290
+ groupId: l.id,
291
+ guideId: a.content.guideId
292
+ }
293
+ ],
294
+ group: l
295
+ })
296
+ ];
297
+ case "Error":
298
+ return [
299
+ $({
300
+ messageId: m,
301
+ message: [
302
+ A.HTML,
303
+ {
304
+ body: a.content.message || "Something went wrong."
305
+ }
306
+ ],
307
+ group: l
308
+ })
309
+ ];
310
+ default:
311
+ return [];
312
+ }
313
+ }, p = /* @__PURE__ */ new Map([
314
+ ["Text", 1],
315
+ ["List", 2],
316
+ ["Contact", 3]
317
+ ]), f = [], k = e.sort((a, l) => {
318
+ const b = p.get(a.type) || 20, m = p.get(l.type) || 20;
319
+ return b - m;
320
+ }).filter((a) => !a.relation).reduce((a, l) => a.concat(l), []), P = (a) => typeof a.actions.selector < "u";
321
+ for (const a of k) {
322
+ const l = e.filter(
323
+ (m) => m.relation === a.id && !P(m)
324
+ ), u = rt({ isUser: !1 });
325
+ (await d(a, u, l, n)).forEach((m) => {
326
+ u.items.push(m);
327
+ }), u.items.length && f.push(u);
328
+ }
329
+ return f;
330
+ };
331
+ class mt {
332
+ constructor(e, n, t, d) {
333
+ M(this, "globalState");
334
+ M(this, "localState");
335
+ M(this, "clientId");
336
+ M(this, "entries");
337
+ this.endpoint = e, this.events = n, this.store = t, this.container = d, this.clientId = j(), this.entries = [];
338
+ }
339
+ async fetchGuide(e) {
340
+ const n = {
341
+ command: { guide: { id: e } },
342
+ globalState: this.globalState,
343
+ clientId: this.clientId
344
+ };
345
+ return await this.postEnvelope(n);
346
+ }
347
+ async postEnvelope(e, n) {
348
+ try {
349
+ const t = await fetch(
350
+ this.endpoint,
351
+ this.getHttpOptions(e)
352
+ );
353
+ if (!t.ok)
354
+ throw t;
355
+ const d = await t.json();
356
+ return this.handleResponse(d, n);
357
+ } catch (t) {
358
+ process.env.NODE_ENV !== "production" && console.warn("Something went wrong when posting the envelope.");
359
+ let d = "Something went wrong";
360
+ return (t.message === "Failed to fetch" || t.message === "NetworkError when attempting to fetch resource.") && (d = "Lost connection... Please try again."), this.events.dispatch("tracking:http-client-error", { message: d, error: t }), this.handleResponse({
361
+ entries: [
362
+ {
363
+ id: j(),
364
+ type: "Error",
365
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
366
+ content: {
367
+ message: d
368
+ },
369
+ actions: {},
370
+ tags: []
371
+ }
372
+ ]
373
+ });
374
+ }
375
+ }
376
+ async handleResponse(e, n) {
377
+ this.entries = this.entries.concat(e.entries), this.store.write(this.entries, "entries");
378
+ const t = e.entries.find((p) => p.type === "Widget");
379
+ return t && this.events.dispatch("provider:widget-entry", t.content), e.globalState && (this.globalState = e.globalState, this.store.write(e.globalState, "globalState")), this.localState = e.entries.reduce(
380
+ (p, f) => f.localState ? f.localState : p,
381
+ void 0
382
+ ), this.store.write(this.localState, "localState"), await gt(
383
+ this.container,
384
+ e.entries,
385
+ n
386
+ );
387
+ }
388
+ getHttpOptions(e) {
389
+ const n = {
390
+ method: "POST",
391
+ headers: {
392
+ Accept: "application/json",
393
+ "Content-Type": "application/json"
394
+ }
395
+ };
396
+ return e.funnel = this.container.get("$widget").name, e.localState = e.localState || this.localState, Object.assign({}, n, {
397
+ body: JSON.stringify(e),
398
+ credentials: "same-origin"
399
+ });
400
+ }
401
+ }
402
+ const ft = "@telia-ace/conversational-hub-bot-provider", pt = "0.0.16", ht = "./index.js", bt = "./index.d.ts", yt = {
403
+ ".": {
404
+ import: "./index.mjs",
405
+ require: "./index.js"
406
+ }
407
+ }, vt = {
408
+ "@telia-ace/widget-conversation": "*",
409
+ "@webprovisions/platform": "^1.1.4",
410
+ "@telia-ace/knowledge-widget-core": "*",
411
+ "@telia-ace/widget-utilities": "*",
412
+ "@telia-ace/widget-services": "*",
413
+ "@telia-ace/widget-core": "*",
414
+ "@telia-ace/knowledge-data-client": "*",
415
+ "@telia-ace/knowledge-widget-adapters": "*",
416
+ "@telia-ace/knowledge-widget-ui": "*",
417
+ "@telia-ace/widget-forms": "*"
418
+ }, wt = {
419
+ "@emotion/react": "11.11.1",
420
+ "@emotion/styled": "11.11.0",
421
+ react: "18.2.0"
422
+ }, H = {
423
+ name: ft,
424
+ version: pt,
425
+ main: ht,
426
+ types: bt,
427
+ exports: yt,
428
+ dependencies: vt,
429
+ peerDependencies: wt
430
+ }, kt = async (i) => {
431
+ var J, U, D, R, z;
432
+ const e = "chub-plugin", n = i.get("$widget"), t = (z = (R = (D = (U = (J = n == null ? void 0 : n.container) == null ? void 0 : J.get("$settings")) == null ? void 0 : U.components) == null ? void 0 : D.conversation) == null ? void 0 : R.properties) == null ? void 0 : z.conversationalHubPluginSettings;
433
+ let d, p, f, k = (t == null ? void 0 : t.connectionAttempts) ?? 5, P, a, l;
434
+ V.info((H == null ? void 0 : H.name) + " version: " + (H == null ? void 0 : H.version)), q(
435
+ i,
436
+ K.ContactMethod,
437
+ import("./contact-method-e0b87a2a.mjs")
438
+ ), q(
439
+ i,
440
+ K.ContactList,
441
+ import("./contact-method-list-818567d0.mjs")
442
+ );
443
+ const u = await Q(
444
+ i,
445
+ "conversational-hub-session",
446
+ X.Necessary
447
+ ), b = (r, s) => {
448
+ const { knowledgeBotEndpoint: o } = s.properties();
449
+ if (o)
450
+ return `${o}/conversations`;
451
+ const { data: c } = r.get("$settings");
452
+ if (!c || !c.projection)
453
+ throw new Error("Missing Knowledge Bot endpoint");
454
+ return `${c.projection}/conversations`;
455
+ }, m = (r) => {
456
+ var s;
457
+ if (r.target instanceof HTMLButtonElement) {
458
+ const o = r.target;
459
+ if ((s = o.getAttribute("class")) != null && s.includes("dialog-option-cx")) {
460
+ const c = o.getAttribute("value");
461
+ N(c), d.user.print(
462
+ c.replace(/</g, "&lt;").replace(/>/g, "&gt;")
463
+ );
464
+ }
465
+ }
466
+ };
467
+ (await nt.getInstance(
468
+ i
469
+ )).registerProvider(e, (r, s) => {
470
+ d = r, p = d.controller, f = d.createAgent({
471
+ name: t == null ? void 0 : t.botName,
472
+ avatar: t == null ? void 0 : t.avatar
473
+ }), l = b(i, s);
474
+ const { events: o } = i.get("$widget"), c = new st(i);
475
+ a = new mt(l, o, c, i), O();
476
+ const [v] = i.get("widgetDOMElements");
477
+ v.querySelectorAll(".humany-bot-contact-method").forEach((I) => {
478
+ var w;
479
+ (w = I.closest(".humany-conversation-entry")) == null || w.remove();
480
+ }), v.removeEventListener("click", m), v.addEventListener("click", m);
481
+ const S = () => p.providers.includes(e);
482
+ s.actions.create("submit", (I) => {
483
+ const { events: w } = i.get("$widget");
484
+ w.dispatch("conversational-hub-provider:form-submitted", I);
485
+ }), s.actions.watch("conversation.action", (I, w) => (S() && C(I), w(I))), s.actions.watch("conversation.user-submit", (I, w) => {
486
+ if (!S())
487
+ return w(I);
488
+ N(I.text), d.user.print(
489
+ I.text.replace(/</g, "&lt;").replace(/>/g, "&gt;")
490
+ );
491
+ }), n.events.subscribe(
492
+ "conversational-hub-provider:fetch-guide",
493
+ async (I, w) => {
494
+ if (!(w != null && w.guideId))
495
+ return;
496
+ const B = await a.fetchGuide(parseInt(w.guideId));
497
+ B && g(B);
498
+ }
499
+ );
500
+ });
501
+ async function g(r) {
502
+ r.forEach((s) => {
503
+ const o = s.items.map((c) => c.message);
504
+ s.writer = f.print(o);
505
+ });
506
+ }
507
+ const h = sessionStorage.getItem(
508
+ `humany_${x(
509
+ n.implementation.tenant + n.implementation.name + n.name
510
+ )}`
511
+ );
512
+ function x(r) {
513
+ let s = 0;
514
+ if (r.length === 0)
515
+ return s.toString();
516
+ for (let o = 0; o < r.length; o++) {
517
+ const c = r.charCodeAt(o);
518
+ s = (s << 5) - s + c, s = s & s;
519
+ }
520
+ return s.toString();
521
+ }
522
+ async function O(r = !0) {
523
+ const s = await W(
524
+ i,
525
+ "conversational-hub-session"
526
+ ), o = i.get("dialogflowSettings");
527
+ if (o != null && o.silentStart)
528
+ delete o.silentStart, n.events.dispatch("conversational-hub-provider:started", {});
529
+ else if (s != null && s.sessionId && r)
530
+ P = s == null ? void 0 : s.sessionId;
531
+ else if (t != null && t.welcomeMessage)
532
+ h != null && h.length || f.print(t == null ? void 0 : t.welcomeMessage);
533
+ else {
534
+ const c = {
535
+ extension: t.extension
536
+ };
537
+ o != null && o.initCommand && (c.input = {
538
+ type: "event",
539
+ language: t.language,
540
+ content: {
541
+ name: o.initCommand,
542
+ parameters: o.initParameters || {}
543
+ }
544
+ }), await L(
545
+ t.baseUrl + "/initialize",
546
+ JSON.stringify(c),
547
+ r
548
+ );
549
+ }
550
+ }
551
+ async function C(r) {
552
+ await L(
553
+ `${t.baseUrl}/input/${P}`,
554
+ `{"type":"button_click","language":"${t.language}","content":{"button_text":"${r.label}","value":"${r.actionKey}"}}`
555
+ );
556
+ }
557
+ async function N(r) {
558
+ const s = { text: r };
559
+ if (!P) {
560
+ const o = {
561
+ extension: t.extension
562
+ }, c = i.get("dialogflowSettings");
563
+ c != null && c.initCommand && (o.input = {
564
+ type: "event",
565
+ language: t.language,
566
+ content: {
567
+ name: c.initCommand,
568
+ parameters: c.initParameters || {}
569
+ }
570
+ }), await L(
571
+ t.baseUrl + "/initialize",
572
+ JSON.stringify(o),
573
+ !1
574
+ );
575
+ }
576
+ await L(
577
+ `${t.baseUrl}/input/${P}`,
578
+ `{"type":"text","language":"${t.language}","content":${JSON.stringify(s)}}`
579
+ );
580
+ }
581
+ async function L(r, s, o = !0) {
582
+ var I, w, B, F;
583
+ const c = d.loading();
584
+ let v, S;
585
+ try {
586
+ v = await fetch(r, {
587
+ method: "POST",
588
+ body: s
589
+ }), S = await (v == null ? void 0 : v.json());
590
+ } catch (_) {
591
+ _ instanceof SyntaxError ? V.error("Incorrect JSON format.", _) : V.error("Network error: check connection or baseUrl.", _), c(), f.print(t.errorMessage || "An error occurred");
592
+ return;
593
+ }
594
+ if (!(v != null && v.ok)) {
595
+ if (V.error(`HTTP ${v == null ? void 0 : v.status}.`, JSON.stringify(S)), c(), !!((I = S == null ? void 0 : S.errors) != null && I.filter(
596
+ (E) => E.code === "1003"
597
+ ).length) && k > 0) {
598
+ k--, await new Promise((Z) => setTimeout(Z, 500)), await O(!1);
599
+ const E = JSON.parse(s);
600
+ (w = E == null ? void 0 : E.content) != null && w.text ? await N(E.content.text) : (B = E == null ? void 0 : E.content) != null && B.action && await C((F = E == null ? void 0 : E.content) == null ? void 0 : F.action);
601
+ } else
602
+ f.print(t.errorMessage || "An error occurred");
603
+ return;
604
+ }
605
+ c(), k = (t == null ? void 0 : t.connectionAttempts) ?? 5, S != null && S.action_payload && T(S.action_payload), P = S == null ? void 0 : S.session_id, u({ sessionId: P }), o && Y(S);
606
+ }
607
+ async function T(r) {
608
+ var v;
609
+ const s = (v = r == null ? void 0 : r.transfer_parameters) == null ? void 0 : v.contactGuide, o = d.loading(), c = await a.fetchGuide(parseInt(s));
610
+ o(), c && g(c);
611
+ }
612
+ function Y(r) {
613
+ var s;
614
+ r == null || r.agent_response.forEach((o) => {
615
+ f.print(o.payload.text);
616
+ }), (s = r == null ? void 0 : r.prompt) != null && s.items && f.print([
617
+ "button-list",
618
+ {
619
+ actions: r.prompt.items.map((o) => {
620
+ let c;
621
+ return o.type === "button" && (c = { actionKey: o.value, label: o.text }), o.type === "link" && (c = {
622
+ actionKey: o.value,
623
+ label: o.text,
624
+ data: {
625
+ href: o.url,
626
+ target: (t == null ? void 0 : t.linkTarget) ?? "_blank"
627
+ }
628
+ }), c;
629
+ })
630
+ }
631
+ ]);
632
+ }
633
+ }, Pt = ot((i) => {
634
+ i.createCommand(
635
+ "dialogflow",
636
+ (e) => (n) => {
637
+ const t = {
638
+ initCommand: n.initCommand
639
+ };
640
+ n.initParameters && (t.initParameters = n.initParameters), n.silentStart && (t.silentStart = n.silentStart), e.container.register("dialogflowSettings", t);
641
+ }
642
+ );
643
+ });
644
+ export {
645
+ kt as C,
646
+ Pt as d,
647
+ dt as u
648
+ };