// @bun
var{defineProperty:x,getOwnPropertyNames:E,getOwnPropertyDescriptor:W}=Object,A=Object.prototype.hasOwnProperty;function S(j){return this[j]}var L=(j)=>{var k=(D??=new WeakMap).get(j),z;if(k)return k;if(k=x({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var B of E(j))if(!A.call(k,B))x(k,B,{get:S.bind(j,B),enumerable:!(z=W(j,B))||z.enumerable})}return D.set(j,k),k},D;var y=(j)=>j;function b(j,k){this[j]=y.bind(null,k)}var p=(j,k)=>{for(var z in k)x(j,z,{get:k[z],enumerable:!0,configurable:!0,set:b.bind(k,z)})};var c=(j,k)=>()=>(j&&(k=j(j=0)),k);var w=import.meta.require;var h=["mousemove","mousedown","keydown","touchstart","scroll","wheel","resize","visibilitychange"];function C(j={}){let{timeout:k=60000,events:z=h,listenTo:B=typeof document<"u"?document:null,initialState:P=!1,onIdle:H,onActive:Q}=j,K=new Set,q={isIdle:P,lastActive:Date.now(),idleTime:0},J=null,O=null,R=null;function Z(){K.forEach((G)=>G(q))}function $(G){let M=q.isIdle;if(G&&!M)q={...q,isIdle:!0},Z(),H?.(),O=setInterval(()=>{q={...q,idleTime:Date.now()-q.lastActive},Z()},1000);else if(!G&&M){if(q={...q,isIdle:!1,lastActive:Date.now(),idleTime:0},Z(),Q?.(),O)clearInterval(O),O=null}}function X(){if(J)clearTimeout(J);if(!q.isIdle)q={...q,lastActive:Date.now()};$(!1),J=setTimeout(()=>{$(!0)},k)}function F(){X()}function N(){if(document.hidden){if(J)clearTimeout(J);J=setTimeout(()=>{$(!0)},Math.min(k,5000))}else X()}function U(){if(!B||R)return;let G=[];for(let M of z){let Y=M==="visibilitychange"?N:F;B.addEventListener(M,Y,{passive:!0}),G.push([M,Y])}X(),R=()=>{for(let[M,Y]of G)B.removeEventListener(M,Y);if(J)clearTimeout(J),J=null;if(O)clearInterval(O),O=null}}function _(){R?.(),R=null}function V(){X()}if(typeof document<"u")U();return{get:()=>q,subscribe:(G)=>{return K.add(G),G(q),()=>{if(K.delete(G),K.size===0)_()}},reset:V,isIdle:()=>q.isIdle}}function v(j=60000){let k=C({timeout:j});return{get:()=>k.get().isIdle,subscribe:(z)=>{return k.subscribe((B)=>z(B.isIdle))},reset:k.reset}}function I(){let j=C({timeout:Number.POSITIVE_INFINITY});return{get:()=>j.get().lastActive,subscribe:(k)=>{return j.subscribe((z)=>k(z.lastActive))},reset:j.reset}}function T(j,k,z={}){let{warningTime:B=30000,onWarning:P}=z,H=null,Q=!1,K=C({timeout:j-B,onIdle:()=>{if(P){Q=!0;let q=B;P(q),H=setInterval(()=>{if(q-=1000,q<=0){if(H)clearInterval(H),H=null;k()}else P(q)},1000)}else setTimeout(k,B)},onActive:()=>{if(Q){if(Q=!1,H)clearInterval(H),H=null}}});return{reset:K.reset,get:K.get,subscribe:K.subscribe}}export{I as useLastActive,v as useIdleState,C as useIdle,T as useAutoLogout};
