@botonic/webchat-core 2.23.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 (88) hide show
  1. package/CHANGELOG.md +247 -0
  2. package/PR-LOCALE-SUPPORT.md +156 -0
  3. package/README.md +11 -0
  4. package/package.json +29 -0
  5. package/src/index.d.ts +11 -0
  6. package/src/index.js +15 -0
  7. package/src/index.js.map +1 -0
  8. package/src/lib/controllers/index.d.ts +2 -0
  9. package/src/lib/controllers/index.js +3 -0
  10. package/src/lib/controllers/index.js.map +1 -0
  11. package/src/lib/controllers/public.d.ts +383 -0
  12. package/src/lib/controllers/public.js +194 -0
  13. package/src/lib/controllers/public.js.map +1 -0
  14. package/src/lib/controllers/webchat.d.ts +414 -0
  15. package/src/lib/controllers/webchat.js +255 -0
  16. package/src/lib/controllers/webchat.js.map +1 -0
  17. package/src/lib/domain/index.d.ts +50 -0
  18. package/src/lib/domain/index.js +11 -0
  19. package/src/lib/domain/index.js.map +1 -0
  20. package/src/lib/infra/clients/hubtype-api.d.ts +53 -0
  21. package/src/lib/infra/clients/hubtype-api.js +189 -0
  22. package/src/lib/infra/clients/hubtype-api.js.map +1 -0
  23. package/src/lib/infra/clients/index.d.ts +3 -0
  24. package/src/lib/infra/clients/index.js +4 -0
  25. package/src/lib/infra/clients/index.js.map +1 -0
  26. package/src/lib/infra/clients/realtime.d.ts +92 -0
  27. package/src/lib/infra/clients/realtime.js +277 -0
  28. package/src/lib/infra/clients/realtime.js.map +1 -0
  29. package/src/lib/infra/clients/types.d.ts +21 -0
  30. package/src/lib/infra/clients/types.js +2 -0
  31. package/src/lib/infra/clients/types.js.map +1 -0
  32. package/src/lib/infra/repositories/index.d.ts +1 -0
  33. package/src/lib/infra/repositories/index.js +2 -0
  34. package/src/lib/infra/repositories/index.js.map +1 -0
  35. package/src/lib/infra/repositories/storage/base-storage.d.ts +23 -0
  36. package/src/lib/infra/repositories/storage/base-storage.js +102 -0
  37. package/src/lib/infra/repositories/storage/base-storage.js.map +1 -0
  38. package/src/lib/infra/repositories/storage/browser-storage.d.ts +9 -0
  39. package/src/lib/infra/repositories/storage/browser-storage.js +46 -0
  40. package/src/lib/infra/repositories/storage/browser-storage.js.map +1 -0
  41. package/src/lib/infra/repositories/storage/factory.d.ts +2 -0
  42. package/src/lib/infra/repositories/storage/factory.js +20 -0
  43. package/src/lib/infra/repositories/storage/factory.js.map +1 -0
  44. package/src/lib/infra/repositories/storage/in-memory.d.ts +9 -0
  45. package/src/lib/infra/repositories/storage/in-memory.js +21 -0
  46. package/src/lib/infra/repositories/storage/in-memory.js.map +1 -0
  47. package/src/lib/infra/repositories/storage/index.d.ts +7 -0
  48. package/src/lib/infra/repositories/storage/index.js +7 -0
  49. package/src/lib/infra/repositories/storage/index.js.map +1 -0
  50. package/src/lib/infra/repositories/storage/local-storage.d.ts +4 -0
  51. package/src/lib/infra/repositories/storage/local-storage.js +7 -0
  52. package/src/lib/infra/repositories/storage/local-storage.js.map +1 -0
  53. package/src/lib/infra/repositories/storage/session-storage.d.ts +4 -0
  54. package/src/lib/infra/repositories/storage/session-storage.js +7 -0
  55. package/src/lib/infra/repositories/storage/session-storage.js.map +1 -0
  56. package/src/lib/infra/repositories/storage/types.d.ts +30 -0
  57. package/src/lib/infra/repositories/storage/types.js +2 -0
  58. package/src/lib/infra/repositories/storage/types.js.map +1 -0
  59. package/src/lib/machines/message.d.ts +200 -0
  60. package/src/lib/machines/message.js +210 -0
  61. package/src/lib/machines/message.js.map +1 -0
  62. package/src/lib/machines/overlay.d.ts +299 -0
  63. package/src/lib/machines/overlay.js +217 -0
  64. package/src/lib/machines/overlay.js.map +1 -0
  65. package/src/lib/machines/webchat.d.ts +641 -0
  66. package/src/lib/machines/webchat.js +768 -0
  67. package/src/lib/machines/webchat.js.map +1 -0
  68. package/src/lib/services/webchat-service.d.ts +125 -0
  69. package/src/lib/services/webchat-service.js +339 -0
  70. package/src/lib/services/webchat-service.js.map +1 -0
  71. package/src/lib/utils/locale-detection.d.ts +2 -0
  72. package/src/lib/utils/locale-detection.js +20 -0
  73. package/src/lib/utils/locale-detection.js.map +1 -0
  74. package/src/lib/utils/timezone-to-country.d.ts +1 -0
  75. package/src/lib/utils/timezone-to-country.js +105 -0
  76. package/src/lib/utils/timezone-to-country.js.map +1 -0
  77. package/src/lib/webview/constants.d.ts +10 -0
  78. package/src/lib/webview/constants.js +10 -0
  79. package/src/lib/webview/constants.js.map +1 -0
  80. package/src/lib/webview/index.d.ts +3 -0
  81. package/src/lib/webview/index.js +4 -0
  82. package/src/lib/webview/index.js.map +1 -0
  83. package/src/lib/webview/types.d.ts +35 -0
  84. package/src/lib/webview/types.js +16 -0
  85. package/src/lib/webview/types.js.map +1 -0
  86. package/src/lib/webview/utils.d.ts +9 -0
  87. package/src/lib/webview/utils.js +33 -0
  88. package/src/lib/webview/utils.js.map +1 -0
