"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),K=require("./use-auth.cjs"),P=require("../provider/config-provider.cjs");function A(){const{session:e,sdk:i,createSession:r,reload:t,userType:l}=K.useAuth(),{apiUrl:f,publishableKey:d}=P.useConfig(),[o,h]=n.useState([]),[w,a]=n.useState(!1),[E,S]=n.useState(null),c=n.useCallback(s=>{const y={code:s.code||"UNKNOWN_ERROR",message:s.message||"An unknown error occurred",details:s.details,field:s.field};throw S(y),y},[]),v=n.useCallback(async()=>{try{a(!0),S(null);const s=await i.session.listSessions({fields:[]});h(s.data)}catch(s){console.error("Failed to load sessions:",s),S({code:"SESSIONS_LOAD_FAILED",message:"Failed to load sessions"})}finally{a(!1)}},[i.session]);n.useEffect(()=>{v()},[v]);const k=n.useCallback(async s=>r(s),[r]),M=n.useCallback(async s=>{try{a(!0),S(null),i.session.activeSession=s,await t(),await v()}catch(y){c(y)}finally{a(!1)}},[i.session,t,v,c]),x=n.useCallback(async()=>{try{a(!0),S(null);const s=await i.session.refreshSession();return await t(),s}catch(s){return c(s),null}finally{a(!1)}},[i.session,t,c]),b=n.useCallback(async s=>{try{a(!0),S(null),await i.session.revokeSession(s),s===e?.id?await t():await v()}catch(y){c(y)}finally{a(!1)}},[i.session,e?.id,t,v,c]),C=n.useCallback(async(s=!1)=>{try{a(!0),S(null),await i.session.revokeAllSessions({exceptCurrent:s}),s?await v():await t()}catch(y){c(y)}finally{a(!1)}},[i.session,t,v,c]),D=n.useCallback(async()=>{try{a(!0),S(null),await t()}catch(s){c(s)}finally{a(!1)}},[i.session,t,c]),m=n.useMemo(()=>e?.id||null,[e]),I=n.useMemo(()=>e?.accessToken||null,[e]),u=n.useMemo(()=>e?.expiresAt?new Date(e.expiresAt):null,[e]),T=n.useMemo(()=>e?.lastActiveAt?new Date(e.lastActiveAt):null,[e]),U=n.useMemo(()=>!!e&&!e.expired,[e]),g=n.useMemo(()=>u?u.getTime()<=Date.now():!1,[u]),L=n.useMemo(()=>{if(!u)return!1;const s=Date.now()+5*60*1e3;return u.getTime()<=s&&!g},[u,g]),O=n.useMemo(()=>{if(!u)return null;const s=u.getTime()-Date.now();return Math.max(0,Math.floor(s/6e4))},[u]),p=n.useMemo(()=>e?.deviceInfo?{userAgent:e.deviceInfo.userAgent||"",browser:e.deviceInfo.browser||"Unknown",os:e.deviceInfo.os||"Unknown",device:e.deviceInfo.device||"Unknown",ipAddress:e.deviceInfo.ipAddress||"",location:e.deviceInfo.location}:null,[e]),N=n.useMemo(()=>!e||!p?!1:typeof navigator<"u"&&p.userAgent===navigator.userAgent,[e,p]),R=n.useMemo(()=>e?.trustedDevice||!1,[e]),F=n.useMemo(()=>o.length>1,[o]),q=n.useMemo(()=>o.length,[o]),_=n.useMemo(()=>o.filter(s=>s.id!==m),[o,m]);return{session:e,sessions:o,isLoaded:!!e,isLoading:w,error:E,createSession:k,setActiveSession:M,refreshSession:x,revokeSession:b,revokeAllSessions:C,endSession:D,sessionId:m,sessionToken:I,expiresAt:u,lastActiveAt:T,isActive:U,isExpired:g,isExpiring:L,timeUntilExpiry:O,deviceInfo:p,isCurrentDevice:N,isTrustedDevice:R,hasMultipleSessions:F,sessionCount:q,otherSessions:_}}function j(){const{isActive:e,isExpired:i,isExpiring:r,timeUntilExpiry:t,expiresAt:l,refreshSession:f}=A();return{isActive:e,isExpired:i,isExpiring:r,timeUntilExpiry:t,expiresAt:l,refreshSession:f,status:i?"expired":r?"expiring":e?"active":"inactive"}}function W(){const{sessions:e,sessionCount:i,otherSessions:r,hasMultipleSessions:t,revokeSession:l,revokeAllSessions:f,setActiveSession:d,isLoading:o,error:h}=A();return{sessions:e,sessionCount:i,otherSessions:r,hasMultipleSessions:t,revokeSession:l,revokeAllSessions:f,setActiveSession:d,isLoading:o,error:h,revokeAllOthers:()=>f(!0)}}function z(){const{deviceInfo:e,isCurrentDevice:i,isTrustedDevice:r,sessionId:t,lastActiveAt:l}=A();return{deviceInfo:e,isCurrentDevice:i,isTrustedDevice:r,sessionId:t,lastActiveAt:l,isSecure:r&&i}}function B(e={}){const{autoRefresh:i=!1,refreshThreshold:r=5,onExpiry:t,onExpiring:l}=e,{isExpired:f,isExpiring:d,timeUntilExpiry:o,refreshSession:h}=A();return n.useEffect(()=>{i&&d&&o&&o<=r&&h().catch(console.error)},[i,d,o,r,h]),n.useEffect(()=>{f&&t?.()},[f,t]),n.useEffect(()=>{d&&l?.()},[d,l]),{isExpired:f,isExpiring:d,timeUntilExpiry:o,refreshSession:h,autoRefresh:i}}exports.useMultiSession=W;exports.useSession=A;exports.useSessionExpiry=B;exports.useSessionSecurity=z;exports.useSessionStatus=j;
//# sourceMappingURL=use-session.cjs.map
