@getnexorai/sdk 0.1.2 → 0.1.4

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.
@@ -0,0 +1,2483 @@
1
+ // src/chat/config.ts
2
+ function applyDefaults(o) {
3
+ return {
4
+ workflowId: o.workflowId,
5
+ title: o.title ?? "Chat with us",
6
+ subtitle: o.subtitle ?? "We typically reply in a few minutes",
7
+ openingMessage: o.openingMessage,
8
+ greeting: o.greeting ?? "Hi there! \u{1F44B} How can we help?",
9
+ errorText: o.errorText ?? "Sorry, something went wrong. Please try again in a moment.",
10
+ disabledText: o.disabledText ?? "Chat isn't available right now. Please reach us on another channel.",
11
+ locale: o.locale ?? (typeof navigator !== "undefined" ? navigator.language : void 0),
12
+ accentColor: o.accentColor ?? "#111827",
13
+ accentTextColor: o.accentTextColor ?? "#ffffff",
14
+ showBranding: o.showBranding ?? true,
15
+ inputPlaceholder: o.inputPlaceholder ?? "Type a message\u2026",
16
+ showRestart: o.showRestart ?? true,
17
+ restartLabel: o.restartLabel ?? "Start over",
18
+ position: o.position ?? "bottom-right",
19
+ openOnLoad: o.openOnLoad ?? false,
20
+ channels: resolveChannels(o.channels),
21
+ requestContact: resolveRequestContact(o.channels?.requestContact),
22
+ debounceMs: o.debounceMs ?? 700,
23
+ splitReplies: o.splitReplies ?? true,
24
+ splitDelayMs: o.splitDelayMs ?? 0,
25
+ // 0 = auto (random 1000–1500ms per bubble)
26
+ requestTimeoutMs: o.requestTimeoutMs ?? 6e4,
27
+ container: o.container,
28
+ systemPrompt: o.systemPrompt,
29
+ clientPrompt: o.clientPrompt,
30
+ lead: o.lead,
31
+ capture: {
32
+ fields: o.capture?.fields ?? ["first_name", "email"],
33
+ mode: o.capture?.mode ?? "before",
34
+ createLeadOnSubmit: o.capture?.createLeadOnSubmit ?? false,
35
+ label: o.capture?.label ?? "Tell us a bit about you to get started:",
36
+ submitLabel: o.capture?.submitLabel ?? "Start chat",
37
+ placeholders: o.capture?.placeholders ?? {},
38
+ phoneCountryCode: o.capture?.phoneCountryCode ?? "",
39
+ phoneCountries: o.capture?.phoneCountries ?? [],
40
+ errorMessages: {
41
+ required: o.capture?.errorMessages?.required ?? "Please complete this field.",
42
+ email: o.capture?.errorMessages?.email ?? "Enter a valid email address.",
43
+ phone: o.capture?.errorMessages?.phone ?? "Enter a valid phone number, including the country code."
44
+ }
45
+ },
46
+ metadata: o.metadata,
47
+ resumeText: o.resumeText ?? "Continuing your conversation",
48
+ resumeWithNameText: o.resumeWithNameText ?? "Continuing as {name}",
49
+ startOverText: o.startOverText ?? "Start over",
50
+ onOpen: o.onOpen,
51
+ onClose: o.onClose,
52
+ onMessage: o.onMessage,
53
+ onLeadCaptured: o.onLeadCaptured,
54
+ onError: o.onError
55
+ };
56
+ }
57
+ var DEFAULT_CHANNEL_ORDER = [
58
+ "whatsapp",
59
+ "call",
60
+ "email",
61
+ "instagram",
62
+ "chat"
63
+ ];
64
+ var DEFAULT_CHANNEL_LABELS = {
65
+ whatsapp: "WhatsApp",
66
+ call: "Call",
67
+ email: "Email",
68
+ instagram: "Instagram",
69
+ chat: "Chat"
70
+ };
71
+ function resolveChannels(c) {
72
+ if (!c) return [];
73
+ const order = c.order ?? DEFAULT_CHANNEL_ORDER;
74
+ const label = (k) => c.labels?.[k] ?? DEFAULT_CHANNEL_LABELS[k];
75
+ const contactChannels = new Set(
76
+ c.requestContact ? c.requestContact.channels ?? ["call", "email"] : []
77
+ );
78
+ const contactBtn = (key) => ({
79
+ key,
80
+ href: null,
81
+ external: false,
82
+ action: "contact",
83
+ label: label(key)
84
+ });
85
+ const seen = /* @__PURE__ */ new Set();
86
+ const out = [];
87
+ for (const key of order) {
88
+ if (seen.has(key)) continue;
89
+ seen.add(key);
90
+ switch (key) {
91
+ case "whatsapp": {
92
+ if (contactChannels.has("whatsapp")) {
93
+ out.push(contactBtn(key));
94
+ break;
95
+ }
96
+ if (!c.whatsapp) break;
97
+ const digits = c.whatsapp.replace(/[^\d]/g, "");
98
+ if (!digits) break;
99
+ const qs = c.whatsappText ? `?text=${encodeURIComponent(c.whatsappText)}` : "";
100
+ out.push({ key, href: `https://wa.me/${digits}${qs}`, external: true, label: label(key) });
101
+ break;
102
+ }
103
+ case "call": {
104
+ if (contactChannels.has("call")) {
105
+ out.push(contactBtn(key));
106
+ break;
107
+ }
108
+ if (!c.call) break;
109
+ const tel = c.call.replace(/[^\d+]/g, "");
110
+ if (!tel) break;
111
+ out.push({ key, href: `tel:${tel}`, external: false, label: label(key) });
112
+ break;
113
+ }
114
+ case "email": {
115
+ if (contactChannels.has("email")) {
116
+ out.push(contactBtn(key));
117
+ break;
118
+ }
119
+ if (!c.email) break;
120
+ out.push({ key, href: `mailto:${c.email}`, external: false, label: label(key) });
121
+ break;
122
+ }
123
+ case "instagram": {
124
+ if (!c.instagram) break;
125
+ const href = /^https?:\/\//i.test(c.instagram) ? c.instagram : `https://instagram.com/${c.instagram.replace(/^@/, "")}`;
126
+ out.push({ key, href, external: true, label: label(key) });
127
+ break;
128
+ }
129
+ case "chat": {
130
+ if (c.chat === false) break;
131
+ out.push({ key, href: null, external: false, label: label(key) });
132
+ break;
133
+ }
134
+ }
135
+ }
136
+ if (out.every((ch) => ch.key === "chat")) return [];
137
+ return out;
138
+ }
139
+ var DEFAULT_CONTACT_TITLE = {
140
+ call: "Request a call",
141
+ email: "Request an email",
142
+ whatsapp: "Request a message"
143
+ };
144
+ var DEFAULT_CONTACT_SUBTITLE = {
145
+ call: "Leave your details and we'll call you. We need this to reach you.",
146
+ email: "Leave your details and we'll email you. We need this to reach you.",
147
+ whatsapp: "Leave your details and we'll message you. We need this to reach you."
148
+ };
149
+ var DEFAULT_CONTACT_SUBMIT = {
150
+ call: "Request call",
151
+ email: "Request email",
152
+ whatsapp: "Request message"
153
+ };
154
+ var DEFAULT_CONTACT_SUCCESS = {
155
+ call: "Thanks! We'll call you shortly.",
156
+ email: "Thanks! We'll email you shortly.",
157
+ whatsapp: "Thanks! We'll message you shortly."
158
+ };
159
+ function resolveRequestContact(rc) {
160
+ if (!rc) return void 0;
161
+ const channels = rc.channels && rc.channels.length ? rc.channels : ["call", "email"];
162
+ const pick = (over, def) => {
163
+ const out = {};
164
+ for (const ch of channels) out[ch] = over?.[ch] ?? def[ch];
165
+ return out;
166
+ };
167
+ return {
168
+ channels,
169
+ title: pick(rc.title, DEFAULT_CONTACT_TITLE),
170
+ subtitle: pick(rc.subtitle, DEFAULT_CONTACT_SUBTITLE),
171
+ submitLabel: pick(rc.submitLabel, DEFAULT_CONTACT_SUBMIT),
172
+ successText: pick(rc.successText, DEFAULT_CONTACT_SUCCESS),
173
+ doneLabel: rc.doneLabel ?? "Done",
174
+ errorText: rc.errorText ?? "Something went wrong. Please try again."
175
+ };
176
+ }
177
+ function needsCapture(cfg) {
178
+ if (cfg.capture.mode === "skip") return false;
179
+ if (cfg.lead) {
180
+ const have = cfg.lead;
181
+ return cfg.capture.fields.some((f) => !have[f]);
182
+ }
183
+ return true;
184
+ }
185
+
186
+ // src/chat/session.ts
187
+ var SESSION_KEY = "nexor.chat.session_id";
188
+ var SESSION_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
189
+ function ensureSessionId() {
190
+ const now = Date.now();
191
+ try {
192
+ const rec = readRecord();
193
+ if (rec && now - rec.ts < SESSION_RETENTION_MS) {
194
+ writeRecord(rec.id, now);
195
+ return rec.id;
196
+ }
197
+ } catch {
198
+ }
199
+ const id = `sess_${randomHex(16)}`;
200
+ try {
201
+ writeRecord(id, now);
202
+ } catch {
203
+ }
204
+ return id;
205
+ }
206
+ function touchSession() {
207
+ try {
208
+ const rec = readRecord();
209
+ if (rec) writeRecord(rec.id, Date.now());
210
+ } catch {
211
+ }
212
+ }
213
+ function readRecord() {
214
+ const raw = window.localStorage.getItem(SESSION_KEY);
215
+ if (!raw) return null;
216
+ try {
217
+ const o = JSON.parse(raw);
218
+ if (o && typeof o.id === "string" && typeof o.ts === "number") return o;
219
+ } catch {
220
+ }
221
+ if (raw.startsWith("sess_")) return { id: raw, ts: Date.now() };
222
+ return null;
223
+ }
224
+ function writeRecord(id, ts) {
225
+ window.localStorage.setItem(SESSION_KEY, JSON.stringify({ id, ts }));
226
+ }
227
+ function randomHex(bytes) {
228
+ const arr = new Uint8Array(bytes);
229
+ if (typeof crypto !== "undefined" && crypto.getRandomValues) {
230
+ crypto.getRandomValues(arr);
231
+ } else {
232
+ for (let i = 0; i < bytes; i++) arr[i] = Math.floor(Math.random() * 256);
233
+ }
234
+ return Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
235
+ }
236
+
237
+ // src/chat/validate.ts
238
+ function isValidEmail(value) {
239
+ const s = (value || "").trim();
240
+ if (s.length < 6 || s.length > 254) return false;
241
+ return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(s);
242
+ }
243
+
244
+ // src/chat/template.ts
245
+ var ALIASES = {
246
+ first_name: "first_name",
247
+ name: "first_name",
248
+ lead_first_name: "first_name",
249
+ last_name: "last_name",
250
+ lastname: "last_name",
251
+ lead_last_name: "last_name",
252
+ full_name: "full_name",
253
+ fullname: "full_name",
254
+ email: "email",
255
+ phone: "phone"
256
+ };
257
+ function clean(v) {
258
+ return v == null ? "" : String(v).trim();
259
+ }
260
+ function leadValue(lead, canonical) {
261
+ if (!lead) return "";
262
+ switch (canonical) {
263
+ case "first_name":
264
+ return clean(lead.first_name);
265
+ case "last_name":
266
+ return clean(lead.last_name);
267
+ case "full_name":
268
+ return [clean(lead.first_name), clean(lead.last_name)].filter(Boolean).join(" ");
269
+ case "email":
270
+ return clean(lead.email);
271
+ case "phone":
272
+ return clean(lead.phone);
273
+ default:
274
+ return "";
275
+ }
276
+ }
277
+ function renderLeadTemplate(template, lead) {
278
+ if (typeof template !== "string" || !template) return "";
279
+ const replaced = template.replace(/\{\{\s*([a-zA-Z_]+)\s*\}\}/g, (match, raw) => {
280
+ const canonical = ALIASES[raw.toLowerCase()];
281
+ if (!canonical) return match;
282
+ return leadValue(lead, canonical);
283
+ });
284
+ return replaced.replace(/[ \t]+([,.!?;:])/g, "$1").replace(/[ \t]{2,}/g, " ").replace(/^[ \t]*[,;:][ \t]*/gm, "").replace(/[ \t]+$/gm, "").trim();
285
+ }
286
+
287
+ // src/chat/countries.ts
288
+ var FALLBACK_COUNTRY = {
289
+ iso: "CL",
290
+ name: "Chile",
291
+ dial: "+56",
292
+ flag: "\u{1F1E8}\u{1F1F1}",
293
+ nsn: [9, 9]
294
+ };
295
+ var COUNTRIES = [
296
+ FALLBACK_COUNTRY,
297
+ { iso: "MX", name: "M\xE9xico", dial: "+52", flag: "\u{1F1F2}\u{1F1FD}", nsn: [10, 10] },
298
+ { iso: "AR", name: "Argentina", dial: "+54", flag: "\u{1F1E6}\u{1F1F7}", nsn: [10, 11] },
299
+ { iso: "CO", name: "Colombia", dial: "+57", flag: "\u{1F1E8}\u{1F1F4}", nsn: [10, 10] },
300
+ { iso: "PE", name: "Per\xFA", dial: "+51", flag: "\u{1F1F5}\u{1F1EA}", nsn: [9, 9] },
301
+ { iso: "BR", name: "Brasil", dial: "+55", flag: "\u{1F1E7}\u{1F1F7}", nsn: [10, 11] },
302
+ { iso: "EC", name: "Ecuador", dial: "+593", flag: "\u{1F1EA}\u{1F1E8}", nsn: [8, 9] },
303
+ { iso: "UY", name: "Uruguay", dial: "+598", flag: "\u{1F1FA}\u{1F1FE}", nsn: [8, 8] },
304
+ { iso: "US", name: "United States", dial: "+1", flag: "\u{1F1FA}\u{1F1F8}", nsn: [10, 10] },
305
+ { iso: "ES", name: "Espa\xF1a", dial: "+34", flag: "\u{1F1EA}\u{1F1F8}", nsn: [9, 9] },
306
+ { iso: "GB", name: "United Kingdom", dial: "+44", flag: "\u{1F1EC}\u{1F1E7}", nsn: [10, 10] }
307
+ ];
308
+ function maxNsn(c) {
309
+ return c.nsn[1];
310
+ }
311
+ function countryByIso(iso) {
312
+ if (!iso) return void 0;
313
+ const up = iso.toUpperCase();
314
+ return COUNTRIES.find((c) => c.iso === up);
315
+ }
316
+ function countryByDial(dial) {
317
+ if (!dial) return void 0;
318
+ const norm = dial.startsWith("+") ? dial : `+${dial.replace(/^\+?/, "")}`;
319
+ return COUNTRIES.find((c) => c.dial === norm);
320
+ }
321
+ function resolveDefaultCountry(codeOrIso) {
322
+ const v = (codeOrIso || "").trim();
323
+ return countryByDial(v) || countryByIso(v) || FALLBACK_COUNTRY;
324
+ }
325
+
326
+ // src/chat/styles.ts
327
+ var widgetCss = (accent, accentText) => `
328
+ .nexor-chat,
329
+ .nexor-chat * {
330
+ box-sizing: border-box;
331
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
332
+ }
333
+
334
+ .nexor-chat {
335
+ position: fixed;
336
+ z-index: 2147483640;
337
+ bottom: 20px;
338
+ right: 20px;
339
+ font-size: 14px;
340
+ line-height: 1.4;
341
+ color: #111;
342
+ }
343
+
344
+ .nexor-chat[data-position="bottom-left"] {
345
+ right: auto;
346
+ left: 20px;
347
+ }
348
+
349
+ /* \u2500\u2500 Launcher wrapper (anchors the optional speed-dial fan) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
350
+ .nexor-chat__launcher-wrap {
351
+ position: relative;
352
+ display: inline-flex;
353
+ }
354
+
355
+ /* \u2500\u2500 Launcher bubble \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
356
+ .nexor-chat__launcher {
357
+ width: 56px;
358
+ height: 56px;
359
+ border-radius: 9999px;
360
+ background: ${accent};
361
+ color: ${accentText};
362
+ border: none;
363
+ cursor: pointer;
364
+ display: flex;
365
+ align-items: center;
366
+ justify-content: center;
367
+ box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
368
+ transition: transform 160ms cubic-bezier(.2,.7,.2,1.1),
369
+ box-shadow 160ms ease,
370
+ background 200ms ease;
371
+ padding: 0;
372
+ position: relative;
373
+ animation: nexor-launcher-in 360ms cubic-bezier(.2,.7,.2,1.1) both;
374
+ }
375
+ .nexor-chat__launcher:hover {
376
+ transform: translateY(-2px) scale(1.04);
377
+ box-shadow: 0 10px 24px rgba(0, 0, 0, 0.24);
378
+ }
379
+ .nexor-chat__launcher:active {
380
+ transform: translateY(0) scale(0.97);
381
+ transition-duration: 80ms;
382
+ }
383
+ .nexor-chat__launcher:focus-visible {
384
+ outline: 2px solid ${accent};
385
+ outline-offset: 2px;
386
+ }
387
+ .nexor-chat[data-open="true"] .nexor-chat__launcher {
388
+ transform: scale(0.85);
389
+ opacity: 0.75;
390
+ }
391
+ /* While the chat panel is open the launcher is dormant: no hover fan, no
392
+ clicks. The visitor closes the chat (header \u2715) to reach the channels again.
393
+ pointer-events:none also stops the CSS :hover that would reveal the fan. */
394
+ .nexor-chat[data-open="true"] .nexor-chat__launcher-wrap {
395
+ pointer-events: none;
396
+ }
397
+ .nexor-chat__launcher svg { width: 24px; height: 24px; transition: transform 200ms ease; }
398
+ .nexor-chat[data-open="true"] .nexor-chat__launcher svg { transform: rotate(8deg); }
399
+
400
+ .nexor-chat__unread {
401
+ position: absolute;
402
+ top: -4px;
403
+ right: -4px;
404
+ min-width: 18px;
405
+ height: 18px;
406
+ background: #ef4444;
407
+ color: #fff;
408
+ border-radius: 9999px;
409
+ font-size: 11px;
410
+ font-weight: 600;
411
+ padding: 0 5px;
412
+ display: flex;
413
+ align-items: center;
414
+ justify-content: center;
415
+ border: 2px solid #fff;
416
+ animation: nexor-pop-in 300ms cubic-bezier(.2,.7,.2,1.1) both;
417
+ }
418
+
419
+ @keyframes nexor-launcher-in {
420
+ from { transform: translateY(20px) scale(0.6); opacity: 0; }
421
+ to { transform: translateY(0) scale(1); opacity: 1; }
422
+ }
423
+ @keyframes nexor-pop-in {
424
+ from { transform: scale(0); }
425
+ to { transform: scale(1); }
426
+ }
427
+
428
+ /* \u2500\u2500 Speed-dial channel fan \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
429
+ /* Sits above the launcher, out of normal flow so it never shifts the bubble.
430
+ Hidden until the wrapper is hovered (pointer devices) or tapped open
431
+ (data-expanded, set by JS \u2014 the only path on touch). */
432
+ .nexor-chat__channels {
433
+ position: absolute;
434
+ /* Anchor the fan's box to the top of the launcher and push the buttons up
435
+ with padding instead of a gap. This keeps the launcher + fan a single,
436
+ continuous hover surface \u2014 moving the cursor from the bubble up to the
437
+ buttons never crosses dead space, so the drawer no longer snaps shut. */
438
+ bottom: 100%;
439
+ right: 0;
440
+ padding-bottom: 14px;
441
+ display: flex;
442
+ flex-direction: column;
443
+ gap: 12px;
444
+ align-items: flex-end;
445
+ opacity: 0;
446
+ visibility: hidden;
447
+ pointer-events: none;
448
+ transform: translateY(10px);
449
+ transition: opacity 180ms ease, transform 180ms ease, visibility 180ms;
450
+ }
451
+ .nexor-chat[data-position="bottom-left"] .nexor-chat__channels {
452
+ right: auto;
453
+ left: 0;
454
+ align-items: flex-start;
455
+ }
456
+
457
+ @media (hover: hover) and (pointer: fine) {
458
+ .nexor-chat__launcher-wrap:hover .nexor-chat__channels {
459
+ opacity: 1;
460
+ visibility: visible;
461
+ pointer-events: auto;
462
+ transform: translateY(0);
463
+ }
464
+ }
465
+ .nexor-chat__launcher-wrap[data-expanded="true"] .nexor-chat__channels {
466
+ opacity: 1;
467
+ visibility: visible;
468
+ pointer-events: auto;
469
+ transform: translateY(0);
470
+ }
471
+
472
+ /* Subtle rise-and-stagger as each item appears (nearest the launcher first). */
473
+ .nexor-chat__channel-item {
474
+ display: flex;
475
+ align-items: center;
476
+ gap: 10px;
477
+ transform: translateY(8px);
478
+ opacity: 0;
479
+ transition: transform 200ms cubic-bezier(.2,.7,.2,1.1), opacity 200ms ease;
480
+ }
481
+ .nexor-chat[data-position="bottom-left"] .nexor-chat__channel-item {
482
+ flex-direction: row-reverse;
483
+ }
484
+ .nexor-chat__launcher-wrap[data-expanded="true"] .nexor-chat__channel-item,
485
+ .nexor-chat__launcher-wrap:hover .nexor-chat__channel-item {
486
+ transform: translateY(0);
487
+ opacity: 1;
488
+ }
489
+ .nexor-chat__channel-item:nth-last-child(1) { transition-delay: 0ms; }
490
+ .nexor-chat__channel-item:nth-last-child(2) { transition-delay: 40ms; }
491
+ .nexor-chat__channel-item:nth-last-child(3) { transition-delay: 80ms; }
492
+ .nexor-chat__channel-item:nth-last-child(4) { transition-delay: 120ms; }
493
+ .nexor-chat__channel-item:nth-last-child(5) { transition-delay: 160ms; }
494
+
495
+ /* The round mini-button for each channel. */
496
+ .nexor-chat__channel {
497
+ width: 46px;
498
+ height: 46px;
499
+ border-radius: 9999px;
500
+ border: none;
501
+ cursor: pointer;
502
+ display: flex;
503
+ align-items: center;
504
+ justify-content: center;
505
+ padding: 0;
506
+ background: ${accent};
507
+ color: ${accentText};
508
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
509
+ text-decoration: none;
510
+ transition: transform 140ms cubic-bezier(.2,.7,.2,1.1), box-shadow 140ms ease, filter 140ms ease;
511
+ }
512
+ .nexor-chat__channel:hover {
513
+ transform: scale(1.08);
514
+ box-shadow: 0 8px 18px rgba(0, 0, 0, 0.26);
515
+ filter: brightness(1.05);
516
+ }
517
+ .nexor-chat__channel:active { transform: scale(0.94); }
518
+ .nexor-chat__channel:focus-visible { outline: 2px solid ${accent}; outline-offset: 2px; }
519
+ .nexor-chat__channel svg { width: 24px; height: 24px; display: block; }
520
+
521
+ /* Brand colours win over the accent where a channel has a recognisable hue. */
522
+ .nexor-chat__channel--whatsapp { background: #25d366; color: #fff; }
523
+ .nexor-chat__channel--instagram {
524
+ background: radial-gradient(circle at 30% 110%, #fdf497 0%, #fd5949 45%, #d6249f 70%, #285aeb 100%);
525
+ color: #fff;
526
+ }
527
+
528
+ /* Label pill beside each button. */
529
+ .nexor-chat__channel-label {
530
+ background: #fff;
531
+ color: #111;
532
+ font-size: 12px;
533
+ font-weight: 600;
534
+ padding: 5px 10px;
535
+ border-radius: 8px;
536
+ white-space: nowrap;
537
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.16);
538
+ }
539
+
540
+ /* \u2500\u2500 Panel \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
541
+ .nexor-chat__panel {
542
+ position: absolute;
543
+ bottom: 72px;
544
+ right: 0;
545
+ width: 360px;
546
+ max-width: calc(100vw - 32px);
547
+ height: 540px;
548
+ max-height: calc(100vh - 120px);
549
+ background: #fff;
550
+ border-radius: 14px;
551
+ box-shadow: 0 24px 60px rgba(0, 0, 0, 0.22);
552
+ display: flex;
553
+ flex-direction: column;
554
+ overflow: hidden;
555
+ transform: translateY(14px) scale(0.96);
556
+ opacity: 0;
557
+ pointer-events: none;
558
+ transition: transform 220ms cubic-bezier(.2,.7,.2,1.1),
559
+ opacity 220ms ease;
560
+ border: 1px solid rgba(0, 0, 0, 0.06);
561
+ /* Use visibility so screen readers don't read collapsed content */
562
+ visibility: hidden;
563
+ }
564
+ .nexor-chat[data-position="bottom-left"] .nexor-chat__panel {
565
+ right: auto;
566
+ left: 0;
567
+ }
568
+ .nexor-chat[data-open="true"] .nexor-chat__panel {
569
+ transform: translateY(0) scale(1);
570
+ opacity: 1;
571
+ pointer-events: auto;
572
+ visibility: visible;
573
+ }
574
+
575
+ .nexor-chat__header {
576
+ background: ${accent};
577
+ color: ${accentText};
578
+ padding: 14px 16px;
579
+ display: flex;
580
+ align-items: center;
581
+ justify-content: space-between;
582
+ gap: 12px;
583
+ }
584
+ .nexor-chat__title {
585
+ font-weight: 600;
586
+ font-size: 15px;
587
+ margin: 0;
588
+ }
589
+ .nexor-chat__subtitle {
590
+ font-size: 12px;
591
+ opacity: 0.85;
592
+ margin: 2px 0 0;
593
+ line-height: 1.4;
594
+ }
595
+ .nexor-chat__status-dot {
596
+ display: inline-block;
597
+ width: 8px;
598
+ height: 8px;
599
+ border-radius: 9999px;
600
+ background: #34d399;
601
+ margin-right: 6px;
602
+ vertical-align: 1px;
603
+ animation: nexor-pulse 2.4s ease-in-out infinite;
604
+ box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.6);
605
+ }
606
+ @keyframes nexor-pulse {
607
+ 0%, 100% { box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.55); }
608
+ 50% { box-shadow: 0 0 0 6px rgba(52, 211, 153, 0); }
609
+ }
610
+ .nexor-chat__close {
611
+ background: transparent;
612
+ border: none;
613
+ color: inherit;
614
+ cursor: pointer;
615
+ padding: 4px;
616
+ border-radius: 6px;
617
+ opacity: 0.9;
618
+ transition: background 140ms ease, transform 140ms ease;
619
+ }
620
+ .nexor-chat__close:hover { background: rgba(255,255,255,0.18); opacity: 1; }
621
+ .nexor-chat__close:active { transform: scale(0.92); }
622
+ .nexor-chat__close svg { width: 18px; height: 18px; display: block; }
623
+ /* Keep the title left and group the action buttons on the right. */
624
+ .nexor-chat__header > div { margin-right: auto; min-width: 0; }
625
+ .nexor-chat__restart {
626
+ background: transparent;
627
+ border: none;
628
+ color: inherit;
629
+ cursor: pointer;
630
+ padding: 4px;
631
+ border-radius: 6px;
632
+ opacity: 0.7;
633
+ transition: background 140ms ease, transform 200ms ease, opacity 140ms ease;
634
+ }
635
+ .nexor-chat__restart:hover { background: rgba(255,255,255,0.18); opacity: 1; }
636
+ .nexor-chat__restart:active { transform: rotate(-90deg) scale(0.9); }
637
+ .nexor-chat__restart svg { width: 17px; height: 17px; display: block; }
638
+ /* Nothing to reset on the capture screen \u2014 hide it there. */
639
+ .nexor-chat__panel[data-view="capture"] .nexor-chat__restart { display: none; }
640
+
641
+ /* \u2500\u2500 Body / messages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
642
+ .nexor-chat__body {
643
+ flex: 1;
644
+ overflow-y: auto;
645
+ padding: 14px;
646
+ background: #f6f7f9;
647
+ display: flex;
648
+ flex-direction: column;
649
+ gap: 8px;
650
+ scroll-behavior: smooth;
651
+ /* Prevent layout shift when scrollbar appears */
652
+ scrollbar-gutter: stable;
653
+ }
654
+ .nexor-chat__body::-webkit-scrollbar { width: 6px; }
655
+ .nexor-chat__body::-webkit-scrollbar-thumb {
656
+ background: rgba(0,0,0,0.15);
657
+ border-radius: 9999px;
658
+ }
659
+
660
+ .nexor-chat__msg {
661
+ max-width: 80%;
662
+ padding: 8px 12px;
663
+ border-radius: 14px;
664
+ white-space: pre-wrap;
665
+ word-wrap: break-word;
666
+ animation: nexor-msg-in 240ms cubic-bezier(.2,.7,.2,1.1) both;
667
+ will-change: transform, opacity;
668
+ }
669
+ .nexor-chat__msg--bot {
670
+ background: #fff;
671
+ align-self: flex-start;
672
+ border: 1px solid rgba(0,0,0,0.06);
673
+ border-bottom-left-radius: 4px;
674
+ box-shadow: 0 1px 2px rgba(0,0,0,0.04);
675
+ }
676
+ .nexor-chat__msg--user {
677
+ background: ${accent};
678
+ color: ${accentText};
679
+ align-self: flex-end;
680
+ border-bottom-right-radius: 4px;
681
+ animation-name: nexor-msg-in-user;
682
+ }
683
+ .nexor-chat__msg--system {
684
+ background: transparent;
685
+ color: #6b7280;
686
+ font-size: 12px;
687
+ align-self: center;
688
+ text-align: center;
689
+ max-width: 100%;
690
+ animation-name: nexor-fade-in;
691
+ }
692
+ .nexor-chat__msg-time {
693
+ display: block;
694
+ margin-top: 4px;
695
+ font-size: 10px;
696
+ line-height: 1;
697
+ opacity: 0.55;
698
+ text-align: right;
699
+ }
700
+ .nexor-chat__msg--bot .nexor-chat__msg-time { text-align: left; }
701
+
702
+ @keyframes nexor-msg-in {
703
+ from { transform: translateY(8px) scale(0.96); opacity: 0; }
704
+ to { transform: translateY(0) scale(1); opacity: 1; }
705
+ }
706
+ @keyframes nexor-msg-in-user {
707
+ from { transform: translateY(8px) translateX(8px) scale(0.96); opacity: 0; }
708
+ to { transform: translateY(0) translateX(0) scale(1); opacity: 1; }
709
+ }
710
+ @keyframes nexor-fade-in {
711
+ from { opacity: 0; }
712
+ to { opacity: 1; }
713
+ }
714
+
715
+ .nexor-chat__typing {
716
+ align-self: flex-start;
717
+ background: #fff;
718
+ border: 1px solid rgba(0,0,0,0.06);
719
+ border-radius: 14px;
720
+ border-bottom-left-radius: 4px;
721
+ padding: 10px 14px;
722
+ display: inline-flex;
723
+ gap: 4px;
724
+ animation: nexor-msg-in 240ms cubic-bezier(.2,.7,.2,1.1) both;
725
+ }
726
+ .nexor-chat__typing span {
727
+ width: 6px; height: 6px; border-radius: 9999px;
728
+ background: #9ca3af;
729
+ display: inline-block;
730
+ animation: nexor-blink 1.2s infinite ease-in-out;
731
+ }
732
+ .nexor-chat__typing span:nth-child(2) { animation-delay: 0.15s; }
733
+ .nexor-chat__typing span:nth-child(3) { animation-delay: 0.30s; }
734
+ @keyframes nexor-blink {
735
+ 0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
736
+ 30% { opacity: 1; transform: translateY(-3px); }
737
+ }
738
+
739
+ /* \u2500\u2500 Resume banner (returning visitor) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
740
+ .nexor-chat__resume {
741
+ display: flex;
742
+ align-items: center;
743
+ justify-content: space-between;
744
+ gap: 10px;
745
+ background: #fff;
746
+ border: 1px solid rgba(0,0,0,0.06);
747
+ border-radius: 10px;
748
+ padding: 8px 12px;
749
+ margin-bottom: 4px;
750
+ box-shadow: 0 1px 2px rgba(0,0,0,0.04);
751
+ animation: nexor-fade-in 240ms ease both;
752
+ }
753
+ .nexor-chat__resume-text {
754
+ font-size: 12px;
755
+ color: #6b7280;
756
+ min-width: 0;
757
+ overflow: hidden;
758
+ text-overflow: ellipsis;
759
+ white-space: nowrap;
760
+ }
761
+ .nexor-chat__resume-reset {
762
+ flex: none;
763
+ background: transparent;
764
+ border: none;
765
+ color: ${accent};
766
+ font: inherit;
767
+ font-size: 12px;
768
+ font-weight: 600;
769
+ cursor: pointer;
770
+ padding: 2px 4px;
771
+ border-radius: 6px;
772
+ text-decoration: underline;
773
+ text-underline-offset: 2px;
774
+ transition: opacity 140ms ease;
775
+ }
776
+ .nexor-chat__resume-reset:hover { opacity: 0.7; }
777
+ .nexor-chat__resume-reset:active { opacity: 0.5; }
778
+
779
+ /* \u2500\u2500 Capture as its own full-panel view \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
780
+ /* The form is a distinct screen, not a card wedged into the message list.
781
+ data-view swaps between the capture screen and the chat (body + composer). */
782
+ .nexor-chat__capture-view {
783
+ min-height: 0;
784
+ overflow-y: auto;
785
+ padding: 16px;
786
+ background: #f6f7f9;
787
+ display: flex;
788
+ flex-direction: column;
789
+ animation: nexor-fade-in 200ms ease both;
790
+ }
791
+ /* In the form view the panel hugs the form \u2014 no fixed 540px height leaving
792
+ empty gaps above and below the card. It still can't exceed max-height. */
793
+ .nexor-chat__panel[data-view="capture"] {
794
+ height: auto;
795
+ }
796
+ .nexor-chat__panel[data-view="chat"] .nexor-chat__capture-view { display: none; }
797
+ .nexor-chat__panel[data-view="capture"] .nexor-chat__body,
798
+ .nexor-chat__panel[data-view="capture"] .nexor-chat__composer { display: none; }
799
+ .nexor-chat__capture-view .nexor-chat__capture-label {
800
+ font-size: 13px;
801
+ font-weight: 600;
802
+ color: #374151;
803
+ }
804
+
805
+ /* \u2500\u2500 Lead capture form \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
806
+ .nexor-chat__capture {
807
+ background: #fff;
808
+ border: 1px solid rgba(0,0,0,0.06);
809
+ border-radius: 12px;
810
+ padding: 12px;
811
+ display: flex;
812
+ flex-direction: column;
813
+ gap: 8px;
814
+ margin-top: 4px;
815
+ animation: nexor-msg-in 280ms cubic-bezier(.2,.7,.2,1.1) both;
816
+ box-shadow: 0 2px 8px rgba(0,0,0,0.05);
817
+ }
818
+ .nexor-chat__capture-label {
819
+ font-size: 12px;
820
+ color: #6b7280;
821
+ margin: 0;
822
+ }
823
+ .nexor-chat__capture-row {
824
+ display: flex;
825
+ gap: 8px;
826
+ }
827
+ .nexor-chat__capture-row > * { flex: 1; }
828
+ .nexor-chat__input,
829
+ .nexor-chat__capture input {
830
+ font: inherit;
831
+ padding: 8px 10px;
832
+ border: 1px solid #d1d5db;
833
+ border-radius: 8px;
834
+ background: #fff;
835
+ color: #111;
836
+ outline: none;
837
+ width: 100%;
838
+ transition: border-color 140ms ease, box-shadow 140ms ease;
839
+ }
840
+ /* Composer textarea: auto-grows (height managed in JS) up to ~4 lines, then
841
+ scrolls. resize:none disables the manual drag handle. */
842
+ textarea.nexor-chat__input {
843
+ display: block;
844
+ resize: none;
845
+ overflow-y: auto;
846
+ line-height: 20px;
847
+ max-height: 98px; /* 4 lines (20\xD74) + 16 padding + 2 border */
848
+ white-space: pre-wrap;
849
+ word-break: break-word;
850
+ }
851
+ .nexor-chat__input:focus,
852
+ .nexor-chat__capture input:focus {
853
+ border-color: ${accent};
854
+ box-shadow: 0 0 0 3px ${accent}33;
855
+ }
856
+ .nexor-chat__input--error,
857
+ .nexor-chat__capture input.nexor-chat__input--error {
858
+ border-color: #ef4444;
859
+ box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.16);
860
+ }
861
+ .nexor-chat__capture-error {
862
+ font-size: 12px;
863
+ color: #ef4444;
864
+ margin: 0;
865
+ }
866
+ .nexor-chat__capture-error[hidden] { display: none; }
867
+
868
+ /* \u2500\u2500 Phone field (flag + dial code selector joined to a digit input) \u2500\u2500 */
869
+ .nexor-chat__phone {
870
+ display: flex;
871
+ align-items: stretch;
872
+ width: 100%;
873
+ border: 1px solid #d1d5db;
874
+ border-radius: 8px;
875
+ background: #fff;
876
+ overflow: hidden;
877
+ transition: border-color 140ms ease, box-shadow 140ms ease;
878
+ }
879
+ .nexor-chat__phone:focus-within {
880
+ border-color: ${accent};
881
+ box-shadow: 0 0 0 3px ${accent}33;
882
+ }
883
+ .nexor-chat__phone.nexor-chat__input--error {
884
+ border-color: #ef4444;
885
+ box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.16);
886
+ }
887
+ .nexor-chat__phone-country {
888
+ flex: none;
889
+ border: none;
890
+ border-right: 1px solid #e5e7eb;
891
+ background: #f9fafb;
892
+ color: #111;
893
+ font: inherit;
894
+ padding: 0 12px 0 10px;
895
+ cursor: pointer;
896
+ outline: none;
897
+ }
898
+ .nexor-chat__phone-number.nexor-chat__input {
899
+ border: none;
900
+ border-radius: 0;
901
+ box-shadow: none;
902
+ flex: 1;
903
+ min-width: 0;
904
+ }
905
+
906
+ .nexor-chat__capture-submit {
907
+ background: ${accent};
908
+ color: ${accentText};
909
+ border: none;
910
+ border-radius: 8px;
911
+ padding: 8px 12px;
912
+ font: inherit;
913
+ font-weight: 600;
914
+ cursor: pointer;
915
+ transition: transform 140ms ease, filter 140ms ease;
916
+ }
917
+ .nexor-chat__capture-submit:hover { filter: brightness(1.08); }
918
+ .nexor-chat__capture-submit:active { transform: scale(0.97); }
919
+ .nexor-chat__capture-submit[disabled] { opacity: 0.6; cursor: not-allowed; }
920
+
921
+ /* \u2500\u2500 Contact-request view (call me / email me) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
922
+ /* Its own full-panel screen, opened from a "call"/"email" fan button. Reuses
923
+ the capture form styling (.nexor-chat__capture) for the fields. */
924
+ .nexor-chat__contact-view {
925
+ min-height: 0;
926
+ overflow-y: auto;
927
+ padding: 16px;
928
+ background: #f6f7f9;
929
+ display: flex;
930
+ flex-direction: column;
931
+ animation: nexor-fade-in 200ms ease both;
932
+ }
933
+ /* Show the contact view only in its own data-view; hug the form (no fixed
934
+ height leaving gaps), but never exceed the panel's max-height. */
935
+ .nexor-chat__panel[data-view="contact"] { height: auto; }
936
+ .nexor-chat__panel:not([data-view="contact"]) .nexor-chat__contact-view { display: none; }
937
+ .nexor-chat__panel[data-view="contact"] .nexor-chat__capture-view,
938
+ .nexor-chat__panel[data-view="contact"] .nexor-chat__body,
939
+ .nexor-chat__panel[data-view="contact"] .nexor-chat__composer { display: none; }
940
+ .nexor-chat__panel[data-view="contact"] .nexor-chat__restart { display: none; }
941
+
942
+ /* The contact form lives under the header, which carries its title/explainer;
943
+ the live-status dot ("we're listening") makes no sense for a form. */
944
+ .nexor-chat__panel[data-view="contact"] .nexor-chat__status-dot { display: none; }
945
+
946
+ /* form \u2194 success swap, driven by the view's data-state */
947
+ .nexor-chat__contact-view[data-state="sent"] .nexor-chat__contact-form { display: none; }
948
+ .nexor-chat__contact-view:not([data-state="sent"]) .nexor-chat__contact-success { display: none; }
949
+
950
+ .nexor-chat__contact-success {
951
+ display: flex;
952
+ flex-direction: column;
953
+ align-items: center;
954
+ text-align: center;
955
+ gap: 12px;
956
+ padding: 28px 12px 12px;
957
+ animation: nexor-msg-in 280ms cubic-bezier(.2,.7,.2,1.1) both;
958
+ }
959
+ .nexor-chat__contact-success-icon {
960
+ width: 48px;
961
+ height: 48px;
962
+ display: flex;
963
+ align-items: center;
964
+ justify-content: center;
965
+ border-radius: 50%;
966
+ background: ${accent};
967
+ color: ${accentText};
968
+ }
969
+ .nexor-chat__contact-success-icon svg { width: 26px; height: 26px; }
970
+ .nexor-chat__contact-success-text {
971
+ font-size: 14px;
972
+ color: #374151;
973
+ margin: 0;
974
+ line-height: 1.5;
975
+ }
976
+ .nexor-chat__contact-success .nexor-chat__capture-submit {
977
+ margin-top: 4px;
978
+ align-self: stretch;
979
+ }
980
+
981
+ /* \u2500\u2500 Composer \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
982
+ .nexor-chat__composer {
983
+ display: flex;
984
+ gap: 8px;
985
+ padding: 10px;
986
+ background: #fff;
987
+ border-top: 1px solid rgba(0,0,0,0.06);
988
+ /* Pin the send button to the bottom so it stays put as the textarea grows. */
989
+ align-items: flex-end;
990
+ }
991
+ .nexor-chat__send {
992
+ background: ${accent};
993
+ color: ${accentText};
994
+ border: none;
995
+ border-radius: 8px;
996
+ padding: 0 14px;
997
+ font: inherit;
998
+ font-weight: 600;
999
+ cursor: pointer;
1000
+ min-width: 44px;
1001
+ height: 38px; /* matches the one-line textarea height */
1002
+ flex: none;
1003
+ display: flex;
1004
+ align-items: center;
1005
+ justify-content: center;
1006
+ transition: transform 140ms ease, filter 140ms ease;
1007
+ }
1008
+ .nexor-chat__send:hover { filter: brightness(1.08); }
1009
+ .nexor-chat__send:active { transform: scale(0.95); }
1010
+ .nexor-chat__send[disabled] {
1011
+ opacity: 0.6;
1012
+ cursor: not-allowed;
1013
+ transform: none;
1014
+ }
1015
+ .nexor-chat__send svg {
1016
+ width: 18px;
1017
+ height: 18px;
1018
+ transition: transform 200ms ease;
1019
+ }
1020
+ .nexor-chat__send:not([disabled]):hover svg { transform: translateX(2px); }
1021
+
1022
+ /* Small spinner inside the send button while a turn is in flight */
1023
+ .nexor-chat__send--loading svg { display: none; }
1024
+ .nexor-chat__send--loading::after {
1025
+ content: "";
1026
+ width: 14px;
1027
+ height: 14px;
1028
+ border-radius: 9999px;
1029
+ border: 2px solid currentColor;
1030
+ border-top-color: transparent;
1031
+ animation: nexor-spin 700ms linear infinite;
1032
+ }
1033
+ @keyframes nexor-spin {
1034
+ to { transform: rotate(360deg); }
1035
+ }
1036
+
1037
+ .nexor-chat__footer {
1038
+ display: flex;
1039
+ align-items: center;
1040
+ justify-content: center;
1041
+ gap: 5px;
1042
+ font-size: 11px;
1043
+ color: #9ca3af;
1044
+ padding: 6px;
1045
+ background: #fff;
1046
+ border-top: 1px solid rgba(0,0,0,0.04);
1047
+ }
1048
+ .nexor-chat__footer a {
1049
+ color: inherit;
1050
+ text-decoration: none;
1051
+ font-weight: 600;
1052
+ }
1053
+ /* "Powered by" + the Nexor wordmark (same mark as the site navbar). Plain
1054
+ attribution \u2014 not a link, but hovering shows a getnexor.ai tooltip. */
1055
+ .nexor-chat__brand {
1056
+ position: relative;
1057
+ display: inline-flex;
1058
+ align-items: center;
1059
+ cursor: help; /* hover reveals the getnexor.ai tooltip; not a link */
1060
+ }
1061
+ .nexor-chat__brand-logo {
1062
+ height: 13px;
1063
+ width: auto;
1064
+ display: block;
1065
+ }
1066
+ /* Custom tooltip (instant + reliable across browsers, unlike a native SVG
1067
+ <title>). Sits above the logo and fades in on hover/focus. */
1068
+ .nexor-chat__brand-tip {
1069
+ position: absolute;
1070
+ bottom: calc(100% + 7px);
1071
+ left: 50%;
1072
+ transform: translateX(-50%) translateY(4px);
1073
+ background: #111827;
1074
+ color: #fff;
1075
+ font-size: 11px;
1076
+ font-weight: 500;
1077
+ line-height: 1;
1078
+ letter-spacing: 0.01em;
1079
+ padding: 5px 8px;
1080
+ border-radius: 6px;
1081
+ white-space: nowrap;
1082
+ opacity: 0;
1083
+ pointer-events: none;
1084
+ box-shadow: 0 4px 12px rgba(0,0,0,0.18);
1085
+ transition: opacity 140ms ease, transform 140ms ease;
1086
+ z-index: 5;
1087
+ }
1088
+ .nexor-chat__brand-tip::after {
1089
+ content: "";
1090
+ position: absolute;
1091
+ top: 100%;
1092
+ left: 50%;
1093
+ transform: translateX(-50%);
1094
+ border: 4px solid transparent;
1095
+ border-top-color: #111827;
1096
+ }
1097
+ .nexor-chat__brand:hover .nexor-chat__brand-tip {
1098
+ opacity: 1;
1099
+ transform: translateX(-50%) translateY(0);
1100
+ }
1101
+
1102
+ /* \u2500\u2500 Mobile: full-screen panel with an obvious close affordance \u2500\u2500\u2500\u2500\u2500 */
1103
+ @media (max-width: 480px) {
1104
+ /* The panel takes over the whole viewport so the keyboard + messages have
1105
+ room. 100dvh tracks the dynamic viewport as mobile browser chrome shows/
1106
+ hides, avoiding a cut-off composer. */
1107
+ .nexor-chat__panel {
1108
+ position: fixed;
1109
+ inset: 0;
1110
+ top: 0;
1111
+ left: 0;
1112
+ right: 0;
1113
+ bottom: 0;
1114
+ width: 100%;
1115
+ max-width: 100%;
1116
+ height: 100vh;
1117
+ height: 100dvh;
1118
+ max-height: none;
1119
+ border-radius: 0;
1120
+ border: none;
1121
+ transform: translateY(100%);
1122
+ }
1123
+ .nexor-chat[data-position="bottom-left"] .nexor-chat__panel { left: 0; }
1124
+ .nexor-chat[data-open="true"] .nexor-chat__panel { transform: translateY(0); }
1125
+ /* Keep the form view full-screen on phones too (overrides the desktop
1126
+ hug-the-form height). */
1127
+ .nexor-chat__panel[data-view="capture"] {
1128
+ height: 100vh;
1129
+ height: 100dvh;
1130
+ }
1131
+ .nexor-chat__panel[data-view="capture"] .nexor-chat__capture-view {
1132
+ justify-content: center;
1133
+ }
1134
+
1135
+ /* Hide the launcher (and any open fan) while the full-screen panel is up \u2014
1136
+ the in-header close button is the way back out. */
1137
+ .nexor-chat[data-open="true"] .nexor-chat__launcher-wrap {
1138
+ opacity: 0;
1139
+ pointer-events: none;
1140
+ }
1141
+
1142
+ /* Roomier header + a bigger, unmistakable close target on touch. */
1143
+ .nexor-chat__header {
1144
+ padding: 16px 14px 16px 18px;
1145
+ padding-top: max(16px, env(safe-area-inset-top));
1146
+ }
1147
+ .nexor-chat__close {
1148
+ padding: 10px;
1149
+ background: rgba(255, 255, 255, 0.16);
1150
+ }
1151
+ .nexor-chat__close svg { width: 24px; height: 24px; }
1152
+
1153
+ /* Keep the composer clear of the home-indicator / gesture bar. */
1154
+ .nexor-chat__composer {
1155
+ padding-bottom: max(10px, env(safe-area-inset-bottom));
1156
+ }
1157
+ }
1158
+
1159
+ /* Respect users who request reduced motion. */
1160
+ @media (prefers-reduced-motion: reduce) {
1161
+ .nexor-chat,
1162
+ .nexor-chat * {
1163
+ animation-duration: 0.001ms !important;
1164
+ transition-duration: 0.001ms !important;
1165
+ }
1166
+ .nexor-chat__status-dot { animation: none; }
1167
+ }
1168
+ `;
1169
+
1170
+ // src/chat/dom.ts
1171
+ var STYLE_ATTR = "data-nexor-chat-styles";
1172
+ function injectStyles(accent, accentText) {
1173
+ if (document.querySelector(`style[${STYLE_ATTR}]`)) return;
1174
+ const style = document.createElement("style");
1175
+ style.setAttribute(STYLE_ATTR, "1");
1176
+ style.textContent = widgetCss(accent, accentText);
1177
+ document.head.appendChild(style);
1178
+ }
1179
+ var CHAT_ICON = `
1180
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1181
+ <path d="M4 5.5C4 4.67 4.67 4 5.5 4h13c.83 0 1.5.67 1.5 1.5v9c0 .83-.67 1.5-1.5 1.5H9l-4 4V5.5Z"
1182
+ stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
1183
+ </svg>`;
1184
+ var CHANNEL_ICONS = {
1185
+ whatsapp: `
1186
+ <svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1187
+ <path d="M.057 24l1.687-6.163a11.867 11.867 0 0 1-1.587-5.946C.16 5.335 5.495 0 12.05 0a11.817 11.817 0 0 1 8.413 3.488 11.824 11.824 0 0 1 3.48 8.414c-.003 6.557-5.338 11.892-11.893 11.892a11.9 11.9 0 0 1-5.688-1.448L.057 24zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884a9.82 9.82 0 0 0 1.5 5.245l-.999 3.648 3.738-.981zm11.387-5.464c-.074-.124-.272-.198-.57-.347-.297-.149-1.758-.868-2.031-.967-.272-.099-.47-.149-.669.149-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.462-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.297-.347.446-.521.151-.172.2-.296.3-.495.099-.198.05-.372-.025-.521-.075-.148-.669-1.611-.916-2.206-.242-.579-.487-.501-.669-.51l-.57-.01c-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.263.489 1.694.626.712.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.248-.695.248-1.29.173-1.414z"/>
1188
+ </svg>`,
1189
+ call: `
1190
+ <svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1191
+ <path d="M6.6 10.8c1.4 2.8 3.8 5.1 6.6 6.6l2.2-2.2c.3-.3.7-.4 1-.2 1.1.4 2.3.6 3.5.6.6 0 1 .4 1 1V20c0 .6-.4 1-1 1C10.5 21 3 13.5 3 4.5c0-.6.4-1 1-1h3.5c.6 0 1 .4 1 1 0 1.2.2 2.4.6 3.5.1.4 0 .8-.2 1L6.6 10.8Z"/>
1192
+ </svg>`,
1193
+ email: `
1194
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1195
+ <rect x="3" y="5" width="18" height="14" rx="2" stroke="currentColor" stroke-width="1.8"/>
1196
+ <path d="m4 7 8 6 8-6" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
1197
+ </svg>`,
1198
+ instagram: `
1199
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1200
+ <rect x="3.5" y="3.5" width="17" height="17" rx="5" stroke="currentColor" stroke-width="1.8"/>
1201
+ <circle cx="12" cy="12" r="4" stroke="currentColor" stroke-width="1.8"/>
1202
+ <circle cx="17.2" cy="6.8" r="1.2" fill="currentColor"/>
1203
+ </svg>`,
1204
+ chat: CHAT_ICON
1205
+ };
1206
+ function buildLauncher(cfg) {
1207
+ const wrap = document.createElement("div");
1208
+ wrap.className = "nexor-chat__launcher-wrap";
1209
+ const hasFan = cfg.channels.length > 0;
1210
+ const { fan, channelButtons } = hasFan ? buildChannelFan(cfg.channels) : { fan: null, channelButtons: [] };
1211
+ if (fan) wrap.appendChild(fan);
1212
+ const el = document.createElement("button");
1213
+ el.type = "button";
1214
+ el.className = "nexor-chat__launcher";
1215
+ el.setAttribute("aria-label", hasFan ? "Contact us" : "Open chat");
1216
+ if (hasFan) {
1217
+ el.setAttribute("aria-haspopup", "true");
1218
+ el.setAttribute("aria-expanded", "false");
1219
+ }
1220
+ el.innerHTML = CHAT_ICON;
1221
+ const badge = document.createElement("span");
1222
+ badge.className = "nexor-chat__unread";
1223
+ badge.style.display = "none";
1224
+ el.appendChild(badge);
1225
+ wrap.appendChild(el);
1226
+ return { wrap, el, badge, fan, channelButtons };
1227
+ }
1228
+ function buildChannelFan(channels) {
1229
+ const fan = document.createElement("div");
1230
+ fan.className = "nexor-chat__channels";
1231
+ fan.setAttribute("role", "menu");
1232
+ const channelButtons = [];
1233
+ for (const ch of channels) {
1234
+ const item = document.createElement("div");
1235
+ item.className = "nexor-chat__channel-item";
1236
+ const labelEl = document.createElement("span");
1237
+ labelEl.className = "nexor-chat__channel-label";
1238
+ labelEl.textContent = ch.label;
1239
+ let el;
1240
+ if (ch.href === null) {
1241
+ const btn = document.createElement("button");
1242
+ btn.type = "button";
1243
+ el = btn;
1244
+ } else {
1245
+ const a = document.createElement("a");
1246
+ a.href = ch.href;
1247
+ if (ch.external) {
1248
+ a.target = "_blank";
1249
+ a.rel = "noopener noreferrer";
1250
+ }
1251
+ el = a;
1252
+ }
1253
+ el.className = `nexor-chat__channel nexor-chat__channel--${ch.key}`;
1254
+ el.setAttribute("role", "menuitem");
1255
+ el.setAttribute("aria-label", ch.label);
1256
+ el.innerHTML = CHANNEL_ICONS[ch.key];
1257
+ item.appendChild(labelEl);
1258
+ item.appendChild(el);
1259
+ fan.appendChild(item);
1260
+ channelButtons.push({ key: ch.key, el, action: ch.action });
1261
+ }
1262
+ return { fan, channelButtons };
1263
+ }
1264
+ function buildPanel(cfg) {
1265
+ const el = document.createElement("div");
1266
+ el.className = "nexor-chat__panel";
1267
+ el.setAttribute("role", "dialog");
1268
+ el.setAttribute("aria-label", cfg.title);
1269
+ const { header, closeBtn, restartBtn, titleEl, subtitleEl, subtitleTextEl } = buildHeader(cfg);
1270
+ const body = buildBody();
1271
+ const { form, input, send } = buildComposer(cfg);
1272
+ const footer = cfg.showBranding ? buildFooter() : null;
1273
+ const captureInputs = {};
1274
+ let captureView = null;
1275
+ let captureForm = null;
1276
+ let captureError = null;
1277
+ let phoneCountry = null;
1278
+ if (needsCapture(cfg)) {
1279
+ const built = buildCaptureView(cfg, captureInputs);
1280
+ captureView = built.view;
1281
+ captureForm = built.form;
1282
+ captureError = built.error;
1283
+ phoneCountry = built.phoneCountry;
1284
+ }
1285
+ const contact = cfg.requestContact ? buildContactView(cfg) : null;
1286
+ el.setAttribute("data-view", captureView ? "capture" : "chat");
1287
+ el.appendChild(header);
1288
+ if (captureView) el.appendChild(captureView);
1289
+ if (contact) el.appendChild(contact.view);
1290
+ el.appendChild(body);
1291
+ el.appendChild(form);
1292
+ if (footer) el.appendChild(footer);
1293
+ return {
1294
+ el,
1295
+ closeBtn,
1296
+ restartBtn,
1297
+ headerTitle: titleEl,
1298
+ headerSubtitle: subtitleEl,
1299
+ headerSubtitleText: subtitleTextEl,
1300
+ body,
1301
+ form,
1302
+ input,
1303
+ send,
1304
+ captureView,
1305
+ captureForm,
1306
+ captureError,
1307
+ phoneCountry,
1308
+ captureInputs,
1309
+ contact
1310
+ };
1311
+ }
1312
+ function buildHeader(cfg) {
1313
+ const header = document.createElement("div");
1314
+ header.className = "nexor-chat__header";
1315
+ const info = document.createElement("div");
1316
+ const titleEl = document.createElement("p");
1317
+ titleEl.className = "nexor-chat__title";
1318
+ titleEl.textContent = cfg.title;
1319
+ info.appendChild(titleEl);
1320
+ const subtitleEl = document.createElement("p");
1321
+ subtitleEl.className = "nexor-chat__subtitle";
1322
+ const dot = document.createElement("span");
1323
+ dot.className = "nexor-chat__status-dot";
1324
+ dot.setAttribute("aria-hidden", "true");
1325
+ const subtitleTextEl = document.createElement("span");
1326
+ subtitleTextEl.className = "nexor-chat__subtitle-text";
1327
+ subtitleTextEl.textContent = cfg.subtitle ?? "";
1328
+ subtitleEl.appendChild(dot);
1329
+ subtitleEl.appendChild(subtitleTextEl);
1330
+ if (!cfg.subtitle) subtitleEl.hidden = true;
1331
+ info.appendChild(subtitleEl);
1332
+ header.appendChild(info);
1333
+ let restartBtn = null;
1334
+ if (cfg.showRestart) {
1335
+ restartBtn = document.createElement("button");
1336
+ restartBtn.type = "button";
1337
+ restartBtn.className = "nexor-chat__restart";
1338
+ restartBtn.setAttribute("aria-label", cfg.restartLabel);
1339
+ restartBtn.title = cfg.restartLabel;
1340
+ restartBtn.innerHTML = `
1341
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1342
+ <path d="M4 5v5h5M20 19v-5h-5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
1343
+ <path d="M19 10a7 7 0 0 0-12-3L4 10M5 14a7 7 0 0 0 12 3l3-3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
1344
+ </svg>
1345
+ `;
1346
+ header.appendChild(restartBtn);
1347
+ }
1348
+ const closeBtn = document.createElement("button");
1349
+ closeBtn.type = "button";
1350
+ closeBtn.className = "nexor-chat__close";
1351
+ closeBtn.setAttribute("aria-label", "Close chat");
1352
+ closeBtn.innerHTML = `
1353
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1354
+ <path d="M6 6l12 12M6 18L18 6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
1355
+ </svg>
1356
+ `;
1357
+ header.appendChild(closeBtn);
1358
+ return { header, closeBtn, restartBtn, titleEl, subtitleEl, subtitleTextEl };
1359
+ }
1360
+ function buildBody() {
1361
+ const body = document.createElement("div");
1362
+ body.className = "nexor-chat__body";
1363
+ return body;
1364
+ }
1365
+ function buildCaptureView(cfg, inputsOut) {
1366
+ const view = document.createElement("div");
1367
+ view.className = "nexor-chat__capture-view";
1368
+ const { form, error, phoneCountry } = buildCaptureForm(cfg, inputsOut);
1369
+ view.appendChild(form);
1370
+ return { view, form, error, phoneCountry };
1371
+ }
1372
+ var DEFAULT_FIELD_PLACEHOLDERS = {
1373
+ first_name: "First name",
1374
+ last_name: "Last name",
1375
+ email: "Email",
1376
+ phone: "Phone"
1377
+ };
1378
+ function buildCaptureForm(cfg, inputsOut) {
1379
+ const form = document.createElement("form");
1380
+ form.className = "nexor-chat__capture";
1381
+ form.noValidate = true;
1382
+ let phoneCountry = null;
1383
+ const label = document.createElement("p");
1384
+ label.className = "nexor-chat__capture-label";
1385
+ label.textContent = cfg.capture.label;
1386
+ form.appendChild(label);
1387
+ const fields = cfg.capture.fields;
1388
+ const ph = (f) => cfg.capture.placeholders[f] ?? DEFAULT_FIELD_PLACEHOLDERS[f];
1389
+ if (fields.includes("first_name") || fields.includes("last_name")) {
1390
+ const row = document.createElement("div");
1391
+ row.className = "nexor-chat__capture-row";
1392
+ if (fields.includes("first_name")) {
1393
+ inputsOut.first_name = makeInput("first_name", ph("first_name"), "text", cfg.lead?.first_name);
1394
+ row.appendChild(inputsOut.first_name);
1395
+ }
1396
+ if (fields.includes("last_name")) {
1397
+ inputsOut.last_name = makeInput("last_name", ph("last_name"), "text", cfg.lead?.last_name);
1398
+ row.appendChild(inputsOut.last_name);
1399
+ }
1400
+ if (row.childElementCount > 0) form.appendChild(row);
1401
+ }
1402
+ if (fields.includes("email")) {
1403
+ inputsOut.email = makeInput("email", ph("email"), "email", cfg.lead?.email);
1404
+ form.appendChild(inputsOut.email);
1405
+ }
1406
+ if (fields.includes("phone")) {
1407
+ const built = buildPhoneField(cfg, ph("phone"));
1408
+ inputsOut.phone = built.input;
1409
+ phoneCountry = built.select;
1410
+ form.appendChild(built.wrap);
1411
+ }
1412
+ const error = document.createElement("p");
1413
+ error.className = "nexor-chat__capture-error";
1414
+ error.setAttribute("role", "alert");
1415
+ error.hidden = true;
1416
+ form.appendChild(error);
1417
+ const submit = document.createElement("button");
1418
+ submit.type = "submit";
1419
+ submit.className = "nexor-chat__capture-submit";
1420
+ submit.textContent = cfg.capture.submitLabel;
1421
+ form.appendChild(submit);
1422
+ return { form, error, phoneCountry };
1423
+ }
1424
+ function resolveCountryList(isos) {
1425
+ if (!isos.length) return COUNTRIES;
1426
+ const picked = isos.map((i) => countryByIso(i)).filter((c) => Boolean(c));
1427
+ return picked.length ? picked : COUNTRIES;
1428
+ }
1429
+ function buildPhoneField(cfg, placeholder) {
1430
+ const wrap = document.createElement("div");
1431
+ wrap.className = "nexor-chat__phone";
1432
+ const list = resolveCountryList(cfg.capture.phoneCountries);
1433
+ const def = resolveDefaultCountry(cfg.capture.phoneCountryCode);
1434
+ const select = document.createElement("select");
1435
+ select.className = "nexor-chat__phone-country";
1436
+ select.name = "phone_country";
1437
+ select.setAttribute("aria-label", "Country code");
1438
+ for (const c of list) {
1439
+ const opt = document.createElement("option");
1440
+ opt.value = c.iso;
1441
+ opt.textContent = `${c.flag} ${c.dial}`;
1442
+ if (c.iso === def.iso) opt.selected = true;
1443
+ select.appendChild(opt);
1444
+ }
1445
+ const input = document.createElement("input");
1446
+ input.name = "phone";
1447
+ input.type = "tel";
1448
+ input.inputMode = "numeric";
1449
+ input.autocomplete = "tel-national";
1450
+ input.placeholder = placeholder;
1451
+ input.className = "nexor-chat__input nexor-chat__phone-number";
1452
+ input.maxLength = maxNsn(def);
1453
+ if (cfg.lead?.phone) {
1454
+ const digits = String(cfg.lead.phone).replace(/\D/g, "");
1455
+ const dialDigits = def.dial.replace(/\D/g, "");
1456
+ const national = digits.startsWith(dialDigits) ? digits.slice(dialDigits.length) : digits;
1457
+ input.value = national.slice(0, input.maxLength);
1458
+ }
1459
+ wrap.appendChild(select);
1460
+ wrap.appendChild(input);
1461
+ return { wrap, select, input };
1462
+ }
1463
+ function buildContactView(cfg) {
1464
+ const rc = cfg.requestContact;
1465
+ const view = document.createElement("div");
1466
+ view.className = "nexor-chat__contact-view";
1467
+ view.setAttribute("data-state", "form");
1468
+ const form = document.createElement("form");
1469
+ form.className = "nexor-chat__capture nexor-chat__contact-form";
1470
+ form.noValidate = true;
1471
+ const inputs = {};
1472
+ const ph = (f) => cfg.capture.placeholders[f] ?? DEFAULT_FIELD_PLACEHOLDERS[f];
1473
+ const row = document.createElement("div");
1474
+ row.className = "nexor-chat__capture-row";
1475
+ inputs.first_name = makeInput("first_name", ph("first_name"), "text", cfg.lead?.first_name);
1476
+ inputs.last_name = makeInput("last_name", ph("last_name"), "text", cfg.lead?.last_name);
1477
+ row.appendChild(inputs.first_name);
1478
+ row.appendChild(inputs.last_name);
1479
+ form.appendChild(row);
1480
+ inputs.email = makeInput("email", ph("email"), "email", cfg.lead?.email);
1481
+ form.appendChild(inputs.email);
1482
+ const phone = buildPhoneField(cfg, ph("phone"));
1483
+ inputs.phone = phone.input;
1484
+ const phoneCountry = phone.select;
1485
+ form.appendChild(phone.wrap);
1486
+ const error = document.createElement("p");
1487
+ error.className = "nexor-chat__capture-error";
1488
+ error.setAttribute("role", "alert");
1489
+ error.hidden = true;
1490
+ form.appendChild(error);
1491
+ const submitBtn = document.createElement("button");
1492
+ submitBtn.type = "submit";
1493
+ submitBtn.className = "nexor-chat__capture-submit";
1494
+ form.appendChild(submitBtn);
1495
+ view.appendChild(form);
1496
+ const success = document.createElement("div");
1497
+ success.className = "nexor-chat__contact-success";
1498
+ const successIcon = document.createElement("div");
1499
+ successIcon.className = "nexor-chat__contact-success-icon";
1500
+ successIcon.innerHTML = `
1501
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1502
+ <path d="M5 13l4 4L19 7" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/>
1503
+ </svg>`;
1504
+ const successText = document.createElement("p");
1505
+ successText.className = "nexor-chat__contact-success-text";
1506
+ const successBtn = document.createElement("button");
1507
+ successBtn.type = "button";
1508
+ successBtn.className = "nexor-chat__capture-submit";
1509
+ successBtn.textContent = rc?.doneLabel ?? "Done";
1510
+ success.appendChild(successIcon);
1511
+ success.appendChild(successText);
1512
+ success.appendChild(successBtn);
1513
+ view.appendChild(success);
1514
+ return {
1515
+ view,
1516
+ form,
1517
+ inputs,
1518
+ error,
1519
+ phoneCountry,
1520
+ submitBtn,
1521
+ success,
1522
+ successText,
1523
+ successBtn
1524
+ };
1525
+ }
1526
+ function buildComposer(cfg) {
1527
+ const form = document.createElement("form");
1528
+ form.className = "nexor-chat__composer";
1529
+ const input = document.createElement("textarea");
1530
+ input.className = "nexor-chat__input";
1531
+ input.placeholder = cfg.inputPlaceholder;
1532
+ input.rows = 1;
1533
+ input.setAttribute("autocomplete", "off");
1534
+ const send = document.createElement("button");
1535
+ send.type = "submit";
1536
+ send.className = "nexor-chat__send";
1537
+ send.setAttribute("aria-label", "Send message");
1538
+ send.innerHTML = `
1539
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
1540
+ <path d="M3.4 11.2 20.5 4.3c.7-.3 1.4.4 1.1 1.1l-6.9 17.1c-.3.8-1.4.8-1.8 0L10 14l-7.5-3c-.8-.3-.8-1.5 0-1.8Z"
1541
+ stroke="currentColor" stroke-width="1.6" stroke-linejoin="round" fill="currentColor"/>
1542
+ </svg>
1543
+ `;
1544
+ form.appendChild(input);
1545
+ form.appendChild(send);
1546
+ return { form, input, send };
1547
+ }
1548
+ var NEXOR_WORDMARK = `
1549
+ <svg viewBox="0 0 1722 505" fill="none" xmlns="http://www.w3.org/2000/svg" class="nexor-chat__brand-logo" role="img" aria-label="Nexor">
1550
+ <defs>
1551
+ <linearGradient id="nexorChatBrandGrad" x1="1195.44" y1="21.3423" x2="1195.44" y2="504.454" gradientUnits="userSpaceOnUse">
1552
+ <stop stop-color="#232522"/>
1553
+ <stop offset="1" stop-color="#A1A1A1"/>
1554
+ </linearGradient>
1555
+ </defs>
1556
+ <path d="M31.824 416.676V103.332H67.32L82.008 147.396C92.412 125.976 125.46 99.0477 196.452 99.0477C299.268 99.0477 319.464 159.024 319.464 247.764V416.676H254.592V242.256C254.592 186.564 244.8 153.516 178.092 153.516C111.384 153.516 97.308 185.952 97.308 242.256V416.676H31.824ZM506.373 420.96C380.301 420.96 349.089 343.236 349.089 259.392C349.089 175.548 380.913 99.0477 505.761 99.0477C621.429 99.0477 653.865 187.176 645.909 285.708H417.021C420.693 339.564 447.621 370.776 503.925 370.776C555.945 370.776 572.469 345.684 578.589 320.592H647.133C641.625 373.836 604.905 420.96 506.373 420.96ZM417.633 233.688H578.589V232.464C579.201 189.624 559.005 149.844 499.641 149.844C438.441 149.844 420.693 187.788 417.633 233.688ZM622.452 416.676L747.912 254.496L631.02 103.332H706.296L788.916 217.164L872.76 103.332H946.2L829.92 254.496L955.38 416.676H876.432L789.528 290.604L702.012 416.676H622.452Z" fill="#232522"/>
1557
+ <path d="M1195.22 21.3423C1199.01 21.3424 1202.55 23.2768 1205.81 26.6265C1209.06 29.9743 1212.14 34.8402 1215.03 40.9858C1220.81 53.2843 1225.98 70.9957 1230.31 92.7749C1236.49 123.842 1240.99 163.329 1243.06 207.497C1245.04 168.74 1249.06 134.088 1254.51 106.604C1258.48 86.5946 1263.23 70.315 1268.53 59.0063C1271.18 53.3557 1274 48.8741 1277 45.7866C1279.99 42.6974 1283.27 40.8979 1286.78 40.8979C1290.29 40.8981 1293.56 42.6975 1296.56 45.7866C1299.55 48.8741 1302.37 53.3556 1305.03 59.0063C1310.33 70.315 1315.07 86.5945 1319.04 106.604C1324.73 135.304 1328.87 171.821 1330.75 212.666C1332.39 186.451 1335.28 162.981 1339.08 144C1342.12 128.775 1345.76 116.368 1349.84 107.735C1351.88 103.422 1354.07 99.9788 1356.4 97.5962C1358.73 95.2117 1361.31 93.7866 1364.11 93.7866C1366.91 93.7866 1369.5 95.2116 1371.83 97.5962C1374.16 99.9788 1376.34 103.422 1378.38 107.735C1382.46 116.368 1386.1 128.775 1389.15 144C1393.72 166.881 1396.98 196.283 1398.32 229.154C1399.23 221.906 1400.42 215.317 1401.84 209.621C1403.54 202.788 1405.59 197.165 1407.92 193.217C1410.19 189.36 1412.99 186.675 1416.33 186.675C1419.68 186.676 1422.47 189.36 1424.75 193.217C1427.07 197.165 1429.12 202.788 1430.83 209.621C1434.24 223.306 1436.33 242.142 1436.33 262.898C1436.33 283.654 1434.24 302.489 1430.83 316.174C1429.12 323.007 1427.07 328.631 1424.75 332.579C1422.47 336.436 1419.68 339.119 1416.33 339.12C1412.99 339.12 1410.19 336.436 1407.92 332.579C1405.59 328.631 1403.54 323.007 1401.84 316.174C1400.42 310.478 1399.23 303.889 1398.32 296.641C1396.98 329.512 1393.72 358.915 1389.15 381.796C1386.1 397.021 1382.46 409.428 1378.38 418.061C1376.34 422.374 1374.16 425.816 1371.83 428.199C1369.5 430.583 1366.91 432.009 1364.11 432.009C1361.31 432.009 1358.73 430.584 1356.4 428.199C1354.07 425.816 1351.88 422.374 1349.84 418.061C1345.76 409.428 1342.12 397.021 1339.08 381.796C1335.28 362.814 1332.39 339.344 1330.75 313.129C1328.87 353.974 1324.73 390.492 1319.04 419.192C1315.07 439.201 1310.33 455.481 1305.03 466.79C1302.37 472.44 1299.55 476.922 1296.56 480.009C1293.56 483.098 1290.29 484.898 1286.78 484.898C1283.27 484.898 1279.99 483.098 1277 480.009C1274 476.922 1271.18 472.44 1268.53 466.79C1263.23 455.481 1258.48 439.201 1254.51 419.192C1249.06 391.708 1245.04 357.055 1243.06 318.298C1240.99 362.466 1236.49 401.953 1230.31 433.021C1225.98 454.8 1220.81 472.512 1215.03 484.81C1212.14 490.956 1209.06 495.822 1205.81 499.169C1202.55 502.519 1199.01 504.454 1195.22 504.454C1191.44 504.454 1187.89 502.519 1184.64 499.169C1181.38 495.822 1178.31 490.956 1175.42 484.81C1169.64 472.512 1164.46 454.8 1160.13 433.021C1153.95 401.956 1149.46 362.472 1147.39 318.308C1145.4 357.061 1141.38 391.71 1135.93 419.192C1131.96 439.202 1127.22 455.481 1121.91 466.79C1119.26 472.44 1116.44 476.922 1113.45 480.009C1110.45 483.098 1107.18 484.898 1103.67 484.898C1100.16 484.898 1096.88 483.098 1093.89 480.009C1090.89 476.922 1088.07 472.44 1085.42 466.79C1080.12 455.481 1075.37 439.202 1071.4 419.192C1065.71 390.491 1061.58 353.973 1059.69 313.128C1058.05 339.344 1055.16 362.814 1051.37 381.796C1048.32 397.021 1044.68 409.428 1040.6 418.061C1038.56 422.374 1036.38 425.816 1034.05 428.199C1031.72 430.583 1029.14 432.009 1026.33 432.009C1023.53 432.009 1020.95 430.584 1018.62 428.199C1016.29 425.816 1014.11 422.374 1012.07 418.061C1007.99 409.428 1004.34 397.021 1001.3 381.796C996.723 358.916 993.462 329.514 992.122 296.645C991.214 303.891 990.024 310.479 988.604 316.174C986.901 323.007 984.852 328.631 982.525 332.579C980.252 336.436 977.453 339.12 974.111 339.12C970.77 339.12 967.97 336.436 965.697 332.579C963.371 328.631 961.321 323.007 959.618 316.174C956.207 302.489 954.111 283.654 954.111 262.898C954.111 242.142 956.207 223.306 959.618 209.621C961.321 202.788 963.371 197.165 965.697 193.217C967.97 189.36 970.77 186.675 974.111 186.675C977.453 186.675 980.252 189.36 982.525 193.217C984.852 197.165 986.901 202.788 988.604 209.621C990.024 215.316 991.214 221.904 992.122 229.15C993.462 196.281 996.723 166.88 1001.3 144C1004.34 128.775 1007.99 116.368 1012.07 107.735C1014.11 103.422 1016.29 99.9787 1018.62 97.5962C1020.95 95.2115 1023.53 93.7866 1026.33 93.7866C1029.14 93.7868 1031.72 95.2116 1034.05 97.5962C1036.38 99.9787 1038.56 103.422 1040.6 107.735C1044.68 116.368 1048.32 128.775 1051.37 144C1055.16 162.981 1058.05 186.451 1059.69 212.666C1061.58 171.821 1065.71 135.304 1071.4 106.604C1075.37 86.5944 1080.12 70.315 1085.42 59.0063C1088.07 53.3558 1090.89 48.8741 1093.89 45.7866C1096.88 42.6974 1100.16 40.8979 1103.67 40.8979C1107.18 40.898 1110.45 42.6975 1113.45 45.7866C1116.44 48.8741 1119.26 53.3558 1121.91 59.0063C1127.22 70.315 1131.96 86.5945 1135.93 106.604C1141.38 134.086 1145.4 168.735 1147.39 207.487C1149.46 163.323 1153.95 123.84 1160.13 92.7749C1164.46 70.9957 1169.64 53.2843 1175.42 40.9858C1178.31 34.8403 1181.38 29.9743 1184.64 26.6265C1187.89 23.2769 1191.44 21.3423 1195.22 21.3423Z" fill="url(#nexorChatBrandGrad)"/>
1558
+ <path d="M1490.82 416.676V103.332H1525.71L1541.01 146.784C1559.98 106.392 1596.7 95.3758 1652.39 99.0477V155.964C1588.13 146.784 1556.31 177.996 1556.31 251.436V416.676H1490.82Z" fill="#232522"/>
1559
+ <path d="M1681.68 78.9118C1650.58 78.9118 1642.32 61.0558 1642.32 39.3598C1642.32 17.9518 1650.86 -0.000219345 1681.68 -0.000219345C1712.88 -0.000219345 1721.04 17.9518 1721.04 39.4558C1721.04 61.0558 1712.88 78.9118 1681.68 78.9118ZM1651.82 39.4558C1651.82 57.3118 1658.26 70.7518 1681.68 70.7518C1705.2 70.7518 1711.54 57.3118 1711.54 39.4558C1711.54 21.5998 1705.2 8.15978 1681.68 8.15978C1658.35 8.15978 1651.82 21.6958 1651.82 39.4558ZM1662.1 39.8398C1662.1 28.2238 1666.9 18.3358 1681.87 18.3358C1693.87 18.3358 1700.11 25.0558 1700.11 36.1918H1691.28C1690.7 29.2798 1687.82 26.3998 1681.78 26.3998C1674.1 26.3998 1671.02 31.2958 1671.02 39.5518C1671.02 47.9038 1674.1 52.8958 1681.78 52.8958C1687.82 52.8958 1690.8 50.0158 1691.28 43.3918H1700.11C1700.11 54.6238 1694.16 61.0558 1682.16 61.0558C1666.9 61.0558 1662.1 51.2638 1662.1 39.8398Z" fill="#232522"/>
1560
+ </svg>`;
1561
+ function buildFooter() {
1562
+ const el = document.createElement("div");
1563
+ el.className = "nexor-chat__footer";
1564
+ el.innerHTML = `<span>Powered by</span><span class="nexor-chat__brand" aria-label="Nexor"><span class="nexor-chat__brand-tip" role="tooltip">getnexor.ai</span>${NEXOR_WORDMARK}</span>`;
1565
+ return el;
1566
+ }
1567
+ function buildMessage(role, text, ts, locale) {
1568
+ const el = document.createElement("div");
1569
+ el.className = `nexor-chat__msg nexor-chat__msg--${role}`;
1570
+ const body = document.createElement("span");
1571
+ body.className = "nexor-chat__msg-text";
1572
+ body.textContent = text;
1573
+ el.appendChild(body);
1574
+ if (role !== "system" && ts) {
1575
+ const time = document.createElement("time");
1576
+ time.className = "nexor-chat__msg-time";
1577
+ time.dataset.ts = String(ts);
1578
+ time.textContent = formatRelativeTime(ts, locale);
1579
+ el.appendChild(time);
1580
+ }
1581
+ return el;
1582
+ }
1583
+ function formatRelativeTime(ts, locale, now = Date.now()) {
1584
+ try {
1585
+ const diff = Math.max(0, now - ts);
1586
+ const min = Math.round(diff / 6e4);
1587
+ const hr = Math.round(diff / 36e5);
1588
+ const day = Math.round(diff / 864e5);
1589
+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
1590
+ if (diff < 45e3) return rtf.format(0, "second");
1591
+ if (min < 60) return rtf.format(-min, "minute");
1592
+ if (hr < 24) return rtf.format(-hr, "hour");
1593
+ if (day < 7) return rtf.format(-day, "day");
1594
+ return new Date(ts).toLocaleDateString(locale, {
1595
+ day: "numeric",
1596
+ month: "short"
1597
+ });
1598
+ } catch {
1599
+ try {
1600
+ return new Date(ts).toLocaleTimeString(locale, {
1601
+ hour: "2-digit",
1602
+ minute: "2-digit"
1603
+ });
1604
+ } catch {
1605
+ return "";
1606
+ }
1607
+ }
1608
+ }
1609
+ function buildResumeBanner(text, resetLabel) {
1610
+ const el = document.createElement("div");
1611
+ el.className = "nexor-chat__resume";
1612
+ const label = document.createElement("span");
1613
+ label.className = "nexor-chat__resume-text";
1614
+ label.textContent = text;
1615
+ const resetBtn = document.createElement("button");
1616
+ resetBtn.type = "button";
1617
+ resetBtn.className = "nexor-chat__resume-reset";
1618
+ resetBtn.textContent = resetLabel;
1619
+ el.appendChild(label);
1620
+ el.appendChild(resetBtn);
1621
+ return { el, resetBtn };
1622
+ }
1623
+ function buildTypingIndicator() {
1624
+ const el = document.createElement("div");
1625
+ el.className = "nexor-chat__typing";
1626
+ el.innerHTML = "<span></span><span></span><span></span>";
1627
+ return el;
1628
+ }
1629
+ function makeInput(name, placeholder, type, value) {
1630
+ const i = document.createElement("input");
1631
+ i.name = name;
1632
+ i.type = type;
1633
+ i.placeholder = placeholder;
1634
+ if (value) i.value = value;
1635
+ return i;
1636
+ }
1637
+
1638
+ // src/chat/index.ts
1639
+ function initChat(client, options) {
1640
+ ensureBrowser();
1641
+ if (!options || !options.workflowId) {
1642
+ throw new Error("Nexor SDK: initChat() requires `workflowId`.");
1643
+ }
1644
+ const cfg = applyDefaults(options);
1645
+ injectStyles(cfg.accentColor, cfg.accentTextColor);
1646
+ let sessionId = ensureSessionId();
1647
+ let historyKey = `nexor.chat.history.${sessionId}`;
1648
+ const root = buildRoot(cfg);
1649
+ const launcher = buildLauncher(cfg);
1650
+ const panel = buildPanel(cfg);
1651
+ root.appendChild(launcher.wrap);
1652
+ root.appendChild(panel.el);
1653
+ (cfg.container ?? document.body).appendChild(root);
1654
+ const hasFan = launcher.channelButtons.length > 0;
1655
+ const state = {
1656
+ isOpen: false,
1657
+ expanded: false,
1658
+ // speed-dial fan visible (touch/click-pinned)
1659
+ unread: 0,
1660
+ leadId: void 0,
1661
+ captured: !needsCapture(cfg),
1662
+ pendingLead: { ...cfg.lead ?? {} },
1663
+ history: [],
1664
+ // Dashboard-authored web-chat config (fetched once on init). Until it
1665
+ // resolves, assume enabled and fall back to the host-provided `greeting`.
1666
+ webchatEnabled: true,
1667
+ serverInitialMessage: null,
1668
+ configResolved: false,
1669
+ disabledShown: false,
1670
+ // The opening message actually displayed to the visitor — sent to the API
1671
+ // so the agent's <webchat_opening> matches what was shown (esp. for a
1672
+ // per-page openingMessage override).
1673
+ shownOpening: null,
1674
+ // Set by "start over" → the next turn tells the API to reset the conversation
1675
+ // episode (keep the lead, clear its prior run/thread/variables). Cleared once
1676
+ // that turn lands.
1677
+ resetEpisode: false,
1678
+ // True when this page load started with NO restored transcript. Used to
1679
+ // decide whether to offer the "continue / start fresh" choice when the API
1680
+ // reports it recognised a returning lead (matched_existing).
1681
+ freshLocalSession: false,
1682
+ // Ensures the returning-lead choice banner is offered at most once.
1683
+ episodeChoiceOffered: false,
1684
+ // The channel a visitor picked in the contact-request form (call/email/...).
1685
+ contactChannel: null,
1686
+ // In-flight "create lead on form submit" request — the first chat turn waits
1687
+ // on it so the backend matches that lead (no duplicate) rather than racing it.
1688
+ leadCreating: null
1689
+ };
1690
+ let configReady;
1691
+ const send = {
1692
+ pending: [],
1693
+ timer: 0,
1694
+ inFlight: false,
1695
+ typing: null,
1696
+ splitTimer: 0
1697
+ // staggers multi-bubble bot replies
1698
+ };
1699
+ const greeting = { pending: false, timer: 0 };
1700
+ const GREETING_DELAY_MS = 2e3;
1701
+ launcher.el.addEventListener("click", hasFan ? toggleFan : toggle);
1702
+ panel.closeBtn.addEventListener("click", close);
1703
+ panel.restartBtn?.addEventListener("click", startOver);
1704
+ panel.form.addEventListener("submit", onComposerSubmit);
1705
+ panel.input.addEventListener("input", autosizeComposer);
1706
+ panel.input.addEventListener("keydown", (e) => {
1707
+ if (e.key === "Enter" && !e.shiftKey && !e.isComposing) {
1708
+ e.preventDefault();
1709
+ onComposerSubmit(e);
1710
+ }
1711
+ });
1712
+ panel.captureForm?.addEventListener("submit", onCaptureSubmit);
1713
+ for (const input of Object.values(panel.captureInputs)) {
1714
+ input?.addEventListener("input", clearCaptureError);
1715
+ }
1716
+ if (panel.phoneCountry && panel.captureInputs.phone) {
1717
+ wirePhoneCap(panel.phoneCountry, panel.captureInputs.phone, clearCaptureError);
1718
+ }
1719
+ if (panel.contact) {
1720
+ const c = panel.contact;
1721
+ c.form.addEventListener("submit", onContactSubmit);
1722
+ for (const input of Object.values(c.inputs)) {
1723
+ input?.addEventListener("input", clearContactError);
1724
+ }
1725
+ if (c.phoneCountry && c.inputs.phone) {
1726
+ wirePhoneCap(c.phoneCountry, c.inputs.phone, clearContactError);
1727
+ }
1728
+ c.successBtn.addEventListener("click", close);
1729
+ }
1730
+ let onDocPointerDown = null;
1731
+ if (hasFan) {
1732
+ for (const btn of launcher.channelButtons) {
1733
+ btn.el.addEventListener("click", () => {
1734
+ if (btn.key === "chat") {
1735
+ collapseFan();
1736
+ open();
1737
+ } else if (btn.action === "contact") {
1738
+ collapseFan();
1739
+ openContact(btn.key);
1740
+ } else {
1741
+ collapseFan();
1742
+ }
1743
+ });
1744
+ }
1745
+ onDocPointerDown = (e) => {
1746
+ if (state.expanded && !launcher.wrap.contains(e.target)) collapseFan();
1747
+ };
1748
+ document.addEventListener("pointerdown", onDocPointerDown);
1749
+ }
1750
+ let resumeBanner = null;
1751
+ const persisted = loadPersisted();
1752
+ state.freshLocalSession = persisted.history.length === 0;
1753
+ if (persisted.history.length) {
1754
+ state.history = persisted.history;
1755
+ if (persisted.lead) state.pendingLead = { ...state.pendingLead, ...persisted.lead };
1756
+ state.captured = true;
1757
+ setView("chat");
1758
+ renderResumeBanner();
1759
+ for (const m of persisted.history) {
1760
+ appendBubble(m.role === "user" ? "user" : "bot", m.text, m.ts);
1761
+ }
1762
+ scrollToBottom();
1763
+ }
1764
+ syncComposerLock();
1765
+ configReady = loadServerConfig();
1766
+ if (cfg.openOnLoad) open();
1767
+ function toggleFan() {
1768
+ state.expanded ? collapseFan() : expandFan();
1769
+ }
1770
+ function expandFan() {
1771
+ state.expanded = true;
1772
+ launcher.wrap.setAttribute("data-expanded", "true");
1773
+ launcher.el.setAttribute("aria-expanded", "true");
1774
+ }
1775
+ function collapseFan() {
1776
+ if (!state.expanded) return;
1777
+ state.expanded = false;
1778
+ launcher.wrap.setAttribute("data-expanded", "false");
1779
+ launcher.el.setAttribute("aria-expanded", "false");
1780
+ }
1781
+ function open() {
1782
+ collapseFan();
1783
+ if (state.isOpen) return;
1784
+ state.isOpen = true;
1785
+ root.setAttribute("data-open", "true");
1786
+ clearUnread();
1787
+ if (!state.webchatEnabled) {
1788
+ showDisabled();
1789
+ cfg.onOpen?.();
1790
+ return;
1791
+ }
1792
+ if (state.history.length === 0 && state.captured && canGreet()) {
1793
+ scheduleGreeting();
1794
+ }
1795
+ if (!state.captured) {
1796
+ const first = cfg.capture.fields[0];
1797
+ (first && panel.captureInputs[first] || panel.input).focus();
1798
+ } else {
1799
+ panel.input.focus();
1800
+ }
1801
+ cfg.onOpen?.();
1802
+ }
1803
+ function close() {
1804
+ if (!state.isOpen) return;
1805
+ state.isOpen = false;
1806
+ root.setAttribute("data-open", "false");
1807
+ if (panel.el.getAttribute("data-view") === "contact") {
1808
+ state.contactChannel = null;
1809
+ setView(baseView());
1810
+ restoreHeader();
1811
+ }
1812
+ cfg.onClose?.();
1813
+ }
1814
+ function restoreHeader() {
1815
+ panel.headerTitle.textContent = cfg.title;
1816
+ panel.headerSubtitleText.textContent = cfg.subtitle ?? "";
1817
+ panel.headerSubtitle.hidden = !cfg.subtitle;
1818
+ }
1819
+ function toggle() {
1820
+ state.isOpen ? close() : open();
1821
+ }
1822
+ function onCaptureSubmit(e) {
1823
+ e.preventDefault();
1824
+ clearCaptureError();
1825
+ const collected = readCaptureFields(panel);
1826
+ const required = cfg.capture.fields;
1827
+ const msgs = cfg.capture.errorMessages;
1828
+ for (const f of required) {
1829
+ if (!collected[f]) return failCapture(f, msgs.required);
1830
+ }
1831
+ if (required.includes("email") && !isValidEmail(collected.email ?? "")) {
1832
+ return failCapture("email", msgs.email);
1833
+ }
1834
+ if (required.includes("phone")) {
1835
+ const country = countryByIso(panel.phoneCountry?.value);
1836
+ const digits = (collected.phone ?? "").replace(/\D/g, "");
1837
+ if (!country || digits.length < country.nsn[0] || digits.length > country.nsn[1]) {
1838
+ return failCapture("phone", msgs.phone);
1839
+ }
1840
+ collected.phone = `${country.dial}${digits}`;
1841
+ }
1842
+ state.pendingLead = { ...state.pendingLead, ...collected };
1843
+ state.captured = true;
1844
+ persistCapturedLead();
1845
+ setView("chat");
1846
+ syncComposerLock();
1847
+ panel.input.focus();
1848
+ if (state.history.length === 0 && canGreet()) scheduleGreeting();
1849
+ }
1850
+ function persistCapturedLead() {
1851
+ if (!cfg.capture.createLeadOnSubmit || state.leadId) return;
1852
+ const lead = state.pendingLead;
1853
+ if (!lead.first_name && !lead.email && !lead.phone) return;
1854
+ const resolvedMeta = typeof cfg.metadata === "function" ? cfg.metadata() : cfg.metadata;
1855
+ state.leadCreating = client.createLead({
1856
+ first_name: lead.first_name || "Web visitor",
1857
+ last_name: lead.last_name,
1858
+ email: lead.email,
1859
+ phone: lead.phone,
1860
+ source: "web-chat",
1861
+ metadata: {
1862
+ ...resolvedMeta,
1863
+ web_chat_session_id: sessionId
1864
+ }
1865
+ }).then((res) => {
1866
+ if (res?.lead?.id && !state.leadId) {
1867
+ state.leadId = res.lead.id;
1868
+ cfg.onLeadCaptured?.(res.lead.id);
1869
+ }
1870
+ if (res && !res.existed) state.episodeChoiceOffered = true;
1871
+ }).catch((err) => {
1872
+ cfg.onError?.(err);
1873
+ }).finally(() => {
1874
+ state.leadCreating = null;
1875
+ });
1876
+ }
1877
+ function failCapture(field, message) {
1878
+ showCaptureError(message);
1879
+ const input = panel.captureInputs[field];
1880
+ const target = input?.closest(".nexor-chat__phone") || input;
1881
+ target?.classList.add("nexor-chat__input--error");
1882
+ input?.focus();
1883
+ }
1884
+ function showCaptureError(message) {
1885
+ if (!panel.captureError) return;
1886
+ panel.captureError.textContent = message;
1887
+ panel.captureError.hidden = false;
1888
+ }
1889
+ function clearCaptureError() {
1890
+ if (panel.captureError) {
1891
+ panel.captureError.textContent = "";
1892
+ panel.captureError.hidden = true;
1893
+ }
1894
+ panel.captureForm?.querySelectorAll(".nexor-chat__input--error").forEach((el) => el.classList.remove("nexor-chat__input--error"));
1895
+ }
1896
+ function wirePhoneCap(select, input, onChange) {
1897
+ const cap = () => {
1898
+ const c = countryByIso(select.value);
1899
+ const max = c ? maxNsn(c) : 15;
1900
+ input.maxLength = max;
1901
+ const digits = input.value.replace(/\D/g, "").slice(0, max);
1902
+ if (input.value !== digits) input.value = digits;
1903
+ };
1904
+ input.addEventListener("input", cap);
1905
+ select.addEventListener("change", () => {
1906
+ cap();
1907
+ onChange?.();
1908
+ });
1909
+ }
1910
+ function openContact(channel) {
1911
+ const c = panel.contact;
1912
+ const rc = cfg.requestContact;
1913
+ if (!c || !rc) return;
1914
+ state.contactChannel = channel;
1915
+ panel.headerTitle.textContent = rc.title[channel] ?? cfg.title;
1916
+ panel.headerSubtitleText.textContent = rc.subtitle[channel] ?? "";
1917
+ panel.headerSubtitle.hidden = !rc.subtitle[channel];
1918
+ c.submitBtn.textContent = rc.submitLabel[channel] ?? "";
1919
+ c.successText.textContent = rc.successText[channel] ?? "";
1920
+ c.view.setAttribute("data-state", "form");
1921
+ c.submitBtn.disabled = false;
1922
+ clearContactError();
1923
+ resetContactForm();
1924
+ cancelGreeting();
1925
+ state.isOpen = true;
1926
+ root.setAttribute("data-open", "true");
1927
+ clearUnread();
1928
+ setView("contact");
1929
+ const firstEmpty = [c.inputs.first_name, c.inputs.email, c.inputs.phone].find(
1930
+ (i) => i && !i.value
1931
+ );
1932
+ (firstEmpty ?? c.inputs.first_name ?? null)?.focus();
1933
+ cfg.onOpen?.();
1934
+ }
1935
+ function resetContactForm() {
1936
+ const c = panel.contact;
1937
+ if (!c) return;
1938
+ const lead = state.pendingLead;
1939
+ if (c.inputs.first_name) c.inputs.first_name.value = lead.first_name ?? "";
1940
+ if (c.inputs.last_name) c.inputs.last_name.value = lead.last_name ?? "";
1941
+ if (c.inputs.email) c.inputs.email.value = lead.email ?? "";
1942
+ const def = resolveDefaultCountry(cfg.capture.phoneCountryCode);
1943
+ if (c.phoneCountry) c.phoneCountry.value = def.iso;
1944
+ if (c.inputs.phone) {
1945
+ let national = "";
1946
+ if (lead.phone) {
1947
+ const digits = String(lead.phone).replace(/\D/g, "");
1948
+ const dialDigits = def.dial.replace(/\D/g, "");
1949
+ national = (digits.startsWith(dialDigits) ? digits.slice(dialDigits.length) : digits).slice(
1950
+ 0,
1951
+ maxNsn(def)
1952
+ );
1953
+ }
1954
+ c.inputs.phone.maxLength = maxNsn(def);
1955
+ c.inputs.phone.value = national;
1956
+ }
1957
+ }
1958
+ function onContactSubmit(e) {
1959
+ e.preventDefault();
1960
+ const c = panel.contact;
1961
+ const channel = state.contactChannel;
1962
+ if (!c || !channel) return;
1963
+ clearContactError();
1964
+ const msgs = cfg.capture.errorMessages;
1965
+ const first = c.inputs.first_name?.value.trim() ?? "";
1966
+ const last = c.inputs.last_name?.value.trim() ?? "";
1967
+ const email = c.inputs.email?.value.trim() ?? "";
1968
+ const phoneDigits = (c.inputs.phone?.value ?? "").replace(/\D/g, "");
1969
+ if (!first) return failContact("first_name", msgs.required);
1970
+ const needEmail = channel === "email";
1971
+ if (needEmail && !email) return failContact("email", msgs.required);
1972
+ if (email && !isValidEmail(email)) return failContact("email", msgs.email);
1973
+ const needPhone = channel === "call" || channel === "whatsapp";
1974
+ let e164;
1975
+ if (needPhone || phoneDigits) {
1976
+ const country = countryByIso(c.phoneCountry?.value);
1977
+ if (!country || phoneDigits.length < country.nsn[0] || phoneDigits.length > country.nsn[1]) {
1978
+ return failContact("phone", msgs.phone);
1979
+ }
1980
+ e164 = `${country.dial}${phoneDigits}`;
1981
+ }
1982
+ void submitContact(channel, { first, last, email, phone: e164 });
1983
+ }
1984
+ async function submitContact(channel, data) {
1985
+ const c = panel.contact;
1986
+ const rc = cfg.requestContact;
1987
+ if (!c || !rc) return;
1988
+ c.submitBtn.disabled = true;
1989
+ const resolvedMeta = typeof cfg.metadata === "function" ? cfg.metadata() : cfg.metadata;
1990
+ const meta = resolvedMeta ?? void 0;
1991
+ const pageUrl = meta && meta.page_url || (typeof window !== "undefined" ? window.location.href : void 0);
1992
+ try {
1993
+ const res = await client.requestContact(
1994
+ {
1995
+ workflow_id: cfg.workflowId,
1996
+ first_name: data.first,
1997
+ last_name: data.last || void 0,
1998
+ email: data.email || void 0,
1999
+ phone: data.phone,
2000
+ force_first_channel: channel,
2001
+ source: "web-chat",
2002
+ // Context for the agent's first outreach: they were on the site and
2003
+ // explicitly asked to be reached on this channel.
2004
+ metadata: {
2005
+ ...meta ?? {},
2006
+ contact_request: true,
2007
+ requested_channel: channel,
2008
+ requested_at: (/* @__PURE__ */ new Date()).toISOString(),
2009
+ context_note: `Website visitor asked to be contacted via ${channel}${pageUrl ? ` from ${pageUrl}` : ""}.`
2010
+ }
2011
+ },
2012
+ { timeoutMs: cfg.requestTimeoutMs, maxRetries: 0 }
2013
+ );
2014
+ if (res?.lead?.id && !state.leadId) {
2015
+ state.leadId = res.lead.id;
2016
+ cfg.onLeadCaptured?.(res.lead.id);
2017
+ }
2018
+ c.successText.textContent = rc.successText[channel] ?? "";
2019
+ panel.headerSubtitle.hidden = true;
2020
+ c.view.setAttribute("data-state", "sent");
2021
+ } catch (err) {
2022
+ c.submitBtn.disabled = false;
2023
+ showContactError(rc.errorText);
2024
+ cfg.onError?.(err);
2025
+ }
2026
+ }
2027
+ function failContact(field, message) {
2028
+ const c = panel.contact;
2029
+ if (!c) return;
2030
+ showContactError(message);
2031
+ const input = c.inputs[field];
2032
+ const target = input?.closest(".nexor-chat__phone") || input;
2033
+ target?.classList.add("nexor-chat__input--error");
2034
+ input?.focus();
2035
+ }
2036
+ function showContactError(message) {
2037
+ const c = panel.contact;
2038
+ if (!c) return;
2039
+ c.error.textContent = message;
2040
+ c.error.hidden = false;
2041
+ }
2042
+ function clearContactError() {
2043
+ const c = panel.contact;
2044
+ if (!c) return;
2045
+ c.error.textContent = "";
2046
+ c.error.hidden = true;
2047
+ c.form.querySelectorAll(".nexor-chat__input--error").forEach((el) => el.classList.remove("nexor-chat__input--error"));
2048
+ }
2049
+ function setView(view) {
2050
+ panel.el.setAttribute("data-view", view);
2051
+ }
2052
+ function baseView() {
2053
+ return state.captured ? "chat" : "capture";
2054
+ }
2055
+ function identityText() {
2056
+ const l = state.pendingLead;
2057
+ const who = (l.first_name || l.email || l.phone || "").trim();
2058
+ return who ? cfg.resumeWithNameText.replace("{name}", who) : cfg.resumeText;
2059
+ }
2060
+ function renderResumeBanner(prepend = false) {
2061
+ if (resumeBanner) return;
2062
+ const banner = buildResumeBanner(identityText(), cfg.startOverText);
2063
+ banner.resetBtn.addEventListener("click", startOver);
2064
+ if (prepend && panel.body.firstChild) {
2065
+ panel.body.insertBefore(banner.el, panel.body.firstChild);
2066
+ } else {
2067
+ panel.body.appendChild(banner.el);
2068
+ }
2069
+ resumeBanner = banner.el;
2070
+ }
2071
+ function startOver() {
2072
+ cancelGreeting();
2073
+ if (send.timer) {
2074
+ window.clearTimeout(send.timer);
2075
+ send.timer = 0;
2076
+ }
2077
+ if (send.splitTimer) {
2078
+ window.clearTimeout(send.splitTimer);
2079
+ send.splitTimer = 0;
2080
+ }
2081
+ send.pending = [];
2082
+ hideTyping();
2083
+ try {
2084
+ window.localStorage.removeItem(historyKey);
2085
+ } catch {
2086
+ }
2087
+ state.history = [];
2088
+ state.shownOpening = null;
2089
+ resumeBanner = null;
2090
+ panel.body.replaceChildren();
2091
+ if (panel.captureView) {
2092
+ state.pendingLead = { ...cfg.lead ?? {} };
2093
+ state.captured = false;
2094
+ state.leadId = void 0;
2095
+ state.episodeChoiceOffered = false;
2096
+ state.resetEpisode = true;
2097
+ resetCaptureForm();
2098
+ setView("capture");
2099
+ syncComposerLock();
2100
+ const first = cfg.capture.fields[0];
2101
+ (first && panel.captureInputs[first] || panel.input).focus();
2102
+ return;
2103
+ }
2104
+ state.resetEpisode = true;
2105
+ setView("chat");
2106
+ syncComposerLock();
2107
+ if (state.captured && canGreet()) scheduleGreeting();
2108
+ panel.input.focus();
2109
+ }
2110
+ function resetCaptureForm() {
2111
+ const lead = state.pendingLead;
2112
+ if (panel.captureInputs.first_name) panel.captureInputs.first_name.value = lead.first_name ?? "";
2113
+ if (panel.captureInputs.last_name) panel.captureInputs.last_name.value = lead.last_name ?? "";
2114
+ if (panel.captureInputs.email) panel.captureInputs.email.value = lead.email ?? "";
2115
+ const def = resolveDefaultCountry(cfg.capture.phoneCountryCode);
2116
+ if (panel.phoneCountry) panel.phoneCountry.value = def.iso;
2117
+ if (panel.captureInputs.phone) {
2118
+ let national = "";
2119
+ if (lead.phone) {
2120
+ const digits = String(lead.phone).replace(/\D/g, "");
2121
+ const dialDigits = def.dial.replace(/\D/g, "");
2122
+ national = (digits.startsWith(dialDigits) ? digits.slice(dialDigits.length) : digits).slice(
2123
+ 0,
2124
+ maxNsn(def)
2125
+ );
2126
+ }
2127
+ panel.captureInputs.phone.maxLength = maxNsn(def);
2128
+ panel.captureInputs.phone.value = national;
2129
+ }
2130
+ clearCaptureError();
2131
+ }
2132
+ function onComposerSubmit(e) {
2133
+ e.preventDefault();
2134
+ const text = panel.input.value.trim();
2135
+ if (!text) return;
2136
+ panel.input.value = "";
2137
+ autosizeComposer();
2138
+ queueMessage(text);
2139
+ }
2140
+ function autosizeComposer() {
2141
+ const el = panel.input;
2142
+ el.style.height = "auto";
2143
+ el.style.height = `${el.scrollHeight + 2}px`;
2144
+ }
2145
+ function queueMessage(text) {
2146
+ if (!state.captured) {
2147
+ const first = cfg.capture.fields[0];
2148
+ (first && panel.captureInputs[first] || panel.input).focus();
2149
+ return;
2150
+ }
2151
+ cancelGreeting();
2152
+ appendUser(text);
2153
+ send.pending.push(text);
2154
+ scheduleFlush();
2155
+ }
2156
+ function greetingText() {
2157
+ const raw = cfg.openingMessage || state.serverInitialMessage;
2158
+ if (raw) return renderLeadTemplate(raw, state.pendingLead);
2159
+ return cfg.greeting ?? "";
2160
+ }
2161
+ function canGreet() {
2162
+ if (!state.webchatEnabled) return false;
2163
+ if (cfg.openingMessage) return true;
2164
+ if (!state.configResolved) return true;
2165
+ return !!state.serverInitialMessage || !!cfg.greeting;
2166
+ }
2167
+ function scheduleGreeting() {
2168
+ if (greeting.pending) return;
2169
+ greeting.pending = true;
2170
+ showTyping();
2171
+ greeting.timer = window.setTimeout(async () => {
2172
+ if (!cfg.openingMessage) await configReady;
2173
+ if (!greeting.pending) return;
2174
+ greeting.pending = false;
2175
+ greeting.timer = 0;
2176
+ hideTyping();
2177
+ const text = greetingText();
2178
+ if (text) {
2179
+ state.shownOpening = text;
2180
+ appendBot(text);
2181
+ }
2182
+ }, GREETING_DELAY_MS);
2183
+ }
2184
+ function cancelGreeting() {
2185
+ if (!greeting.pending) return;
2186
+ if (greeting.timer) window.clearTimeout(greeting.timer);
2187
+ greeting.timer = 0;
2188
+ greeting.pending = false;
2189
+ hideTyping();
2190
+ }
2191
+ async function loadServerConfig() {
2192
+ try {
2193
+ const res = await client.getChatConfig(cfg.workflowId, {
2194
+ timeoutMs: 1e4,
2195
+ maxRetries: 1
2196
+ });
2197
+ applyServerConfig(res.enabled !== false, res.initial_message ?? null);
2198
+ } catch {
2199
+ applyServerConfig(true, null);
2200
+ }
2201
+ }
2202
+ function applyServerConfig(enabled, initialMessage) {
2203
+ state.configResolved = true;
2204
+ state.serverInitialMessage = initialMessage;
2205
+ state.webchatEnabled = enabled;
2206
+ if (!enabled) {
2207
+ hideChatChannelButton();
2208
+ cancelGreeting();
2209
+ if (state.isOpen) showDisabled();
2210
+ }
2211
+ }
2212
+ function showDisabled() {
2213
+ setView("chat");
2214
+ if (!state.disabledShown) {
2215
+ appendSystem(cfg.disabledText);
2216
+ state.disabledShown = true;
2217
+ }
2218
+ panel.input.disabled = true;
2219
+ panel.send.disabled = true;
2220
+ panel.input.placeholder = cfg.disabledText;
2221
+ }
2222
+ function hideChatChannelButton() {
2223
+ for (const btn of launcher.channelButtons) {
2224
+ if (btn.key !== "chat") continue;
2225
+ const item = btn.el.closest(".nexor-chat__channel-item");
2226
+ if (item instanceof HTMLElement) item.style.display = "none";
2227
+ }
2228
+ }
2229
+ function showTyping() {
2230
+ if (send.typing) {
2231
+ panel.body.appendChild(send.typing);
2232
+ scrollToBottom();
2233
+ return;
2234
+ }
2235
+ send.typing = appendNode(buildTypingIndicator());
2236
+ }
2237
+ function hideTyping() {
2238
+ send.typing?.remove();
2239
+ send.typing = null;
2240
+ }
2241
+ function scheduleFlush() {
2242
+ showTyping();
2243
+ if (send.timer) window.clearTimeout(send.timer);
2244
+ send.timer = window.setTimeout(() => void flush(), cfg.debounceMs);
2245
+ }
2246
+ async function flush() {
2247
+ if (send.inFlight || send.pending.length === 0) return;
2248
+ const batch = send.pending.splice(0, send.pending.length);
2249
+ const message = batch.join("\n");
2250
+ send.inFlight = true;
2251
+ if (state.leadCreating) {
2252
+ try {
2253
+ await state.leadCreating;
2254
+ } catch {
2255
+ }
2256
+ }
2257
+ const turnResetsEpisode = state.resetEpisode;
2258
+ try {
2259
+ const res = await client.chat(
2260
+ {
2261
+ workflow_id: cfg.workflowId,
2262
+ session_id: sessionId,
2263
+ message,
2264
+ // Additive page/instance prompt injection (API appends to the
2265
+ // workflow persona). Per route, the host passes different values.
2266
+ system_prompt: cfg.systemPrompt,
2267
+ client_prompt: cfg.clientPrompt,
2268
+ // The opening the visitor actually saw, so the agent doesn't repeat
2269
+ // or contradict a per-page opening override.
2270
+ opening_shown: state.shownOpening ?? void 0,
2271
+ // First turn after "start over" → reset the conversation episode on
2272
+ // the same lead (clear prior run/thread/variables server-side).
2273
+ reset_episode: turnResetsEpisode || void 0,
2274
+ lead: Object.keys(state.pendingLead).length ? state.pendingLead : void 0,
2275
+ // Resolve metadata per-turn so a function form picks up live SPA state
2276
+ // (e.g. the current route) without re-mounting the widget.
2277
+ metadata: typeof cfg.metadata === "function" ? cfg.metadata() : cfg.metadata
2278
+ },
2279
+ // A chat turn is NOT idempotent (re-sending re-runs the LLM and
2280
+ // re-inserts the message). Never retry; just give it a generous timeout.
2281
+ { timeoutMs: cfg.requestTimeoutMs, maxRetries: 0 }
2282
+ );
2283
+ if (turnResetsEpisode) state.resetEpisode = false;
2284
+ if (res.lead_id && !state.leadId) {
2285
+ state.leadId = res.lead_id;
2286
+ cfg.onLeadCaptured?.(state.leadId);
2287
+ }
2288
+ if (res.matched_existing && state.freshLocalSession && !state.episodeChoiceOffered && !turnResetsEpisode) {
2289
+ state.episodeChoiceOffered = true;
2290
+ renderResumeBanner(true);
2291
+ }
2292
+ const reply = (res.reply ?? "").trim();
2293
+ hideTyping();
2294
+ if (reply) {
2295
+ appendBotReply(reply);
2296
+ } else {
2297
+ appendSystem(cfg.errorText);
2298
+ cfg.onError?.(new Error("Nexor SDK: empty reply from server"));
2299
+ }
2300
+ } catch (err) {
2301
+ hideTyping();
2302
+ if (/web chat is not enabled/i.test(err?.message || "")) {
2303
+ applyServerConfig(false, state.serverInitialMessage);
2304
+ } else {
2305
+ appendSystem(cfg.errorText);
2306
+ }
2307
+ cfg.onError?.(err);
2308
+ } finally {
2309
+ if (!send.splitTimer) hideTyping();
2310
+ send.inFlight = false;
2311
+ panel.input.focus();
2312
+ if (send.pending.length) scheduleFlush();
2313
+ }
2314
+ }
2315
+ function appendUser(text) {
2316
+ const ts = Date.now();
2317
+ appendBubble("user", text, ts);
2318
+ state.history.push({ role: "user", text, ts });
2319
+ saveHistory();
2320
+ cfg.onMessage?.({ role: "user", text });
2321
+ }
2322
+ function appendBot(text, showTime = true) {
2323
+ const ts = showTime ? Date.now() : void 0;
2324
+ appendBubble("bot", text, ts);
2325
+ state.history.push({ role: "bot", text, ts });
2326
+ saveHistory();
2327
+ if (!state.isOpen) bumpUnread();
2328
+ cfg.onMessage?.({ role: "bot", text });
2329
+ }
2330
+ function appendBotReply(text) {
2331
+ const parts = cfg.splitReplies ? splitReply(text) : [text];
2332
+ if (parts.length <= 1) {
2333
+ appendBot(text);
2334
+ return;
2335
+ }
2336
+ const [first, ...rest] = parts;
2337
+ appendBot(first ?? text, false);
2338
+ let i = 0;
2339
+ const sendNext = () => {
2340
+ const part = rest[i];
2341
+ if (part === void 0) return;
2342
+ const isLast = i === rest.length - 1;
2343
+ i++;
2344
+ showTyping();
2345
+ const delay = cfg.splitDelayMs > 0 ? cfg.splitDelayMs : typingDelayFor(part);
2346
+ send.splitTimer = window.setTimeout(() => {
2347
+ send.splitTimer = 0;
2348
+ hideTyping();
2349
+ appendBot(part, isLast);
2350
+ sendNext();
2351
+ }, delay);
2352
+ };
2353
+ sendNext();
2354
+ }
2355
+ function splitReply(text) {
2356
+ const parts = text.split(/\n[ \t]*\n+/).map((s) => s.trim()).filter(Boolean);
2357
+ return parts.length ? parts : [text];
2358
+ }
2359
+ function typingDelayFor(text) {
2360
+ const MIN = 3e3;
2361
+ const MAX = 5e3;
2362
+ const FULL_AT = 180;
2363
+ const scaled = MIN + Math.min(text.length, FULL_AT) * ((MAX - MIN) / FULL_AT);
2364
+ const jitter = Math.random() * 300 - 150;
2365
+ return Math.round(Math.max(MIN, Math.min(MAX, scaled + jitter)));
2366
+ }
2367
+ function saveHistory() {
2368
+ try {
2369
+ window.localStorage.setItem(
2370
+ historyKey,
2371
+ // Persist the known lead alongside the transcript so a returning visitor
2372
+ // is recognised (and not re-asked for their details) after a reload.
2373
+ JSON.stringify({
2374
+ ts: Date.now(),
2375
+ history: state.history,
2376
+ lead: state.pendingLead
2377
+ })
2378
+ );
2379
+ touchSession();
2380
+ } catch {
2381
+ }
2382
+ }
2383
+ function loadPersisted() {
2384
+ const empty = { history: [], lead: void 0 };
2385
+ try {
2386
+ const raw = window.localStorage.getItem(historyKey);
2387
+ if (!raw) return empty;
2388
+ const o = JSON.parse(raw);
2389
+ if (!o || !Array.isArray(o.history)) return empty;
2390
+ if (Date.now() - (o.ts || 0) > SESSION_RETENTION_MS) {
2391
+ window.localStorage.removeItem(historyKey);
2392
+ return empty;
2393
+ }
2394
+ return {
2395
+ history: o.history,
2396
+ lead: o.lead && typeof o.lead === "object" ? o.lead : void 0
2397
+ };
2398
+ } catch {
2399
+ return empty;
2400
+ }
2401
+ }
2402
+ function appendSystem(text) {
2403
+ appendBubble("system", text);
2404
+ }
2405
+ function appendBubble(role, text, ts) {
2406
+ return appendNode(buildMessage(role, text, ts, cfg.locale));
2407
+ }
2408
+ const refreshTimer = window.setInterval(() => {
2409
+ panel.body.querySelectorAll(".nexor-chat__msg-time[data-ts]").forEach((el) => {
2410
+ const ts = Number(el.dataset.ts);
2411
+ if (ts) el.textContent = formatRelativeTime(ts, cfg.locale);
2412
+ });
2413
+ }, 6e4);
2414
+ function appendNode(node) {
2415
+ panel.body.appendChild(node);
2416
+ if (send.typing && send.typing !== node) panel.body.appendChild(send.typing);
2417
+ scrollToBottom();
2418
+ return node;
2419
+ }
2420
+ function scrollToBottom() {
2421
+ panel.body.scrollTop = panel.body.scrollHeight;
2422
+ }
2423
+ function bumpUnread() {
2424
+ state.unread++;
2425
+ launcher.badge.textContent = String(state.unread);
2426
+ launcher.badge.style.display = "flex";
2427
+ }
2428
+ function clearUnread() {
2429
+ state.unread = 0;
2430
+ launcher.badge.style.display = "none";
2431
+ }
2432
+ function syncComposerLock() {
2433
+ const lock = !state.captured;
2434
+ panel.input.disabled = lock;
2435
+ panel.send.disabled = lock;
2436
+ panel.input.placeholder = lock ? "Complete the form above to start\u2026" : cfg.inputPlaceholder;
2437
+ }
2438
+ return {
2439
+ open,
2440
+ close,
2441
+ toggle,
2442
+ // Programmatic send goes through the same debounce/coalesce path. Resolves
2443
+ // immediately — the message is queued, not necessarily sent yet.
2444
+ send: (text) => {
2445
+ queueMessage(text);
2446
+ return Promise.resolve();
2447
+ },
2448
+ destroy: () => {
2449
+ if (send.timer) window.clearTimeout(send.timer);
2450
+ if (send.splitTimer) window.clearTimeout(send.splitTimer);
2451
+ if (greeting.timer) window.clearTimeout(greeting.timer);
2452
+ window.clearInterval(refreshTimer);
2453
+ if (onDocPointerDown) document.removeEventListener("pointerdown", onDocPointerDown);
2454
+ root.remove();
2455
+ },
2456
+ getSessionId: () => sessionId
2457
+ };
2458
+ }
2459
+ function ensureBrowser() {
2460
+ if (typeof window === "undefined" || typeof document === "undefined") {
2461
+ throw new Error("Nexor SDK: initChat() requires a browser environment.");
2462
+ }
2463
+ }
2464
+ function buildRoot(cfg) {
2465
+ const root = document.createElement("div");
2466
+ root.className = "nexor-chat";
2467
+ root.setAttribute("data-position", cfg.position);
2468
+ root.setAttribute("data-open", "false");
2469
+ return root;
2470
+ }
2471
+ function readCaptureFields(panel) {
2472
+ const out = {};
2473
+ const inputs = panel.captureInputs;
2474
+ if (inputs.first_name) out.first_name = inputs.first_name.value.trim();
2475
+ if (inputs.last_name) out.last_name = inputs.last_name.value.trim();
2476
+ if (inputs.email) out.email = inputs.email.value.trim();
2477
+ if (inputs.phone) out.phone = inputs.phone.value.replace(/\D/g, "");
2478
+ return out;
2479
+ }
2480
+
2481
+ export { initChat };
2482
+ //# sourceMappingURL=chunk-WUUXZIQN.js.map
2483
+ //# sourceMappingURL=chunk-WUUXZIQN.js.map