@@ -0,0 +1,383 @@
1
+ import type { HubtypeUser, WebchatMessage } from '@botonic/shared';
2
+ import { ConfirmCloseOption, OverlayState } from '../domain';
3
+ import type { WebchatController, WebchatEmittedEvent } from './webchat';
4
+ export declare class PublicWebchatController {
5
+ private controller;
6
+ private isInitialized;
7
+ private isFullyReady;
8
+ private resolveReady;
9
+ /** Promise that resolves when webchat is fully initialized and connected */
10
+ ready: Promise<void>;
11
+ constructor();
12
+ setController(controller: WebchatController): void;
13
+ /**
14
+ * Signal that webchat is fully ready (controller + connection established).
15
+ * This resolves the ready promise and dispatches the botonic:ready event.
16
+ */
17
+ signalReady(): void;
18
+ private getController;
19
+ isReady(): boolean;
20
+ sendMessage: (content: WebchatMessage) => void;
21
+ open: () => void;
22
+ close: () => void;
23
+ toggle: () => void;
24
+ updateUser: (user: Partial<HubtypeUser>) => void;
25
+ setUserLocale: (locale: string) => void;
26
+ setUserCountry: (country: string) => void;
27
+ setSystemLocale: (systemLocale: string) => void;
28
+ setUserExtraData: (extraData: Record<string, any>) => void;
29
+ clearMessages: () => void;
30
+ updateMessage: (messageId: string, updates: Partial<WebchatMessage>) => void;
31
+ deleteMessage: (messageId: string) => void;
32
+ setTyping: (isTyping: boolean) => void;
33
+ reconnect: () => void;
34
+ setOnline: (online: boolean) => void;
35
+ getMessages: () => WebchatMessage<import("@botonic/shared").MessageType>[];
36
+ getConnectionStatus: () => string;
37
+ isOnline: () => boolean;
38
+ isOpen: () => boolean;
39
+ getUser: () => HubtypeUser | null;
40
+ /**
41
+ * Overlay API - supports webviews (iframe) and cover components (inline React)
42
+ */
43
+ overlay: {
44
+ openWebview: (url: string, options?: {
45
+ title?: string;
46
+ icon?: string;
47
+ allowClose?: boolean;
48
+ }) => void;
49
+ openCover: (componentId: string, options?: {
50
+ title?: string;
51
+ icon?: string;
52
+ props?: Record<string, unknown>;
53
+ allowClose?: boolean;
54
+ }) => void;
55
+ close: () => void;
56
+ setTitle: (title: string) => void;
57
+ setIcon: (icon: string) => void;
58
+ setAllowClose: (allow: boolean) => void;
59
+ setConfirmClose: (config: ConfirmCloseOption) => void;
60
+ navigate: (url: string) => void;
61
+ sendPayload: (payload: unknown) => void;
62
+ isOpen: () => boolean;
63
+ getState: () => OverlayState | null;
64
+ getType: () => "webview" | "cover" | null;
65
+ isWebview: () => boolean;
66
+ isCover: () => boolean;
67
+ getActor: () => import("xstate").ActorRef<import("xstate").MachineSnapshot<{
68
+ overlayState: OverlayState | null;
69
+ }, {
70
+ type: "OPEN_WEBVIEW";
71
+ url: string;
72
+ title?: string;
73
+ icon?: string;
74
+ allowClose?: boolean;
75
+ } | {
76
+ type: "OPEN_COVER";
77
+ componentId: string;
78
+ title?: string;
79
+ icon?: string;
80
+ props?: Record<string, unknown>;
81
+ allowClose?: boolean;
82
+ } | {
83
+ type: "CLOSE";
84
+ } | {
85
+ type: "SET_TITLE";
86
+ title: string;
87
+ } | {
88
+ type: "SET_ICON";
89
+ icon: string;
90
+ } | {
91
+ type: "SET_ALLOW_CLOSE";
92
+ allow: boolean;
93
+ } | {
94
+ type: "SET_CONFIRM_CLOSE";
95
+ config: ConfirmCloseOption;
96
+ } | {
97
+ type: "NAVIGATE";
98
+ url: string;
99
+ } | {
100
+ type: "SEND_PAYLOAD";
101
+ payload: unknown;
102
+ }, {}, "idle" | "open", string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
103
+ id: "overlay";
104
+ states: {
105
+ readonly idle: {};
106
+ readonly open: {};
107
+ };
108
+ }>, {
109
+ type: "OPEN_WEBVIEW";
110
+ url: string;
111
+ title?: string;
112
+ icon?: string;
113
+ allowClose?: boolean;
114
+ } | {
115
+ type: "OPEN_COVER";
116
+ componentId: string;
117
+ title?: string;
118
+ icon?: string;
119
+ props?: Record<string, unknown>;
120
+ allowClose?: boolean;
121
+ } | {
122
+ type: "CLOSE";
123
+ } | {
124
+ type: "SET_TITLE";
125
+ title: string;
126
+ } | {
127
+ type: "SET_ICON";
128
+ icon: string;
129
+ } | {
130
+ type: "SET_ALLOW_CLOSE";
131
+ allow: boolean;
132
+ } | {
133
+ type: "SET_CONFIRM_CLOSE";
134
+ config: ConfirmCloseOption;
135
+ } | {
136
+ type: "NAVIGATE";
137
+ url: string;
138
+ } | {
139
+ type: "SEND_PAYLOAD";
140
+ payload: unknown;
141
+ }, {
142
+ type: "WEBVIEW_OPENED";
143
+ url: string;
144
+ title: string;
145
+ } | {
146
+ type: "WEBVIEW_CLOSED";
147
+ } | {
148
+ type: "WEBVIEW_PAYLOAD";
149
+ payload: unknown;
150
+ } | {
151
+ type: "COVER_OPENED";
152
+ componentId: string;
153
+ title: string;
154
+ } | {
155
+ type: "COVER_CLOSED";
156
+ } | {
157
+ type: "OVERLAY_CLOSED";
158
+ }>;
159
+ };
160
+ send: (event: any) => void;
161
+ getState: () => import("xstate").MachineSnapshot<import("../..").WebchatControllerContext, {
162
+ type: "INITIALIZE";
163
+ } | {
164
+ type: "RETRY_INITIALIZATION";
165
+ } | {
166
+ type: "RETRY_CONNECTION";
167
+ } | {
168
+ type: "CONNECTION_STATUS_CHANGED";
169
+ status: string;
170
+ } | {
171
+ type: "RECONNECT";
172
+ } | {
173
+ type: "CANCEL_RECONNECT";
174
+ } | {
175
+ type: "SET_ONLINE";
176
+ online: boolean;
177
+ } | {
178
+ type: "SET_TYPING";
179
+ isTypingOn: boolean;
180
+ } | {
181
+ type: "TOGGLE_WEBCHAT";
182
+ isOpen?: boolean;
183
+ } | {
184
+ type: "SEND_MESSAGE";
185
+ message: WebchatMessage;
186
+ } | {
187
+ type: "MESSAGE_RECEIVED";
188
+ message: WebchatMessage;
189
+ } | {
190
+ type: "MESSAGE_STATUS_CHANGED";
191
+ messageId: string;
192
+ status: import("../..").WebchatMessageStatus;
193
+ message: WebchatMessage;
194
+ } | {
195
+ type: "UPDATE_USER";
196
+ user: Partial<HubtypeUser>;
197
+ } | {
198
+ type: "CLEAR_MESSAGES";
199
+ } | {
200
+ type: "UPDATE_MESSAGE";
201
+ messageId: string;
202
+ updates: Partial<WebchatMessage>;
203
+ } | {
204
+ type: "DELETE_MESSAGE";
205
+ messageId: string;
206
+ } | {
207
+ type: "PERSISTENCE.SAVE_MESSAGE";
208
+ } | {
209
+ type: "WEBVIEW.OPEN";
210
+ url: string;
211
+ title?: string;
212
+ }, {
213
+ [x: string]: import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
214
+ webchatService: import("../..").WebchatService;
215
+ storageRepository: import("../..").StorageRepository;
216
+ }, {
217
+ config: import("../domain").WebchatConfig;
218
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<import("../..").StoredMessage[], {
219
+ storageRepository: import("../..").StorageRepository;
220
+ appId: string;
221
+ userId: string;
222
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<HubtypeUser | null, {
223
+ storageRepository: import("../..").StorageRepository;
224
+ appId: string;
225
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
226
+ success: boolean;
227
+ }, {
228
+ webchatService: import("../..").WebchatService;
229
+ user: HubtypeUser;
230
+ messages: WebchatMessage[];
231
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
232
+ success: boolean;
233
+ }, {
234
+ webchatService: import("../..").WebchatService;
235
+ user: HubtypeUser;
236
+ messages: WebchatMessage[];
237
+ attempt: number;
238
+ }, import("xstate").EventObject>> | undefined;
239
+ }, "idle" | "connectionFailed" | "reconnecting" | "operational" | {
240
+ initializing: "initializingServices" | "loadingSession" | "loadingMessages" | "connectingToRealtime" | "spawningPersistedMessages";
241
+ }, string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
242
+ id: "webchat";
243
+ states: {
244
+ readonly idle: {};
245
+ readonly initializing: {
246
+ states: {
247
+ readonly initializingServices: {};
248
+ readonly loadingSession: {};
249
+ readonly loadingMessages: {};
250
+ readonly spawningPersistedMessages: {};
251
+ readonly connectingToRealtime: {};
252
+ };
253
+ };
254
+ readonly connectionFailed: {};
255
+ readonly reconnecting: {};
256
+ readonly operational: {};
257
+ };
258
+ }>;
259
+ subscribe: (callback: (state: any) => void) => import("xstate").Subscription;
260
+ on: <T extends WebchatEmittedEvent["type"]>(eventType: T, callback: (event: Extract<WebchatEmittedEvent, {
261
+ type: T;
262
+ }>) => void) => () => void;
263
+ get messages(): WebchatMessage<import("@botonic/shared").MessageType>[];
264
+ get user(): HubtypeUser | null;
265
+ get connectionStatus(): string;
266
+ get isOpenState(): boolean;
267
+ get overlayState(): OverlayState | null;
268
+ help: () => void;
269
+ debug: {
270
+ getController: () => WebchatController;
271
+ getState: () => import("xstate").MachineSnapshot<import("../..").WebchatControllerContext, {
272
+ type: "INITIALIZE";
273
+ } | {
274
+ type: "RETRY_INITIALIZATION";
275
+ } | {
276
+ type: "RETRY_CONNECTION";
277
+ } | {
278
+ type: "CONNECTION_STATUS_CHANGED";
279
+ status: string;
280
+ } | {
281
+ type: "RECONNECT";
282
+ } | {
283
+ type: "CANCEL_RECONNECT";
284
+ } | {
285
+ type: "SET_ONLINE";
286
+ online: boolean;
287
+ } | {
288
+ type: "SET_TYPING";
289
+ isTypingOn: boolean;
290
+ } | {
291
+ type: "TOGGLE_WEBCHAT";
292
+ isOpen?: boolean;
293
+ } | {
294
+ type: "SEND_MESSAGE";
295
+ message: WebchatMessage;
296
+ } | {
297
+ type: "MESSAGE_RECEIVED";
298
+ message: WebchatMessage;
299
+ } | {
300
+ type: "MESSAGE_STATUS_CHANGED";
301
+ messageId: string;
302
+ status: import("../..").WebchatMessageStatus;
303
+ message: WebchatMessage;
304
+ } | {
305
+ type: "UPDATE_USER";
306
+ user: Partial<HubtypeUser>;
307
+ } | {
308
+ type: "CLEAR_MESSAGES";
309
+ } | {
310
+ type: "UPDATE_MESSAGE";
311
+ messageId: string;
312
+ updates: Partial<WebchatMessage>;
313
+ } | {
314
+ type: "DELETE_MESSAGE";
315
+ messageId: string;
316
+ } | {
317
+ type: "PERSISTENCE.SAVE_MESSAGE";
318
+ } | {
319
+ type: "WEBVIEW.OPEN";
320
+ url: string;
321
+ title?: string;
322
+ }, {
323
+ [x: string]: import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
324
+ webchatService: import("../..").WebchatService;
325
+ storageRepository: import("../..").StorageRepository;
326
+ }, {
327
+ config: import("../domain").WebchatConfig;
328
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<import("../..").StoredMessage[], {
329
+ storageRepository: import("../..").StorageRepository;
330
+ appId: string;
331
+ userId: string;
332
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<HubtypeUser | null, {
333
+ storageRepository: import("../..").StorageRepository;
334
+ appId: string;
335
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
336
+ success: boolean;
337
+ }, {
338
+ webchatService: import("../..").WebchatService;
339
+ user: HubtypeUser;
340
+ messages: WebchatMessage[];
341
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
342
+ success: boolean;
343
+ }, {
344
+ webchatService: import("../..").WebchatService;
345
+ user: HubtypeUser;
346
+ messages: WebchatMessage[];
347
+ attempt: number;
348
+ }, import("xstate").EventObject>> | undefined;
349
+ }, "idle" | "connectionFailed" | "reconnecting" | "operational" | {
350
+ initializing: "initializingServices" | "loadingSession" | "loadingMessages" | "connectingToRealtime" | "spawningPersistedMessages";
351
+ }, string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
352
+ id: "webchat";
353
+ states: {
354
+ readonly idle: {};
355
+ readonly initializing: {
356
+ states: {
357
+ readonly initializingServices: {};
358
+ readonly loadingSession: {};
359
+ readonly loadingMessages: {};
360
+ readonly spawningPersistedMessages: {};
361
+ readonly connectingToRealtime: {};
362
+ };
363
+ };
364
+ readonly connectionFailed: {};
365
+ readonly reconnecting: {};
366
+ readonly operational: {};
367
+ };
368
+ }>;
369
+ logState: () => void;
370
+ };
371
+ }
372
+ export declare const publicWebchatController: PublicWebchatController;
373
+ export declare function setupGlobalWebchatAPI(controller: WebchatController): void;
374
+ /**
375
+ * Ensure window.botonic has open/ready/close etc. before webchat is loaded.
376
+ * Call when the trigger mounts so window.botonic.open() can be used to load and open.
377
+ */
378
+ export declare function ensureGlobalWebchatAPI(): void;
379
+ /**
380
+ * Signal that webchat is fully ready (connection established).
381
+ * Call this after setupGlobalWebchatAPI when the connection is confirmed.
382
+ */
383
+ export declare function signalWebchatReady(): void;
@@ -0,0 +1,194 @@
1
+ export class PublicWebchatController {
2
+ constructor() {
3
+ this.controller = null;
4
+ this.isInitialized = false;
5
+ this.isFullyReady = false;
6
+ this.resolveReady = null;
7
+ this.sendMessage = (content) => this.getController().sendMessage(content);
8
+ this.open = () => {
9
+ if (this.isInitialized && this.controller) {
10
+ this.controller.open();
11
+ }
12
+ else if (typeof window !== 'undefined') {
13
+ // Webchat not loaded yet (lazy trigger). Ask trigger to load and open.
14
+ window.dispatchEvent(new CustomEvent('botonic:open'));
15
+ }
16
+ };
17
+ this.close = () => {
18
+ if (this.isInitialized && this.controller)
19
+ this.controller.close();
20
+ };
21
+ this.toggle = () => {
22
+ if (this.isInitialized && this.controller) {
23
+ this.controller.toggle();
24
+ }
25
+ else if (typeof window !== 'undefined') {
26
+ window.dispatchEvent(new CustomEvent('botonic:open'));
27
+ }
28
+ };
29
+ this.updateUser = (user) => this.getController().updateUser(user);
30
+ this.setUserLocale = (locale) => this.getController().setUserLocale(locale);
31
+ // TODO: Think how we should approach this with the new session model
32
+ this.setUserCountry = (country) => this.getController().setUserCountry(country);
33
+ this.setSystemLocale = (systemLocale) => this.getController().setSystemLocale(systemLocale);
34
+ this.setUserExtraData = (extraData) => this.getController().setUserExtraData(extraData);
35
+ this.clearMessages = () => this.getController().clearMessages();
36
+ this.updateMessage = (messageId, updates) => this.getController().updateMessage(messageId, updates);
37
+ this.deleteMessage = (messageId) => this.getController().deleteMessage(messageId);
38
+ this.setTyping = (isTyping) => this.getController().setTyping(isTyping);
39
+ this.reconnect = () => this.getController().reconnect();
40
+ this.setOnline = (online) => this.getController().setOnline(online);
41
+ this.getMessages = () => this.getController().getMessages();
42
+ this.getConnectionStatus = () => this.getController().getConnectionStatus();
43
+ this.isOnline = () => this.getController().isOnline();
44
+ this.isOpen = () => this.getController().isOpen();
45
+ this.getUser = () => this.getController().getUser();
46
+ /**
47
+ * Overlay API - supports webviews (iframe) and cover components (inline React)
48
+ */
49
+ this.overlay = {
50
+ // Opening overlays
51
+ openWebview: (url, options) => this.getController().overlay.openWebview(url, options),
52
+ openCover: (componentId, options) => this.getController().overlay.openCover(componentId, options),
53
+ // Closing
54
+ close: () => this.getController().overlay.close(),
55
+ // Properties
56
+ setTitle: (title) => this.getController().overlay.setTitle(title),
57
+ setIcon: (icon) => this.getController().overlay.setIcon(icon),
58
+ setAllowClose: (allow) => this.getController().overlay.setAllowClose(allow),
59
+ setConfirmClose: (config) => this.getController().overlay.setConfirmClose(config),
60
+ // Webview-specific
61
+ navigate: (url) => this.getController().overlay.navigate(url),
62
+ sendPayload: (payload) => this.getController().overlay.sendPayload(payload),
63
+ // State queries
64
+ isOpen: () => this.getController().overlay.isOpen(),
65
+ getState: () => this.getController().overlay.getState(),
66
+ getType: () => this.getController().overlay.getType(),
67
+ isWebview: () => this.getController().overlay.isWebview(),
68
+ isCover: () => this.getController().overlay.isCover(),
69
+ getActor: () => this.getController().overlay.getActor(),
70
+ };
71
+ this.send = (event) => this.getController().send(event);
72
+ this.getState = () => this.getController().getState();
73
+ this.subscribe = (callback) => this.getController().subscribe(callback);
74
+ this.on = (eventType, callback) => this.getController().on(eventType, callback);
75
+ this.help = () => {
76
+ console.log(`
77
+ Botonic Webchat Console API
78
+
79
+ MESSAGING:
80
+ botonic.sendMessage('Hello!')
81
+ botonic.clearMessages()
82
+ botonic.deleteMessage(messageId)
83
+
84
+ WEBCHAT CONTROL:
85
+ botonic.open() / botonic.close() / botonic.toggle()
86
+ botonic.isOpen()
87
+
88
+ USER:
89
+ botonic.updateUser({name: 'John'})
90
+ botonic.setUserLocale('es') / setUserCountry('ES') / setSystemLocale('es-ES')
91
+ botonic.setUserExtraData({key: 'value'})
92
+
93
+ OVERLAY (webview or cover component):
94
+ botonic.overlay.openWebview(url, {title?, icon?, allowClose?})
95
+ botonic.overlay.openCover(componentId, {title?, icon?, props?, allowClose?})
96
+ botonic.overlay.close()
97
+ botonic.overlay.setTitle(title) / setIcon(icon)
98
+ botonic.overlay.setAllowClose(boolean)
99
+ botonic.overlay.setConfirmClose(config)
100
+ botonic.overlay.navigate(url) // webview only
101
+ botonic.overlay.sendPayload(payload) // webview only
102
+ botonic.overlay.isOpen() / getState() / getType()
103
+ botonic.overlay.isWebview() / isCover()
104
+
105
+ STATE ACCESS:
106
+ botonic.messages / botonic.user / botonic.connectionStatus / botonic.overlayState
107
+ botonic.getMessages() / getUser() / isOnline()
108
+
109
+ MACHINE ACCESS:
110
+ botonic.send({type: 'EVENT'})
111
+ botonic.subscribe(callback)
112
+ botonic.on('MESSAGE_SENT', callback)
113
+ `);
114
+ };
115
+ this.debug = {
116
+ getController: () => this.getController(),
117
+ getState: () => this.getController().getState(),
118
+ logState: () => this.getController().debug.logState(),
119
+ };
120
+ this.ready = new Promise(resolve => {
121
+ this.resolveReady = resolve;
122
+ });
123
+ }
124
+ setController(controller) {
125
+ this.controller = controller;
126
+ this.isInitialized = true;
127
+ }
128
+ /**
129
+ * Signal that webchat is fully ready (controller + connection established).
130
+ * This resolves the ready promise and dispatches the botonic:ready event.
131
+ */
132
+ signalReady() {
133
+ var _a;
134
+ if (this.isFullyReady)
135
+ return; // Only signal once
136
+ this.isFullyReady = true;
137
+ (_a = this.resolveReady) === null || _a === void 0 ? void 0 : _a.call(this);
138
+ if (typeof window !== 'undefined') {
139
+ window.dispatchEvent(new CustomEvent('botonic:ready'));
140
+ }
141
+ }
142
+ getController() {
143
+ if (!this.isInitialized || !this.controller) {
144
+ throw new Error('Webchat not initialized. Make sure WebchatProvider is mounted.');
145
+ }
146
+ return this.controller;
147
+ }
148
+ isReady() {
149
+ return this.isInitialized && !!this.controller;
150
+ }
151
+ get messages() {
152
+ return this.isReady() ? this.getController().messages : [];
153
+ }
154
+ get user() {
155
+ return this.isReady() ? this.getController().user : null;
156
+ }
157
+ get connectionStatus() {
158
+ return this.isReady()
159
+ ? this.getController().getConnectionStatus()
160
+ : 'disconnected';
161
+ }
162
+ get isOpenState() {
163
+ return this.isReady() ? this.getController().isOpen() : false;
164
+ }
165
+ get overlayState() {
166
+ return this.isReady() ? this.getController().overlay.getState() : null;
167
+ }
168
+ }
169
+ export const publicWebchatController = new PublicWebchatController();
170
+ export function setupGlobalWebchatAPI(controller) {
171
+ publicWebchatController.setController(controller);
172
+ if (typeof window !== 'undefined') {
173
+ ;
174
+ window.botonic = publicWebchatController;
175
+ }
176
+ }
177
+ /**
178
+ * Ensure window.botonic has open/ready/close etc. before webchat is loaded.
179
+ * Call when the trigger mounts so window.botonic.open() can be used to load and open.
180
+ */
181
+ export function ensureGlobalWebchatAPI() {
182
+ if (typeof window === 'undefined')
183
+ return;
184
+ const w = window;
185
+ w.botonic = Object.assign(Object.assign({}, w.botonic), publicWebchatController);
186
+ }
187
+ /**
188
+ * Signal that webchat is fully ready (connection established).
189
+ * Call this after setupGlobalWebchatAPI when the connection is confirmed.
190
+ */
191
+ export function signalWebchatReady() {
192
+ publicWebchatController.signalReady();
193
+ }
194
+ //# sourceMappingURL=public.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.js","sourceRoot":"","sources":["../../../../../../../libs/botonic/webchat-core/src/lib/controllers/public.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,uBAAuB;IASlC;QARQ,eAAU,GAA6B,IAAI,CAAA;QAC3C,kBAAa,GAAG,KAAK,CAAA;QACrB,iBAAY,GAAG,KAAK,CAAA;QACpB,iBAAY,GAAwB,IAAI,CAAA;QA0ChD,gBAAW,GAAG,CAAC,OAAuB,EAAE,EAAE,CACxC,IAAI,CAAC,aAAa,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;QAC3C,SAAI,GAAG,GAAG,EAAE;YACV,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAC1C,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAA;YACxB,CAAC;iBAAM,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;gBACzC,uEAAuE;gBACvE,MAAM,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,cAAc,CAAC,CAAC,CAAA;YACvD,CAAC;QACH,CAAC,CAAA;QACD,UAAK,GAAG,GAAG,EAAE;YACX,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,UAAU;gBAAE,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAA;QACpE,CAAC,CAAA;QACD,WAAM,GAAG,GAAG,EAAE;YACZ,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAC1C,IAAI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