(function(h,p){typeof exports=="object"&&typeof module<"u"?module.exports=p():typeof define=="function"&&define.amd?define(p):(h=typeof globalThis<"u"?globalThis:h||self,h.AnalyzeWiz=p())})(this,function(){"use strict";let h;const p=new Uint8Array(16);function A(){if(!h&&(h=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!h))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return h(p)}const o=[];for(let e=0;e<256;++e)o.push((e+256).toString(16).slice(1));function k(e,t=0){return o[e[t+0]]+o[e[t+1]]+o[e[t+2]]+o[e[t+3]]+"-"+o[e[t+4]]+o[e[t+5]]+"-"+o[e[t+6]]+o[e[t+7]]+"-"+o[e[t+8]]+o[e[t+9]]+"-"+o[e[t+10]]+o[e[t+11]]+o[e[t+12]]+o[e[t+13]]+o[e[t+14]]+o[e[t+15]]}const f={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function S(e,t,n){if(f.randomUUID&&!t&&!e)return f.randomUUID();e=e||{};const i=e.random||(e.rng||A)();if(i[6]=i[6]&15|64,i[8]=i[8]&63|128,t){n=n||0;for(let s=0;s<16;++s)t[n+s]=i[s];return t}return k(i)}let m="";function T(e){m=e}async function L(e){if(!e.ok){let t="Something went wrong";try{t=(await e.json()).message}catch{}throw new Error(t)}return e}async function y(e,t){if(!m)throw new Error("Organize Token must be set");const n={"x-analyze-token":m,"Content-Type":"application/json"};try{const i=await fetch(e,{...t,headers:n});return i.ok?i.json():L(i)}catch{throw new Error("Network error")}}const E={get:(e,t)=>y(`${e}`,{...t,method:"GET"}),post:(e,t,n)=>y(`${e}`,{...n,method:"POST",body:JSON.stringify(t)}),put:(e,t,n)=>y(`${e}`,{...n,method:"PUT",body:JSON.stringify(t)}),delete:(e,t)=>y(`${e}`,{...t,method:"DELETE"})},v=class _{constructor({name:t,selector:n,data:i,handler:s}){this._retryCount=0,this._maxRetryCount=20,this._retryTimeout=500,this._timer=null,this._setupEvent=()=>{const{selector:a,name:c}=this;let r=null;switch(a){case"html":r=document.documentElement;break;case"body":r=document.body;break;case"document":r=document;break;case"window":r=window;break;default:r=document.querySelectorAll(a)}if(!r||r instanceof NodeList&&r.length===0){this._retryCount{w.addEventListener(c,this._eventHandler)}),()=>{d.forEach(w=>{w.removeEventListener(c,this._eventHandler)})}}else{const d=r;return d.addEventListener(c,this._eventHandler),()=>{d.removeEventListener(c,this._eventHandler)}}},this._eventHandler=a=>{var w,b;const{data:c,_customHandler:r}=this;let d;if(typeof r=="function"){const g=r(a);d={subject:((w=_.user)==null?void 0:w.id)??"unlogin",action_type:g.type,resource_type:g.resourceType,resource_id:g.resourceId}}else{if(!c){console.error("[AnalyzeWiz] data is required");return}d={subject:((b=_.user)==null?void 0:b.id)??"unlogin",action_type:c.type,resource_type:c.resourceType,resource_id:c.resourceId}}return E.post(_.api,d)},this.name=t,this.id=S(),this.selector=n,this.data=i,this._off=this._setupEvent(),this._customHandler=s}refresh(){var t;(t=this._off)==null||t.call(this),this._timer&&clearTimeout(this._timer),this._retryCount=0,this._off=this._setupEvent()}};v.api="https://api.analyzewiz.com/action";let l=v;const z=class u{constructor({actions:t,...n}){if(this.options={delay:0,api:"https://analyze-test.shlab.tech/api/v1/log/create",organize:"",token:""},this._originalPushState=window.history.pushState,this._originalReplaceState=window.history.replaceState,this._originalGo=window.history.go,this._originalBack=window.history.back,this._originalForward=window.history.forward,this.actions=new Map,this._refreshActions=()=>{this.actions.forEach(i=>{i.refresh()})},!n.organize)throw new Error("organize is required");if(!n.token)throw new Error("token is required");T(n.token),this.options={...this.options,...n},l.api=this.options.api,u.api=this.options.api,this._actions=t,this._setupActions(),this._rewriteHistory()}static init(t){if(u.instance)return console.warn("AnalyzeWiz has been initialized"),u.instance;u.instance=new u(t)}_getLoadEvents(){const{_actions:t}=this;return t.filter(n=>["load","DOMContentLoaded"].includes(n.name))}_rewriteHistory(){window.history.pushState=this._pushState.bind(this),window.history.replaceState=this._replaceState.bind(this),window.history.back=this._back.bind(this),window.history.forward=this._forward.bind(this),window.history.go=this._go.bind(this)}_pushState(t,n,i){this._originalPushState.apply(window.history,[t,n,i]),window.dispatchEvent(new Event("locationchange")),this._refreshActions()}_replaceState(t,n,i){this._originalReplaceState.apply(window.history,[t,n,i]),window.dispatchEvent(new Event("locationchange")),this._refreshActions()}_back(){this._originalBack.apply(window.history),window.dispatchEvent(new Event("locationchange")),this._refreshActions()}_forward(){this._originalForward.apply(window.history),window.dispatchEvent(new Event("locationchange")),this._refreshActions()}_go(t){this._originalGo.apply(window.history,[t]),window.dispatchEvent(new Event("locationchange")),this._refreshActions()}_setupActions(){const{delay:t}=this.options,n=this._getLoadEvents();if(n.length)for(const i of n){const s=new l(i);this.actions.set(s.id,s)}window.addEventListener("DOMContentLoaded",()=>{setTimeout(()=>{const i=this._actions.filter(s=>!["load","DOMContentLoaded"].includes(s.name));for(const s of i){const a=new l(s);this.actions.set(a.id,a)}},t)}),window.addEventListener("hashchange",this._refreshActions.bind(this)),window.addEventListener("popstate",()=>{window.dispatchEvent(new Event("locationchange")),this._refreshActions()})}add(t){const{name:n,selector:i,data:s}=t,a=new l({name:n,selector:i,data:s});this.actions.set(a.id,a)}static setUser(t){if(!t.id)throw new Error("user.id is required");if(!t.name)throw new Error("user.name is required");l.user=t,u.user=t}static dispatch(t){var n;return E.post(u.api,{subject:((n=l.user)==null?void 0:n.id)??"unlogin",action_type:t.type,resource_type:t.resourceType,resource_id:t.resourceId})}};return z.api="https://api.analyzewiz.com/action",z});