(function(){
try {
// Find the current script tag
var thisScript = (function(){
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length - 1; i >= 0; i--) {
var s = scripts[i];
if (s && /wabble-widget\.js(\?.*)?$/.test(s.src)) return s;
}
return null;
})();
var APP_BASE = (thisScript && thisScript.getAttribute('data-app-base')) || (function(){
try { return new URL(thisScript ? thisScript.src : window.location.href).origin; } catch(e){ return window.location.origin; }
})();
var ORG_ID = thisScript && thisScript.getAttribute('data-org-id');
var WA_PHONE = thisScript && thisScript.getAttribute('data-wa-phone');
var RAW_PROMPTS = (thisScript && thisScript.getAttribute('data-prompts')) || '';
var POSITION = (thisScript && thisScript.getAttribute('data-position')) || 'right'; // 'right' or 'left'
var THEME = (thisScript && thisScript.getAttribute('data-theme')) || 'auto'; // 'light' | 'dark' | 'auto'
var Z_INDEX = (thisScript && thisScript.getAttribute('data-z')) || '2147483000';
function parsePrompts(raw) {
if (!raw) return null;
try {
var p = JSON.parse(raw);
return Array.isArray(p) ? p.slice(0, 5) : null;
} catch (e) {
// support semicolon separated list
if (typeof raw === 'string') return raw.split(';').map(function(s){ return s.trim(); }).filter(Boolean).slice(0,5);
}
return null;
}
var DEFAULT_PROMPTS = [
'Hi! I\'ve got a question.',
'Hi! I would like to get on a demo call.',
'Hi! I\'d like onboarding assistance.'
];
var PROMPTS = parsePrompts(RAW_PROMPTS) || DEFAULT_PROMPTS;
function buildWaUrl(text) {
if (!WA_PHONE) return null;
var base = 'https://api.whatsapp.com/send/';
var qs = '?phone=' + encodeURIComponent(WA_PHONE) + '&text=' + encodeURIComponent(text || '') + '&type=phone_number&app_absent=0';
return base + qs;
}
var links = {
inbox: APP_BASE + '/conversations' + (ORG_ID ? ('?org=' + encodeURIComponent(ORG_ID)) : ''),
setup: APP_BASE + '/settings?tab=whatsapp-business-account' + (ORG_ID ? ('&org=' + encodeURIComponent(ORG_ID)) : '')
};
// Create container and shadow DOM to isolate styles
var host = document.createElement('div');
host.setAttribute('id', 'wabble-widget-host');
host.style.position = 'fixed';
host.style[POSITION === 'left' ? 'left' : 'right'] = '20px';
host.style.bottom = '20px';
host.style.zIndex = Z_INDEX;
document.body.appendChild(host);
var shadow = host.attachShadow({ mode: 'open' });
// Detect theme
var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
var isDark = THEME === 'dark' || (THEME === 'auto' && prefersDark);
// Brand primary uses orange gradient to match app theme
var primaryLight = 'linear-gradient(90deg, #FF956D, #FBC79D)';
var primaryDark = 'linear-gradient(90deg, #FF956D, #FBC79D)';
var whatsappPastel = '#DFF5E6';
var fgLight = '#ffffff';
var fgDark = '#0a0a0a';
var colors = {
primary: isDark ? primaryDark : primaryLight,
foreground: isDark ? fgDark : fgLight,
bgPanel: isDark ? '#111827' : '#ffffff',
text: isDark ? '#e5e7eb' : '#111827',
border: isDark ? '#374151' : '#e5e7eb',
shadow: '0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05)'
};
var style = document.createElement('style');
style.textContent = "\n :host { all: initial; }\n * { box-sizing: border-box; font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, Apple Color Emoji, Segoe UI Emoji; }\n .ww-launcher {\n width: 72px; height: 72px; border-radius: 9999px; display: inline-flex; align-items: center; justify-content: center;\n background: " + colors.primary + "; color: " + colors.foreground + "; border: 1px solid rgba(0,0,0,0.05);\n box-shadow: " + colors.shadow + "; cursor: pointer; transition: transform .2s ease, box-shadow .2s ease, opacity .2s ease;\n }\n .ww-launcher:hover { transform: translateY(-1px); }\n .ww-panel {\n position: absolute; bottom: 88px; " + (POSITION === 'left' ? 'left' : 'right') + ": 0; min-width: 320px; max-width: 360px;\n background: " + colors.bgPanel + "; color: " + colors.text + "; border: 1px solid " + colors.border + "; border-radius: 14px;\n box-shadow: " + colors.shadow + "; padding: 16px; display: none;\n }\n .ww-header { font-weight: 700; font-size: 14px; margin-bottom: 8px; }\n .ww-desc { font-size: 12px; color: " + (isDark ? '#9ca3af' : '#6b7280') + "; margin-bottom: 10px; }\n .ww-actions { display: flex; gap: 10px; margin-top: 12px; }\n .ww-btn { flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 8px;\n height: 40px; border-radius: 10px; border: 1px solid " + colors.border + "; cursor: pointer; text-decoration: none;\n background: transparent; color: " + colors.text + "; font-weight: 600; font-size: 13px;\n }\n .ww-btn.primary { background: " + colors.primary + "; color: " + colors.foreground + "; border: none; }\n .ww-brand { display: block; text-align: center; font-size: 10px; margin-top: 10px; color: " + (isDark ? '#9ca3af' : '#6b7280') + "; }\n .ww-divider { height: 1px; width: 100%; background: " + colors.border + "; margin-top: 10px; }\n .ww-hidden { display: none !important; }\n .ww-icon { width: 22px; height: 22px; }\n .ww-list { margin-top: 10px; }\n .ww-item { display: block; padding: 8px; border-bottom: 1px solid " + colors.border + "; }\n .ww-item:last-child { border-bottom: none; }\n ";
var container = document.createElement('div');
var btn = document.createElement('button');
btn.className = 'ww-launcher';
btn.setAttribute('aria-label', 'Open widget');
btn.title = 'Chat & setup';
btn.innerHTML = '';
var pnl = document.createElement('div');
pnl.className = 'ww-panel';
pnl.setAttribute('role', 'dialog');
pnl.setAttribute('aria-label', 'Widget panel');
container.appendChild(btn);
container.appendChild(pnl);
shadow.appendChild(style);
shadow.appendChild(container);
var launcher = shadow.querySelector('.ww-launcher');
var panel = shadow.querySelector('.ww-panel');
if (launcher && panel) {
// Update launcher icon: simple WhatsApp logo
launcher.innerHTML = '\n \n ';
// Compact panel inspired by provided design (DOM API, CSS classes)
var setupHref = links.setup;
// Use first prompt as default WA deep link if phone is present
var p0 = PROMPTS[0] || 'Hi! I have a question.';
var waDefault = buildWaUrl(p0);
var primaryHref = waDefault || setupHref;
var primaryLabel = WA_PHONE ? 'WhatsApp' : 'Connect';
// Clear panel and build content
while (panel.firstChild) panel.removeChild(panel.firstChild);
var h = document.createElement('div');
h.className = 'ww-header';
h.textContent = 'WhatsApp Inbox';
var d = document.createElement('div');
d.className = 'ww-desc';
d.textContent = 'Manage conversations or finish your WhatsApp setup.';
// Optional quick prompts list (WhatsApp only)
var list = null;
if (WA_PHONE) {
list = document.createElement('div');
list.className = 'ww-list';
for (var i = 0; i < Math.min(PROMPTS.length, 3); i++) {
var text = PROMPTS[i];
var url = buildWaUrl(text);
if (!url) continue;
var item = document.createElement('a');
item.className = 'ww-item';
item.href = url;
item.target = '_blank';
item.rel = 'noopener noreferrer';
item.textContent = text;
list.appendChild(item);
}
}
var actions = document.createElement('div');
actions.className = 'ww-actions';
var aSetup = document.createElement('a');
aSetup.className = 'ww-btn';
aSetup.href = setupHref;
aSetup.target = '_blank';
aSetup.rel = 'noopener noreferrer';
aSetup.textContent = 'Setup';
var aInbox = document.createElement('a');
aInbox.className = 'ww-btn primary';
aInbox.href = primaryHref;
aInbox.target = '_blank';
aInbox.rel = 'noopener noreferrer';
aInbox.textContent = primaryLabel;
actions.appendChild(aSetup);
actions.appendChild(aInbox);
var brand = document.createElement('div');
brand.className = 'ww-brand';
brand.textContent = 'Powered by Chat';
panel.appendChild(h);
panel.appendChild(d);
if (list) panel.appendChild(list);
panel.appendChild(actions);
var divider = document.createElement('div');
divider.className = 'ww-divider';
panel.appendChild(divider);
brand.textContent = 'Powered by Nextbird';
panel.appendChild(brand);
var open = false;
var toggle = function(){
open = !open;
panel.style.display = open ? 'block' : 'none';
};
launcher.addEventListener('click', function(e){
e.stopPropagation();
toggle();
});
// Close on outside click (supports Shadow DOM)
document.addEventListener('click', function(ev){
var path = (ev.composedPath && ev.composedPath()) || [];
var clickedInside = path.indexOf(host) !== -1 || host.contains(ev.target);
if (!clickedInside) {
open = false; panel.style.display = 'none';
}
});
// Close on Escape key
document.addEventListener('keydown', function(ev){
if (ev.key === 'Escape') {
open = false; panel.style.display = 'none';
}
});
}
} catch (e) {
console && console.error && console.error('[Wabble Widget] Failed to initialize:', e);
}
})();