(function(F,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(F=typeof globalThis<"u"?globalThis:F||self,T(F.Devqaly={}))})(this,function(F){"use strict";class T extends Error{constructor(e){super(e)}}var A=(n=>(n.NETWORK_REQUEST="App\\Models\\Session\\Event\\EventNetworkRequest",n.CHANGED_URL="App\\Models\\Session\\Event\\EventUrlChanged",n.ELEMENT_CLICKED="App\\Models\\Session\\Event\\EventElementClick",n.SCROLL="App\\Models\\Session\\Event\\EventElementScroll",n.RESIZE_SCREEN="App\\Models\\Session\\Event\\EventResizeScreen",n.DATABASE_TRANSACTION="App\\Models\\Session\\Event\\EventDatabaseTransaction",n.LOG="App\\Models\\Session\\Event\\EventLog",n.CUSTOM_EVENT="App\\Models\\Session\\Event\\EventCustomEvent",n))(A||{}),j=(n=>(n.EMERGENCY="emergency",n.ALERT="alert",n.CRITICAL="critical",n.ERROR="error",n.WARNING="warning",n.NOTICE="notice",n.INFORMATIONAL="informational",n.DEBUG="debug",n))(j||{});function k(n,e){return{source:"browser-sdk",clientUtcEventCreatedAt:new Date().toISOString(),type:n,...e}}const I={log:console.log,error:console.error,warn:console.warn,debug:console.debug},xe=typeof window<"u"?window.onunhandledrejection:()=>{},Ee=typeof window<"u"?window.onerror:()=>{};class Ce{constructor(e,r){this.queue=e,this.config=r}isEnabled(){return!this.config.disabledListeners.log}registerListeners(){this.registerConsoleLogListener(),this.registerConsoleErrorListener(),this.registerConsoleWarningListener(),this.registerConsoleDebugListener(),this.registerOnUnhandledRejectionListener(),this.registerWindowOnError()}enqueConsoleLog(e,r){const a=k(A.LOG,{level:e,log:r.reduce((l,d)=>l+=JSON.stringify(d),"")});this.queue.enqueue(a)}enqueueUhandled(e){this.queue.enqueue(k(A.LOG,{level:j.CRITICAL,log:e}))}registerConsoleLogListener(){console.log=(...e)=>{I.log(...e),this.enqueConsoleLog(j.INFORMATIONAL,e)}}registerConsoleErrorListener(){console.error=(...e)=>{I.error(...e),this.enqueConsoleLog(j.ERROR,e)}}registerConsoleWarningListener(){console.warn=(...e)=>{I.warn(...e),this.enqueConsoleLog(j.WARNING,e)}}registerConsoleDebugListener(){console.debug=(...e)=>{I.debug(...e),this.enqueConsoleLog(j.DEBUG,e)}}registerOnUnhandledRejectionListener(){window.onunhandledrejection=e=>{let r=`Unhandled Rejection `;e.reason&&(r+=`Reason: ${e.reason}`),e.type&&(r+=`Type: ${e.type}`),this.enqueueUhandled(r)}}registerWindowOnError(){window.onerror=(e,r,a,l,d)=>{if(d&&d instanceof T)return;let h=`Uncaught exception `;r&&(h+=`Source: ${r} `),a!==void 0&&(h+=`Line: ${a} `),l!==void 0&&(h+=`Column number: ${l} `),d&&(h+=`Error name: ${d.name} `,h+=`Error message: ${d.message} `,d.stack&&(h+=`Error message: ${d.stack} `)),this.enqueueUhandled(h)}}unregisterListeners(){console.log=I.log,console.error=I.error,console.warn=I.warn,console.debug=I.debug,window.onunhandledrejection=xe,window.onerror=Ee}}function Oe(n){return n?!!n.innerText:!1}class Re{constructor(e,r){this.queue=e,this.config=r,this.onClick=a=>{const l=a.target;let d=l==null?void 0:l.className;typeof d=="object"&&(d=JSON.stringify(d));const h=k(A.ELEMENT_CLICKED,{positionX:a.clientX,positionY:a.clientY,innerText:Oe(l)?l.innerText:void 0,elementClasses:d});this.queue.enqueue(h)}}isEnabled(){return!this.config.disabledListeners.click}registerListeners(){document.addEventListener("click",this.onClick)}unregisterListeners(){document.removeEventListener("click",this.onClick)}}var $=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function he(n){return n&&n.__esModule&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n}var Te="Expected a function",fe=0/0,Ie="[object Symbol]",Me=/^\s+|\s+$/g,Ue=/^[-+]0x[0-9a-f]+$/i,Le=/^0b[01]+$/i,Pe=/^0o[0-7]+$/i,Ae=parseInt,ke=typeof $=="object"&&$&&$.Object===Object&&$,Ne=typeof self=="object"&&self&&self.Object===Object&&self,Be=ke||Ne||Function("return this")(),We=Object.prototype,qe=We.toString,De=Math.max,Fe=Math.min,re=function(){return Be.Date.now()};function $e(n,e,r){var a,l,d,h,x,E,N=0,Y=!1,B=!1,H=!0;if(typeof n!="function")throw new TypeError(Te);e=ue(e)||0,se(r)&&(Y=!!r.leading,B="maxWait"in r,d=B?De(ue(r.maxWait)||0,e):d,H="trailing"in r?!!r.trailing:H);function V(S){var L=a,i=l;return a=l=void 0,N=S,h=n.apply(i,L),h}function ae(S){return N=S,x=setTimeout(M,e),Y?V(S):h}function K(S){var L=S-E,i=S-N,f=e-L;return B?Fe(f,d-i):f}function G(S){var L=S-E,i=S-N;return E===void 0||L>=e||L<0||B&&i>=d}function M(){var S=re();if(G(S))return Z(S);x=setTimeout(M,K(S))}function Z(S){return x=void 0,H&&a?V(S):(a=l=void 0,h)}function U(){x!==void 0&&clearTimeout(x),N=0,a=E=l=x=void 0}function W(){return x===void 0?h:Z(re())}function _(){var S=re(),L=G(S);if(a=arguments,l=this,E=S,L){if(x===void 0)return ae(E);if(B)return x=setTimeout(M,e),V(E)}return x===void 0&&(x=setTimeout(M,e)),h}return _.cancel=U,_.flush=W,_}function se(n){var e=typeof n;return!!n&&(e=="object"||e=="function")}function Ge(n){return!!n&&typeof n=="object"}function _e(n){return typeof n=="symbol"||Ge(n)&&qe.call(n)==Ie}function ue(n){if(typeof n=="number")return n;if(_e(n))return fe;if(se(n)){var e=typeof n.valueOf=="function"?n.valueOf():n;n=se(e)?e+"":e}if(typeof n!="string")return n===0?n:+n;n=n.replace(Me,"");var r=Le.test(n);return r||Pe.test(n)?Ae(n.slice(2),r?2:8):Ue.test(n)?fe:+n}var je=$e;const pe=he(je);class He{constructor(e,r){this.queue=e,this.config=r,this._onResizeScreen=a=>{const l=a.target;if(a.target===null)return;let d=0,h=0;l instanceof Window&&(d=l.innerHeight,h=l.innerWidth),this.queue.enqueue(k(A.RESIZE_SCREEN,{innerHeight:d,innerWidth:h}))},this.onResizeScreen=pe(this._onResizeScreen,1e3)}isEnabled(){return!this.config.disabledListeners.resize}registerListeners(){window.addEventListener("resize",this.onResizeScreen)}unregisterListeners(){window.removeEventListener("resize",this.onResizeScreen)}}class Ke{constructor(e,r){this.queue=e,this.config=r,this._onScroll=a=>{const l=a.target;if(!l)return;let d=0,h=0,x=0,E=0;l instanceof Document?(d=l.documentElement.scrollHeight,h=l.documentElement.scrollWidth,x=l.documentElement.scrollLeft,E=l.documentElement.scrollTop):l instanceof Element&&(d=l.scrollHeight,h=l.scrollWidth,x=l.scrollLeft,E=l.scrollTop),this.queue.enqueue(k(A.SCROLL,{scrollHeight:d,scrollLeft:x,scrollTop:E,scrollWidth:h}))},this.onScroll=pe(this._onScroll,200)}isEnabled(){return!this.config.disabledListeners.scroll}registerListeners(){document.addEventListener("scroll",this.onScroll)}unregisterListeners(){document.removeEventListener("scroll",this.onScroll)}}const Ve="_devqaly-current-session";class ze{constructor(e,r){this.queue=e,this.config=r,this.previousUrl=""}isEnabled(){return!this.config.disabledListeners.urlChange}registerListeners(){this.observer=new MutationObserver(()=>{location.href!==this.previousUrl&&(this.previousUrl=location.href,this.queue.enqueue(k(A.CHANGED_URL,{toUrl:location.href})))});const e={subtree:!0,childList:!0};this.observer.observe(document,e)}unregisterListeners(){var e;(e=this.observer)==null||e.disconnect()}}let X;const Xe=new Uint8Array(16);function Je(){if(!X&&(X=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!X))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return X(Xe)}const y=[];for(let n=0;n<256;++n)y.push((n+256).toString(16).slice(1));function Qe(n,e=0){return(y[n[e+0]]+y[n[e+1]]+y[n[e+2]]+y[n[e+3]]+"-"+y[n[e+4]]+y[n[e+5]]+"-"+y[n[e+6]]+y[n[e+7]]+"-"+y[n[e+8]]+y[n[e+9]]+"-"+y[n[e+10]]+y[n[e+11]]+y[n[e+12]]+y[n[e+13]]+y[n[e+14]]+y[n[e+15]]).toLowerCase()}const me={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function Ye(n,e,r){if(me.randomUUID&&!e&&!n)return me.randomUUID();n=n||{};const a=n.random||(n.rng||Je)();if(a[6]=a[6]&15|64,a[8]=a[8]&63|128,e){r=r||0;for(let l=0;l<16;++l)e[r+l]=a[l];return e}return Qe(a)}const J=typeof window<"u"?window.fetch:()=>{};class Ze{constructor(e,r){this.queue=e,this.config=r}isEnabled(){return!this.config.disabledListeners.network}formatHeaders(e){if(e instanceof Headers){const r={},a=e.keys();let l=a.next();for(;l.value;)r[l.value]=e.get(l.value),l=a.next();return r}return typeof e=="object"?e:""}getUrl(e){return e instanceof URL?e.href:e instanceof Request?e.url:e}async getBody(e){return e instanceof ArrayBuffer?"":e instanceof ReadableStream?"":ArrayBuffer.isView(e)?"":e instanceof Blob?"":e instanceof File?"":e instanceof URLSearchParams?await new Response(e).text():e instanceof FormData?await new Response(e).text():e??""}getDevqalyHeaders(e){if(!this.config.session)throw new T("No session!");return{"x-devqaly-session-id":this.config.session.id,"x-devqaly-session-secret-token":this.config.session.token,"x-devqaly-request-id":e}}getFetchParams(e,r,a){return typeof e!="string"&&e instanceof Request?[e,a]:a?[e,{...a,headers:{...a.headers,...this.getDevqalyHeaders(r)}}]:[e,{headers:this.getDevqalyHeaders(r)}]}async listenFetch(){typeof fetch=="function"&&(window.fetch=async(e,r)=>{r&&r.signal&&(r.signal=void 0);const a=Ye(),l=await J(...this.getFetchParams(e,a,r)),d={url:this.getUrl(e),method:r?r.method??"GET":"GET",request:{body:r?await this.getBody(r.body):"",headers:r!=null&&r.headers?JSON.stringify(this.formatHeaders(r.headers)):""},response:{body:"",headers:l.headers?JSON.stringify(this.formatHeaders(l.headers)):"",status:l.status},requestId:a};try{l.clone().text().then(h=>{d.response.body=h,this.queue.enqueue(k(A.NETWORK_REQUEST,{method:d.method,url:d.url,requestBody:d.request.body,responseBody:d.response.body,requestHeaders:d.request.headers,responseHeaders:d.response.headers,responseStatus:d.response.status,requestId:d.requestId}))}).catch(h=>I.error(h))}catch(h){I.error(`Error reading response: ${h}`)}return l})}registerListeners(){this.listenFetch()}unregisterListeners(){window.fetch=J}}const et=1;class tt{constructor(e){this.events=[],this.listeners=e.listeners.map(r=>new r(this,e.config)),this.onEvents=e.onEvents}startListening(){this.listeners.filter(e=>e.isEnabled()).forEach(e=>e.registerListeners())}stopListening(){this.flush(),this.listeners.filter(e=>e.isEnabled()).forEach(e=>e.unregisterListeners())}enqueue(e){this.events.push(e),this.events.length>=et&&this.flush()}async flush(){this.events.length&&(this.onEvents(this.events),this.events=[])}}function it(n){return{save:e=>{typeof localStorage<"u"&&localStorage.setItem(n,JSON.stringify(e))},hasValue:()=>{if(typeof localStorage<"u")return!!localStorage.getItem(n)},clear:()=>{typeof localStorage<"u"&&localStorage.removeItem(n)},load:()=>{if(typeof localStorage<"u"){const e=localStorage.getItem(n);return e?JSON.parse(e):void 0}}}}function nt(){return`