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