UNPKG

whatsapp-floating

Version:

Dependency-free floating WhatsApp contact button with geolocation-based phone routing, business-hour scheduling, UTM/path rules and analytics integrations. Built for CDN delivery (jsDelivr/unpkg).

8 lines 25.5 kB
"use strict";(()=>{var Lt=Object.defineProperty,St=Object.defineProperties;var Mt=Object.getOwnPropertyDescriptors;var j=Object.getOwnPropertySymbols;var kt=Object.prototype.hasOwnProperty,It=Object.prototype.propertyIsEnumerable;var Y=(t,e,n)=>e in t?Lt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,b=(t,e)=>{for(var n in e||(e={}))kt.call(e,n)&&Y(t,n,e[n]);if(j)for(var n of j(e))It.call(e,n)&&Y(t,n,e[n]);return t},Z=(t,e)=>St(t,Mt(e));function q(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}function R(t,e){let n=b({},t),o=e;for(let i in o){if(!Object.prototype.hasOwnProperty.call(o,i))continue;let a=o[i],s=n[i];q(a)&&q(s)?n[i]=R(s,a):n[i]=a}return n}function m(t,e,...n){if(!t||!t.debug)return;let o="[WhatsAppFloating]";e==="error"?console.error(o,...n):e==="warn"?console.warn(o,...n):console.log(o,...n)}function Q(t,e){try{return JSON.parse(t)}catch(n){return e}}function A(t){try{let e=window[t],n="__wa_floating_test__";return e.setItem(n,"1"),e.removeItem(n),e}catch(e){return null}}function X(){let t={},e=window.location.search.replace(/^\?/,"");if(!e)return t;let n=e.split("&");for(let o of n){let[i,a]=o.split("="),s=decodeURIComponent(i||""),r=decodeURIComponent((a||"").replace(/\+/g," "));s&&(t[s]=r)}return t}function tt(t){let e=window.innerWidth<=(t.mobileBreakpoint||768),n=/\/mobile/i.test(window.location.pathname)||/\/mobile/i.test(window.location.href);return e||n}function et(){return(navigator.language||navigator.userLanguage||"en").toLowerCase()}var J={AF:["DZ","AO","BJ","BW","BF","BI","CM","CV","CF","TD","KM","CG","CD","DJ","EG","GQ","ER","SZ","ET","GA","GM","GH","GN","GW","CI","KE","LS","LR","LY","MG","MW","ML","MR","MU","MA","MZ","NA","NE","NG","RW","ST","SN","SC","SL","SO","ZA","SS","SD","TZ","TG","TN","UG","ZM","ZW"],AS:["AF","AM","AZ","BH","BD","BT","BN","KH","CN","CY","GE","IN","ID","IR","IQ","IL","JP","JO","KZ","KW","KG","LA","LB","MY","MV","MN","MM","NP","KP","OM","PK","PH","QA","SA","SG","KR","LK","SY","TW","TJ","TH","TL","TR","TM","AE","UZ","VN","YE"],EU:["AL","AD","AT","BY","BE","BA","BG","HR","CY","CZ","DK","EE","FI","FR","DE","GR","HU","IS","IE","IT","XK","LV","LI","LT","LU","MT","MD","MC","ME","NL","MK","NO","PL","PT","RO","RU","SM","RS","SK","SI","ES","SE","CH","UA","GB","VA"],NA:["AG","BS","BB","BZ","CA","CR","CU","DM","DO","SV","GD","GT","HT","HN","JM","MX","NI","PA","KN","LC","VC","TT","US"],SA:["AR","BO","BR","CL","CO","EC","GY","PY","PE","SR","UY","VE"],OC:["AU","FJ","KI","MH","FM","NR","NZ","PW","PG","WS","SB","TO","TV","VU"],AN:["AQ"]};function nt(t){var e;if(!t)return null;for(let n in J)if(((e=J[n])==null?void 0:e.indexOf(t))!==-1)return n;return null}function h(t){return String(t!=null?t:"").toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g,"").trim()}var ot="__wa_floating_geo_cache__",Et=1e3*60*60*6,Tt={country:null,state:null,city:null,ip:null};function p(t){return typeof t=="string"&&t.length>0?t:null}var Ft=t=>{var e,n;return(n=(e=p(t.ip))!=null?e:p(t.query))!=null?n:p(t.ip_address)},Wt=[{name:"ipapi.co",url:"https://ipapi.co/json/",parse:t=>{var e,n;return{country:(e=p(t.country))!=null?e:p(t.country_code),state:(n=p(t.region_code))!=null?n:p(t.region),city:p(t.city)}}},{name:"ipwho.is",url:"https://ipwho.is/",parse:t=>{var e,n;return{country:(e=p(t.country_code))!=null?e:p(t.country),state:(n=p(t.region_code))!=null?n:p(t.region),city:p(t.city)}}},{name:"ip-api.com",url:"http://ip-api.com/json/",parse:t=>{var e,n;return{country:(e=p(t.countryCode))!=null?e:p(t.country),state:(n=p(t.region))!=null?n:p(t.regionName),city:p(t.city)}}},{name:"ipinfo.io",url:"https://ipinfo.io/json",parse:t=>({country:p(t.country),state:p(t.region),city:p(t.city)})},{name:"ip.oxylabs.io",url:"https://ip.oxylabs.io/location",parse:t=>{var o,i,a;let e=t.providers,n=e&&((a=(i=(o=e.maxmind)!=null?o:e.dbip)!=null?i:e.ip2location)!=null?a:e.ipinfo);return{country:p(n==null?void 0:n.country),state:null,city:p(n==null?void 0:n.city)}}}];function Nt(t){let e=[...Wt];if(t.geoApiUrl&&e.unshift({name:"custom:geoApiUrl",url:t.geoApiUrl,parse:n=>{var o,i,a,s,r;return{country:(i=(o=p(n.country))!=null?o:p(n.country_code))!=null?i:p(n.countryCode),state:(r=(s=(a=p(n.region))!=null?a:p(n.region_code))!=null?s:p(n.regionName))!=null?r:p(n.state),city:p(n.city)}}}),t.geoProviders&&t.geoProviders.length)for(let n of t.geoProviders)e.push({name:"custom:"+n,url:n,parse:o=>{var i,a,s,r,c;return{country:(a=(i=p(o.country))!=null?i:p(o.country_code))!=null?a:p(o.countryCode),state:(c=(r=(s=p(o.region))!=null?s:p(o.region_code))!=null?r:p(o.regionName))!=null?c:p(o.state),city:p(o.city)}}});return e}function $t(t){return!!(t.country||t.state||t.city)}function _t(t,e){if(typeof AbortController=="undefined")return fetch(t);let n=new AbortController,o=setTimeout(()=>n.abort(),e);return fetch(t,{signal:n.signal}).finally(()=>clearTimeout(o))}async function Gt(t,e,n){var o;try{let i=await _t(t.url,e);if(!i.ok)return m(n,"warn",`Geo provider ${t.name} responded with status ${i.status}`),null;let a=await i.json(),s=t.parse(a);if(!$t(s))return m(n,"warn",`Geo provider ${t.name} returned no usable fields`),null;let r=((o=t.parseIp)!=null?o:Ft)(a),c=Z(b({},s),{ip:r});return m(n,"info",`Geo provider ${t.name} succeeded`,c),c}catch(i){return m(n,"warn",`Geo provider ${t.name} failed`,i),null}}function Ot(t){let e=A("localStorage");if(!e)return null;let n=e.getItem(ot);if(!n)return null;let o=Q(n,null);if(!o||!o.timestamp||!o.data)return null;let i=t.geoCacheTTL!=null?t.geoCacheTTL:Et;return i>0&&Date.now()-o.timestamp>i?null:o}function Dt(t){let e=A("localStorage");e&&e.setItem(ot,JSON.stringify({timestamp:Date.now(),data:t}))}async function it(t){var a,s,r,c;if(t.location&&(t.location.country||t.location.city||t.location.state))return{country:(a=t.location.country)!=null?a:null,state:(r=(s=t.location.state)!=null?s:t.location.region)!=null?r:null,city:(c=t.location.city)!=null?c:null,ip:null};let e=t.cacheLocation===!0,n=e?Ot(t):null,o=Nt(t),i=t.geoTimeout||5e3;for(let g of o){let l=await Gt(g,i,t);if(l)return n&&n.data.ip&&l.ip&&n.data.ip===l.ip?(m(t,"info","IP unchanged, reusing cached geolocation",n.data),n.data):(e&&Dt(l),l)}return n?(m(t,"warn","All geolocation providers failed, falling back to stale cache",n.data),n.data):(m(t,"error","All geolocation providers failed"),Tt)}var Ut=["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];function at(t){if(!t||typeof t!="string")return null;let e=t.split(":");if(e.length<2)return null;let n=parseInt(e[0],10),o=parseInt(e[1],10);return isNaN(n)||isNaN(o)?null:n*60+o}function Bt(t){if(!t)return new Date;try{let n=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1}).formatToParts(new Date),o=i=>{var a,s;return(s=(a=n.find(r=>r.type===i))==null?void 0:a.value)!=null?s:"0"};return new Date(Number(o("year")),Number(o("month"))-1,Number(o("day")),Number(o("hour"))===24?0:Number(o("hour")),Number(o("minute")),Number(o("second")))}catch(e){return new Date}}function E(t){if(!t)return!0;let e=Bt(t.timezone),n=Ut[e.getDay()];if(t.days&&t.days.length){let o=t.days.map(i=>String(i).toLowerCase());if(!n||o.indexOf(n)===-1)return!1}if(t.hours){let o=e.getHours()*60+e.getMinutes(),i=at(t.hours.start),a=at(t.hours.end);if(i!=null&&a!=null){if(i<=a){if(o<i||o>a)return!1}else if(o<i&&o>a)return!1}}return!0}var st={};function T(t,e){var a,s,r,c,g;if(t.phone)return t.phone;let n=t.numbers;if(!n||!n.length)return null;let o=t.distribution||"random";if(o==="weighted"){let l=0;for(let d of n)l+=d.weight||1;let f=Math.random()*l;for(let d of n)if(f-=d.weight||1,f<=0)return d.phone;return(s=(a=n[n.length-1])==null?void 0:a.phone)!=null?s:null}if(o==="roundrobin"||o==="round-robin"||o==="round_robin"){let l="rule_"+e,f=st[l]||0,d=n[f%n.length];return st[l]=f+1,(r=d==null?void 0:d.phone)!=null?r:null}let i=Math.floor(Math.random()*n.length);return(g=(c=n[i])==null?void 0:c.phone)!=null?g:null}function Vt(t,e){return t==null?!0:e?(Array.isArray(t)?t:[t]).some(o=>h(o)===h(e)):!1}function rt(t,e){if(!t.utm)return!0;for(let n in t.utm){if(!Object.prototype.hasOwnProperty.call(t.utm,n))continue;let o=t.utm[n],i=e["utm_"+n]||e[n];if(h(o)!==h(i))return!1}return!0}function lt(t,e){return!(t.language&&h(e.language).indexOf(h(t.language))!==0||t.referrer&&e.referrer.indexOf(t.referrer)===-1||t.domain&&e.domain.indexOf(t.domain)===-1||t.page&&e.page.indexOf(t.page)===-1||t.continent&&h(t.continent)!==h(e.continent)||t.schedule&&!E(t.schedule)||t.utm&&!rt(t,e.utm))}function ct(t,e){if(t.path&&e.page.indexOf(t.path)===-1)return!1;if(t.pathRegex)try{if(!new RegExp(t.pathRegex).test(e.page))return!1}catch(n){return!1}return!(!t.path&&!t.pathRegex||t.schedule&&!E(t.schedule)||t.utm&&!rt(t,e.utm))}function Kt(t,e){let n=t.pathRules||[];for(let o=0;o<n.length;o++){let i=n[o];if(!ct(i,e))continue;let a=T(i,"path_"+o);if(a||i.images)return{phone:a,matched:"Path",rule:i,images:i.images}}return null}var pt=[{key:"city",label:"Cidade"},{key:"state",label:"Estado"},{key:"country",label:"Pa\xEDs"}];function ut(t,e,n){if(e.key==="city"){let o=t.cities||t.city;if(!o||!Vt(o,n.city)||t.state&&h(t.state)!==h(n.state)||t.country&&h(t.country)!==h(n.country))return!1}else if(e.key==="state"){if(!t.state||t.cities||t.city||h(t.state)!==h(n.state)||t.country&&h(t.country)!==h(n.country))return!1}else if(!t.country||t.state||t.cities||t.city||h(t.country)!==h(n.country))return!1;return!0}function gt(t,e,n){let o=Kt(t,n);if(o&&o.phone)return o;let i=o==null?void 0:o.images,a=t.rules||[];for(let r of pt)if(e[r.key])for(let g=0;g<a.length;g++){let l=a[g];if(!lt(l,n)||!ut(l,r,e))continue;let f=T(l,g);if(f)return{phone:f,matched:r.label,rule:l,images:i||l.images}}let s=null;return t.fallback&&(s=T(t.fallback,"fallback")),{phone:s,matched:"Fallback",rule:t.fallback||null,images:i}}function ft(t,e,n,o,i){var r;let a=t.pathRules||[];for(let c of a)if(ct(c,n)&&c[o])return c[o];let s=t.rules||[];for(let c of pt)if(e[c.key]){for(let l of s)if(lt(l,n)&&ut(l,c,e)&&l[o])return l[o]}return(r=t.fallback)!=null&&r[o]?t.fallback[o]:i||""}function ht(t,e,n){return ft(t,e,n,"message",t.message)}function dt(t,e,n){var o;return ft(t,e,n,"pillText",(o=t.pill)==null?void 0:o.text)}function S(t,e,n){try{if(t.analytics===!1)return;typeof window.gtag=="function"&&window.gtag("event",e,n),window.dataLayer&&typeof window.dataLayer.push=="function"&&window.dataLayer.push(b({event:"whatsapp_floating_"+e},n)),typeof window.fbq=="function"&&window.fbq("trackCustom","WhatsAppFloating_"+e,n)}catch(o){m(t,"warn","Analytics tracking failed",o)}}var mt="wa-floating-styles";function P(t,e){return t==null?e:typeof t=="number"?`${t}px`:t}function zt(t,e){let n="25px",o=e||{};if(t==="custom"){let r="";return["top","bottom","left","right"].forEach(c=>{o[c]!=null&&(r+=`${c}:${P(o[c],n)};`)}),r||`bottom:${n};right:${n};`}let i=t.indexOf("left")!==-1?"left":"right",a=P(o.bottom,n),s=P(o[i],n);return`bottom:${a};${i}:${s};`}var M='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="100%" height="100%"><path fill="#25D366" d="M16 0C7.164 0 0 7.164 0 16c0 2.82.738 5.566 2.14 7.984L0 32l8.207-2.113A15.9 15.9 0 0 0 16 32c8.836 0 16-7.164 16-16S24.836 0 16 0z"/><path fill="#FFF" d="M25.144 22.63c-.4 1.13-1.99 2.07-3.25 2.34-.87.18-2 .33-5.8-1.24-4.86-2.02-7.99-6.94-8.23-7.26-.24-.32-1.97-2.62-1.97-5 0-2.38 1.24-3.55 1.68-4.03.44-.48.96-.6 1.28-.6.32 0 .64 0 .92.02.3.02.7-.11 1.09.83.4.96 1.36 3.34 1.48 3.58.12.24.2.52.04.84-.16.32-.24.52-.48.8-.24.28-.5.62-.72.84-.24.24-.49.5-.21.98.28.48 1.24 2.05 2.67 3.32 1.83 1.63 3.37 2.14 3.85 2.38.48.24.76.2 1.04-.12.28-.32 1.2-1.4 1.52-1.88.32-.48.64-.4 1.08-.24.44.16 2.8 1.32 3.28 1.56.48.24.8.36.92.56.12.2.12 1.14-.28 2.26z"/></svg>';function wt(t){var _,G,O,D,U,B,V,K,z,H;let e=t.theme||{},n=t.position||"bottom-right",o=zt(n,t.offset),i=t.zIndex||2147483647,a=P(e.iconSize,"64px"),s=P((_=e.iconSizeMobile)!=null?_:e.iconSize,"56px"),r=e.pulse,c=r===!0?{scale:!0}:r||{},g=r!=null&&c.scale!==!1,l=(G=c.duration)!=null?G:1800,f=(O=c.scaleAmount)!=null?O:1.08,d=(D=c.color)!=null?D:"#25D366",v=(U=c.opacity)!=null?U:.55,u=t.pill,y=(u==null?void 0:u.expand)||"hover",L=(B=u==null?void 0:u.expandMobile)!=null?B:y,vt=(u==null?void 0:u.color)||"#25D366",$=(u==null?void 0:u.textColor)||"#ffffff",xt=(V=u==null?void 0:u.fontSize)!=null?V:14,Ct=(K=u==null?void 0:u.fontWeight)!=null?K:600,At=(z=u==null?void 0:u.paddingRight)!=null?z:18,Rt=(H=u==null?void 0:u.borderRadius)!=null?H:999,x=t.mobileBreakpoint||768,Pt=".wa-floating-btn{position:fixed;"+o+`z-index:${i};display:inline-flex;align-items:center;justify-content:center;cursor:pointer;border:0;background:transparent;line-height:0;text-decoration:none;transition:transform .2s ease,opacity .2s ease;will-change:transform;opacity:0;transform:scale(.85);}.wa-floating-btn.wa-floating-visible{opacity:1;transform:scale(1);}.wa-floating-btn:hover{transform:scale(1.06);}.wa-floating-btn img{max-width:90vw;max-height:90vw;height:${a};width:auto;display:block;}.wa-floating-btn svg{width:${a};height:${a};display:block;flex-shrink:0;}@media(max-width:768px){.wa-floating-btn img{height:${s};}.wa-floating-btn svg{width:${s};height:${s};}}`+(e.rounded?".wa-floating-btn img,.wa-floating-btn svg{border-radius:12px;}":"")+(e.shadow?".wa-floating-btn img,.wa-floating-btn":"")+(e.shadow&&e.dark?".wa-floating-btn img,.wa-floating-btn svg{box-shadow:0 4px 20px rgba(0,0,0,.6);}":"")+(g?`.wa-floating-btn img,.wa-floating-btn svg{animation:wa-floating-pulse ${l}ms ease-in-out infinite;}@keyframes wa-floating-pulse{0%,100%{transform:scale(1);}50%{transform:scale(${f});}}`:"")+(c.ring?`.wa-floating-btn::after{content:"";position:absolute;inset:0;border-radius:50%;box-shadow:0 0 0 0 ${d};opacity:${v};animation:wa-floating-ring ${l}ms ease-out infinite;pointer-events:none;}@keyframes wa-floating-ring{0%{box-shadow:0 0 0 0 ${d};opacity:${v};}100%{box-shadow:0 0 0 24px ${d};opacity:0;}}`:"")+(u?`.wa-floating-btn.wa-floating-pill{display:inline-flex;align-items:center;justify-content:flex-start;border-radius:${Rt}px;background:${vt};overflow:hidden;max-width:${a};transition:max-width .3s ease;white-space:nowrap;}@media(max-width:${x}px){.wa-floating-btn.wa-floating-pill{max-width:${s};}}.wa-floating-pill-icon{flex:none;width:${a};height:${a};display:flex;align-items:center;justify-content:center;}@media(max-width:${x}px){.wa-floating-pill-icon{width:${s};height:${s};}}.wa-floating-pill-icon svg{width:60%!important;height:60%!important;max-width:none!important;max-height:none!important;display:block!important;}.wa-floating-pill-icon svg>:first-child{fill:transparent;}.wa-floating-pill-icon svg path:last-of-type{fill:${$};}`+(g?`.wa-floating-pill-icon{animation:wa-floating-pulse ${l}ms ease-in-out infinite;}.wa-floating-pill-icon svg{animation:none!important;}`:"")+`.wa-floating-pill-text{opacity:0;transition:opacity .2s ease .1s;padding-right:${At}px;color:${$};font:${Ct} ${xt}px/1.3 -apple-system,BlinkMacSystemFont,sans-serif;}`+(y==="hover"?`@media(min-width:${x+1}px){.wa-floating-btn.wa-floating-pill:hover{max-width:320px;}.wa-floating-btn.wa-floating-pill:hover .wa-floating-pill-text{opacity:1;}}`:"")+(y==="always"?`@media(min-width:${x+1}px){.wa-floating-btn.wa-floating-pill{max-width:320px;}.wa-floating-pill-text{opacity:1;}}`:"")+(y==="click"?`@media(min-width:${x+1}px){.wa-floating-btn.wa-floating-pill.wa-floating-expanded{max-width:320px;}.wa-floating-btn.wa-floating-pill.wa-floating-expanded .wa-floating-pill-text{opacity:1;}}`:"")+(L==="hover"?`@media(max-width:${x}px){.wa-floating-btn.wa-floating-pill:hover{max-width:320px;}.wa-floating-btn.wa-floating-pill:hover .wa-floating-pill-text{opacity:1;}}`:"")+(L==="always"?`@media(max-width:${x}px){.wa-floating-btn.wa-floating-pill{max-width:320px;}.wa-floating-pill-text{opacity:1;}}`:"")+(L==="click"?`@media(max-width:${x}px){.wa-floating-btn.wa-floating-pill.wa-floating-expanded{max-width:320px;}.wa-floating-btn.wa-floating-pill.wa-floating-expanded .wa-floating-pill-text{opacity:1;}}`:""):"")+(t.css||""),C=document.getElementById(mt);C||(C=document.createElement("style"),C.id=mt,C.type="text/css",document.head.appendChild(C)),C.textContent=Pt}var F='<path fill="#FFF" d="M25.144 22.63c-.4 1.13-1.99 2.07-3.25 2.34-.87.18-2 .33-5.8-1.24-4.86-2.02-7.99-6.94-8.23-7.26-.24-.32-1.97-2.62-1.97-5 0-2.38 1.24-3.55 1.68-4.03.44-.48.96-.6 1.28-.6.32 0 .64 0 .92.02.3.02.7-.11 1.09.83.4.96 1.36 3.34 1.48 3.58.12.24.2.52.04.84-.16.32-.24.52-.48.8-.24.28-.5.62-.72.84-.24.24-.49.5-.21.98.28.48 1.24 2.05 2.67 3.32 1.83 1.63 3.37 2.14 3.85 2.38.48.24.76.2 1.04-.12.28-.32 1.2-1.4 1.52-1.88.32-.48.64-.4 1.08-.24.44.16 2.8 1.32 3.28 1.56.48.24.8.36.92.56.12.2.12 1.14-.28 2.26z"/>',Ht='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="100%" height="100%"><rect x="0" y="0" width="32" height="32" rx="9" fill="#25D366"/>'+F+"</svg>",jt='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="100%" height="100%"><circle cx="16" cy="16" r="16" fill="#25D366"/>'+F+"</svg>",Yt='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="100%" height="100%"><circle cx="16" cy="16" r="16" fill="#1a1a1a"/>'+F+"</svg>",k={solid:M,roundedSquare:Ht,flat:jt,mono:Yt};var W="1.2.0",yt="whatsapp-desktop.png",Zt="whatsapp-mobile.png",I=class{constructor(){this.config=null;this.location={country:null,state:null,city:null,ip:null};this.phone=null;this.matched=null;this.el=null;this.ready=!1;this.listeners={};this.geoPromise=null;this.pillExpanded=!1;this.initGen=0}emit(e,n){let o=this.config,i="on"+e.charAt(0).toUpperCase()+e.slice(1);try{let s=o==null?void 0:o[i];typeof s=="function"&&s(n)}catch(s){m(o,"error",`Error in ${String(i)} handler`,s)}let a=this.listeners[e]||[];for(let s of a)try{s(n)}catch(r){m(o,"error",`Error in listener for ${e}`,r)}try{document.dispatchEvent(new CustomEvent(`whatsappfloating:${e}`,{detail:n}))}catch(s){}}on(e,n){var o;return this.listeners[e]||(this.listeners[e]=[]),(o=this.listeners[e])==null||o.push(n),this}off(e,n){let o=this.listeners[e];return o?(this.listeners[e]=o.filter(i=>i!==n),this):this}buildContext(){let e=X(),n={};for(let o in e)/^utm_/i.test(o)&&(n[o]=e[o]);return{language:et(),referrer:document.referrer||"",domain:window.location.hostname||"",page:window.location.pathname||"",utm:n,query:e,cookies:document.cookie,localStorage:A("localStorage"),sessionStorage:A("sessionStorage"),continent:null}}async init(e){let n={debug:!1,production:!0,mobileBreakpoint:768,cacheLocation:!1,analytics:!0,lazyLoad:!1},o=++this.initGen,i=R(n,e||{});this.config=i,i=await this.loadRemoteConfig(i),this.config=i;let a=async()=>{let s=this.buildContext(),r=await it(i);if(o!==this.initGen)return{phone:null,matched:"Fallback",rule:null};this.location=r,s.continent=nt(r.country),this.emit("location",r);let c=gt(i,r,s);return this.phone=c.phone,this.matched=c.matched,this.ruleImages=c.images,this.ruleMessage=ht(i,r,s),this.rulePillText=dt(i,r,s),this.printConsole(r,c),this.emit("phoneSelected",{phone:c.phone,matched:c.matched}),this.render(),this.ready=!0,this.emit("ready",{phone:this.phone,location:this.location,matched:this.matched}),c};return i.lazyLoad?(this.geoPromise=new Promise(s=>{document.readyState==="complete"?setTimeout(()=>a().then(s),0):window.addEventListener("load",()=>a().then(s))}),null):(this.geoPromise=a(),this.geoPromise)}async loadRemoteConfig(e){if(!e.remoteConfigUrl)return e;try{let o=await(await fetch(e.remoteConfigUrl)).json();return R(e,o)}catch(n){return m(e,"warn","Failed to load remote config",n),e}}printConsole(e,n){let o=this.config;!o||o.production&&!o.debug||console.log(`%c[WhatsAppFloating]%c Country: ${e.country||"-"} State: ${e.state||"-"} City: ${e.city||"-"} Matched: ${n.matched} Phone: ${n.phone||"-"}`,"color:#25D366;font-weight:bold;","color:inherit;")}resolveImages(){var i,a,s,r,c,g;let e=this.config;if(!e)return{};e.assetsBaseUrl&&/\.(png|jpe?g|gif|webp|svg)$/i.test(e.assetsBaseUrl)&&m(e,"warn",`assetsBaseUrl "${e.assetsBaseUrl}" looks like a file, not a folder \u2014 it will have "/${yt}" appended to it, which is almost certainly not what you want. Use config.images.desktop/mobile instead to point directly at a specific image file.`);let n=e.assetsBaseUrl?e.assetsBaseUrl.replace(/\/+$/,""):"",o=n?{desktop:`${n}/${yt}`,mobile:`${n}/${Zt}`}:{};return{desktop:((i=this.manualImages)==null?void 0:i.desktop)||((a=this.ruleImages)==null?void 0:a.desktop)||((s=e.images)==null?void 0:s.desktop)||o.desktop,mobile:((r=this.manualImages)==null?void 0:r.mobile)||((c=this.ruleImages)==null?void 0:c.mobile)||((g=e.images)==null?void 0:g.mobile)||o.mobile}}resolveImage(){let e=this.config;if(!e)return;let n=this.resolveImages();return tt(e)?n.mobile||n.desktop:n.desktop||n.mobile}applyMessagePlaceholders(e){var s,r,c,g,l;let n=this.location,o=(s=n.ip)!=null?s:"",i="";if(o&&typeof btoa=="function")try{i=btoa(o)}catch(f){i=""}let a={country:(r=n.country)!=null?r:"",state:(c=n.state)!=null?c:"",region:(g=n.state)!=null?g:"",city:(l=n.city)!=null?l:"",ip:o,ip_base64:i};return e.replace(/\{\{\s*(\w+)\s*\}\}/g,(f,d)=>{let v=a[d.toLowerCase()];return v!==void 0?v:f})}buildLink(){let e=this.config,n=(this.phone||"").replace(/\D/g,""),o=this.applyMessagePlaceholders(this.ruleMessage||(e==null?void 0:e.message)||""),i="";if(e!=null&&e.appendUtmToMessage){let a=this.buildContext(),s=Object.keys(a.utm).map(r=>`${r}=${a.utm[r]}`);s.length&&(i=` [${s.join(", ")}]`)}return`https://wa.me/${n}?text=${encodeURIComponent(o+i)}`}preloadImage(e){try{if(document.querySelector(`link[rel="preload"][href="${e}"]`))return;let n=document.createElement("link");n.rel="preload",n.as="image",n.href=e,document.head.appendChild(n)}catch(n){}}render(){var s,r,c,g;let e=this.config;if(!e)return;if(!this.phone){m(e,"warn","No phone number resolved; button will not be rendered");return}this.destroy(!0),wt(e),this.pillExpanded=!1;let n=!!((s=e.pill)!=null&&s.text),o=this.rulePillText||((r=e.pill)==null?void 0:r.text)||"",i=n?void 0:this.resolveImage();i&&this.preloadImage(i);let a=document.createElement("a");if(a.className=n?"wa-floating-btn wa-floating-pill":"wa-floating-btn",a.setAttribute("href",this.buildLink()),a.setAttribute("target","_blank"),a.setAttribute("rel","noopener noreferrer"),a.setAttribute("aria-label",e.ariaLabel||o||"WhatsApp"),n){let l=document.createElement("span");l.className="wa-floating-pill-icon",l.innerHTML=k[(c=e.pill)==null?void 0:c.icon]||k.solid;let f=l.querySelector("svg");if(f){f.removeAttribute("width"),f.removeAttribute("height");let v=f.firstElementChild;v&&v.setAttribute("fill","transparent");let u=f.querySelectorAll("path"),y=u[u.length-1];y&&y.setAttribute("fill",((g=e.pill)==null?void 0:g.textColor)||"#ffffff")}let d=document.createElement("span");d.className="wa-floating-pill-text",d.textContent=o,a.appendChild(l),a.appendChild(d)}else if(i){let l=document.createElement("img");l.loading="eager",l.decoding="async",l.fetchPriority="high",l.alt=e.imageAlt||"WhatsApp",l.src=i,a.appendChild(l)}else{a.innerHTML=e.icon||k[e.iconVariant]||M;let l=a.querySelector("svg");l&&(l.removeAttribute("width"),l.removeAttribute("height"))}a.addEventListener("click",l=>{var f;if(n&&((f=e.pill)==null?void 0:f.expand)==="click"&&!this.pillExpanded){l.preventDefault(),this.pillExpanded=!0,a.classList.add("wa-floating-expanded");return}this.emit("open",{phone:this.phone}),S(e,"open",{phone:this.phone,matched:this.matched})}),document.body.appendChild(a),this.el=a,requestAnimationFrame(()=>{requestAnimationFrame(()=>{var l;(l=this.el)==null||l.classList.add("wa-floating-visible")})}),S(e,"render",{phone:this.phone,matched:this.matched})}reload(e){let n=e&&this.config?R(this.config,e):this.config;return this.init(n)}open(){if(!this.phone)return m(this.config,"warn","Cannot open: no phone resolved yet"),this;let e=this.buildLink();return window.open(e,"_blank","noopener,noreferrer"),this.emit("open",{phone:this.phone}),this.config&&S(this.config,"open",{phone:this.phone,matched:this.matched}),this}close(){var e;return(e=this.el)==null||e.classList.remove("wa-floating-visible"),this.emit("close",{phone:this.phone}),this}destroy(e=!1){var n;return(n=this.el)!=null&&n.parentNode&&this.el.parentNode.removeChild(this.el),this.el=null,e||(this.ready=!1),this}setPhone(e){return this.phone=e,this.matched="Manual",this.el&&this.render(),this.emit("phoneSelected",{phone:e,matched:"Manual"}),this}setImages(e){return this.manualImages=b(b({},this.manualImages),e),this.el&&this.render(),this}getImages(){return this.resolveImages()}getPhone(){return this.phone}getLocation(){return this.location}getMatched(){return this.matched}isReady(){return this.ready}getVersion(){return W}};var w=new I,qt={version:W,init:t=>w.init(t),reload:t=>w.reload(t),open:()=>w.open(),close:()=>w.close(),destroy:()=>w.destroy(),getPhone:()=>w.getPhone(),getLocation:()=>w.getLocation(),getMatched:()=>w.getMatched(),setPhone:t=>w.setPhone(t),setImages:t=>w.setImages(t),getImages:()=>w.getImages(),isReady:()=>w.isReady(),on:(t,e)=>w.on(t,e),off:(t,e)=>w.off(t,e)};function N(){typeof window!="undefined"&&window.WhatsAppFloatingConfig&&w.init(window.WhatsAppFloatingConfig)}var bt=qt;typeof window!="undefined"&&(window.WhatsAppFloating=bt,document.readyState==="loading"?document.addEventListener("DOMContentLoaded",N):N());})(); // WhatsApp Floating — https://github.com/dansp89/whatsapp-floating //# sourceMappingURL=whatsapp-floating.min.js.map