import{a as r,j as s}from"./vendor-react-YMhRUmcb.js";import{C as N}from"./confirmation-dialog-O1mUMh9e.js";import{a as k,p as b,l as y,B as R,L as A,s as S,C as D,b as E,c as I,f as M,e as F}from"./index-D2uzyr9g.js";import{l as O,az as U,ap as B,T as $,m as z,aA as H,aB as G,au as Y,O as q}from"./vendor-icons-DINCNwpk.js";import"./vendor-fFnFILYR.js";import"./vendor-router-DX94Iu-2.js";import"./vendor-tanstack-CoFNL2zy.js";import"./vendor-utils-4BWoYre8.js";function L(e){const i=e.toLowerCase(),t={deviceType:"unknown",os:"Unknown",browser:"Unknown"};if(i.includes("mobile")||i.includes("android")||i.includes("iphone")?t.deviceType="mobile":i.includes("tablet")||i.includes("ipad")?t.deviceType="tablet":t.deviceType="desktop",i.includes("windows")){t.os="Windows";const a=i.match(/windows nt (\d+\.\d+)/);if(a){const n=a[1];n==="10.0"?t.osVersion="10":n==="6.3"?t.osVersion="8.1":n==="6.2"?t.osVersion="8":n==="6.1"?t.osVersion="7":t.osVersion=n}}else if(i.includes("mac os x")||i.includes("macintosh")){t.os="macOS";const a=i.match(/mac os x (\d+[._]\d+)/);a&&(t.osVersion=a[1].replace("_","."))}else if(i.includes("linux"))t.os="Linux";else if(i.includes("android")){t.os="Android";const a=i.match(/android (\d+\.\d+)/);a&&(t.osVersion=a[1])}else if(i.includes("ios")||i.includes("iphone")||i.includes("ipad")){t.os="iOS";const a=i.match(/os (\d+[._]\d+)/);a&&(t.osVersion=a[1].replace("_","."))}if(i.includes("edg/")){t.browser="Microsoft Edge";const a=i.match(/edg\/(\d+\.\d+)/);a&&(t.browserVersion=a[1])}else if(i.includes("chrome/")&&!i.includes("edg/")){t.browser="Chrome";const a=i.match(/chrome\/(\d+\.\d+)/);a&&(t.browserVersion=a[1])}else if(i.includes("firefox/")){t.browser="Firefox";const a=i.match(/firefox\/(\d+\.\d+)/);a&&(t.browserVersion=a[1])}else if(i.includes("safari/")&&!i.includes("chrome/")){t.browser="Safari";const a=i.match(/version\/(\d+\.\d+)/);a&&(t.browserVersion=a[1])}else if(i.includes("opera/")||i.includes("opr/")){t.browser="Opera";const a=i.match(/(?:opera|opr)\/(\d+\.\d+)/);a&&(t.browserVersion=a[1])}if(i.includes("iphone"))i.match(/iphone\s+os\s+(\d+[._]\d+)/)&&(t.deviceModel="iPhone");else if(i.includes("ipad"))t.deviceModel="iPad";else if(i.includes("android")){const a=i.match(/android.*?;\s*([^)]+)\)/);a&&(t.deviceModel=a[1].trim())}return t}function J(e){const i=[];return e.deviceModel&&i.push(e.deviceModel),i.push(e.os),e.osVersion&&i.push(e.osVersion),i.push(e.browser),e.browserVersion&&i.push(e.browserVersion),i.join(" • ")}function K(e){if(!e)return"Unknown location";const i=[];return e.city&&i.push(e.city),e.region&&i.push(e.region),e.country&&i.push(e.country),i.length>0?i.join(", "):"Unknown location"}function Q(e){return e==="127.0.0.1"||e==="localhost"||e.startsWith("192.168.")||e.startsWith("10.")||e.startsWith("172.16.")||e.startsWith("172.17.")||e.startsWith("172.18.")||e.startsWith("172.19.")||e.startsWith("172.20.")||e.startsWith("172.21.")||e.startsWith("172.22.")||e.startsWith("172.23.")||e.startsWith("172.24.")||e.startsWith("172.25.")||e.startsWith("172.26.")||e.startsWith("172.27.")||e.startsWith("172.28.")||e.startsWith("172.29.")||e.startsWith("172.30.")||e.startsWith("172.31.")}function X(e){const[i,t]=r.useState(e?.initialSessions??[]),[a,n]=r.useState(e?.isLoading??!0),[g,m]=r.useState(null),[p,x]=r.useState(!1),[j,c]=r.useState(null),[d,h]=r.useState(null),[w,f]=r.useState(!1),o=r.useCallback(async()=>{try{n(!0),c(null);const l=await k.get("/auth/sessions");t(l.data.sessions)}catch(l){const u=b(l);y.error("Failed to fetch sessions",{message:u.message}),c(u.message)}finally{n(!1)}},[]);r.useEffect(()=>{if(e?.initialSessions!==void 0){t(e.initialSessions),n(!1);return}if(e?.isLoading===!0){n(!0);return}o()},[o,e?.initialSessions,e?.isLoading]);const v=r.useCallback(l=>{h(l)},[]),V=r.useCallback(async()=>{if(d)try{m(d),c(null),await k.delete(`/auth/sessions/${d}`),await o(),h(null)}catch(l){const u=b(l);y.error("Failed to revoke session",{message:u.message,sessionId:d}),c(u.message)}finally{m(null)}},[d,o]),W=r.useCallback(()=>{f(!0)},[]),_=r.useCallback(async()=>{try{x(!0),c(null),await k.delete("/auth/sessions"),await o(),f(!1)}catch(l){const u=b(l);y.error("Failed to revoke sessions",{message:u.message}),c(u.message)}finally{x(!1)}},[o]),P=r.useCallback(()=>h(null),[]),T=r.useCallback(()=>f(!1),[]);return{sessions:i,loading:a,error:j,revoking:g,revokingAll:p,sessionToRevoke:d,showRevokeAllDialog:w,fetchSessions:o,handleRevokeClick:v,revokeSession:V,handleRevokeAllClick:W,revokeAllOther:_,closeRevokeDialog:P,closeRevokeAllDialog:T}}function Z(){return s.jsxs("div",{children:[s.jsx("h1",{className:"text-3xl font-bold tracking-tight",children:"Active Sessions"}),s.jsx("p",{className:"text-muted-foreground",children:"Manage your active sessions and sign out from other devices"})]})}function ee({message:e}){return s.jsx("div",{className:"bg-destructive/10 text-destructive px-4 py-4 rounded-md",children:e})}function se({disabled:e,loading:i,onClick:t}){return s.jsx("div",{className:"flex justify-end",children:s.jsx(R,{onClick:t,disabled:e,variant:"destructive",children:i?s.jsxs(s.Fragment,{children:[s.jsx(A,{className:"mr-2 h-4 w-4"}),"Revoking..."]}):s.jsxs(s.Fragment,{children:[s.jsx(O,{className:"mr-2 h-4 w-4"}),"Revoke All Other Sessions"]})})})}function ie(){return s.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"No active sessions found."})}function ae(e){switch(e){case"mobile":return s.jsx(Y,{className:"h-5 w-5"});case"tablet":return s.jsx(G,{className:"h-5 w-5"});case"desktop":return s.jsx(H,{className:"h-5 w-5"});default:return s.jsx(z,{className:"h-5 w-5"})}}function C(e){const i=new Date(e);return new Intl.DateTimeFormat("en-US",{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}).format(i)}function te({session:e,isRevoking:i,onRevokeClick:t}){const a=e.device_info??L(e.user_agent),n=e.location_info;return s.jsxs("div",{className:`flex items-center justify-between p-4 border rounded-lg ${e.is_current?"border-primary bg-primary/5":"border-border"}`,children:[s.jsxs("div",{className:"flex items-start gap-4 flex-1",children:[s.jsx("div",{className:"mt-1",children:ae(a.deviceType)}),s.jsxs("div",{className:"flex-1 space-y-2",children:[s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[s.jsx("p",{className:"font-medium",children:J(a)}),e.is_current&&s.jsx(S,{variant:"default",className:"bg-primary text-primary-foreground",children:"Current Session"}),s.jsx(S,{variant:"secondary",className:"capitalize",children:a.deviceType})]}),s.jsxs("div",{className:"flex flex-wrap gap-4 text-sm text-muted-foreground",children:[s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx(U,{className:"h-4 w-4"}),s.jsx("span",{children:e.ip_address||"Unknown IP"})]}),n&&s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx(B,{className:"h-4 w-4"}),s.jsx("span",{children:K(n)})]}),s.jsxs("div",{children:[s.jsx("span",{className:"font-medium",children:"Created:"})," ",C(e.created_at)]}),e.last_activity&&s.jsxs("div",{children:[s.jsx("span",{className:"font-medium",children:"Last activity:"})," ",C(e.last_activity)]})]}),a.browserVersion&&s.jsxs("div",{className:"text-xs text-muted-foreground",children:[a.browser," ",a.browserVersion,a.osVersion&&` • ${a.os} ${a.osVersion}`]})]})]}),!e.is_current&&s.jsx(R,{onClick:()=>t(e.id),disabled:i,variant:"destructive",size:"sm",children:i?s.jsxs(s.Fragment,{children:[s.jsx(A,{className:"mr-2 h-4 w-4"}),"Revoking..."]}):s.jsxs(s.Fragment,{children:[s.jsx($,{className:"mr-2 h-4 w-4"}),"Revoke"]})})]})}function ne({sessions:e,revoking:i,onRevokeClick:t}){return s.jsxs(D,{children:[s.jsxs(E,{children:[s.jsxs(I,{className:"flex items-center gap-2",children:[s.jsx(q,{className:"h-5 w-5"}),"Sessions (",e.length,")"]}),s.jsx(M,{children:"These are the devices where you're currently signed in"})]}),s.jsx(F,{children:e.length===0?s.jsx(ie,{}):s.jsx("div",{className:"space-y-4",children:e.map(a=>s.jsx(te,{session:a,isRevoking:i===a.id,onRevokeClick:t},a.id))})})]})}function re(){return s.jsx("div",{className:"flex items-center justify-center min-h-layout-page-sm",children:s.jsxs("div",{className:"space-y-6 w-full max-w-2xl",children:[s.jsx("div",{className:"h-8 w-64 rounded bg-muted animate-pulse"}),s.jsx("div",{className:"h-4 w-96 rounded bg-muted animate-pulse"}),s.jsx("div",{className:"flex justify-end",children:s.jsx("div",{className:"h-10 w-56 rounded bg-muted animate-pulse"})}),s.jsxs("div",{className:"rounded-lg border bg-card p-6 space-y-4",children:[s.jsx("div",{className:"h-6 w-32 rounded bg-muted animate-pulse"}),s.jsx("div",{className:"h-4 w-72 rounded bg-muted animate-pulse"}),s.jsxs("div",{className:"space-y-4 pt-4",children:[s.jsx("div",{className:"h-24 rounded-lg bg-muted animate-pulse"}),s.jsx("div",{className:"h-24 rounded-lg bg-muted animate-pulse"})]})]})]})})}function ve(e){const i=e?{initialSessions:e.initialSessions,isLoading:e.isLoading}:void 0,{sessions:t,loading:a,error:n,revoking:g,revokingAll:m,sessionToRevoke:p,showRevokeAllDialog:x,handleRevokeClick:j,revokeSession:c,handleRevokeAllClick:d,revokeAllOther:h,closeRevokeDialog:w,closeRevokeAllDialog:f}=X(i),o=r.useMemo(()=>t.map(v=>({...v,device_info:L(v.user_agent),location_info:Q(v.ip_address)?{country:"Local",region:"Network",city:"Private IP"}:null})),[t]);return a?s.jsx(re,{}):s.jsxs("div",{className:"space-y-6",children:[s.jsx(Z,{}),n&&s.jsx(ee,{message:n}),s.jsx(se,{disabled:m||t.length<=1,loading:m,onClick:d}),s.jsx(ne,{sessions:o,revoking:g,onRevokeClick:j}),s.jsx(N,{open:!!p,onClose:w,onConfirm:c,title:"Revoke Session",description:"Are you sure you want to revoke this session? The user will be logged out from this device.",confirmLabel:"Revoke",cancelLabel:"Cancel",variant:"destructive",isLoading:!!g}),s.jsx(N,{open:x,onClose:f,onConfirm:h,title:"Revoke All Other Sessions",description:"Are you sure you want to revoke all other sessions? You will remain logged in on this device, but all other devices will be logged out.",confirmLabel:"Revoke All",cancelLabel:"Cancel",variant:"destructive",isLoading:m})]})}export{ve as SessionsPage,ve as default};