1import{o as e}from"./rolldown-runtime-C0FnF6B9.js";import{v as t}from"./vendor-B0AOCiH9.js";import{li as n}from"./icons-CqyDchun.js";import{Ht as r,Zt as i}from"./radix-Br7wHbo6.js";import{r as a}from"./tanstack-nRY74ZUq.js";import{_ as o,a as s,d as c,f as l,g as u,h as d,m as f,p,u as m}from"./api-DoaDNLpu.js";import{r as h}from"./error-handling-CZgs4L88.js";import{o as g}from"./constants-DmgjC9E0.js";var _=e(n(),1);function v(e){let t=a({queryKey:[`profile`,e],queryFn:async()=>{if(!e)return null;try{return await s(`getProfile`,{userId:e})}catch(e){let t=e;if(t?.status===404||t?.message?.includes(`CORS`)||t?.message?.includes(`fetch`))return null;throw e}},enabled:!!e,retry:!1,refetchOnWindowFocus:!1});return(0,_.useEffect)(()=>{t.error&&h(t.error,r)},[t.error]),t}function y(e){if(!e)return null;try{let t=e.split(`.`);if(t.length!==3)return null;let n=JSON.parse(atob(t[1].replace(/-/g,`+`).replace(/_/g,`/`)));return n.exp?n.exp*1e3:null}catch{return null}}function b(e){let{inactivityTimeout:n=6e5,warningTime:i=3e5,onWarning:a,onTimeout:o}=e,s=t(),[c,l]=(0,_.useState)(!1),[u,f]=(0,_.useState)(null),m=(0,_.useRef)(null),h=(0,_.useRef)(null),g=(0,_.useRef)(null),v=(0,_.useRef)(Date.now()),b=(0,_.useRef)(!1),x=(0,_.useCallback)(()=>{if(v.current=Date.now(),l(!1),m.current&&clearTimeout(m.current),h.current&&clearTimeout(h.current),i>0){let e=Math.max(0,n-i);h.current=setTimeout(()=>{l(!0),a?.()},e)}m.current=setTimeout(async()=>{p()&&(o?await o():(d(),s(`/talent/login`)))},n)},[n,i,a,o,s]),S=(0,_.useCallback)(async()=>{try{let e=p();if(!e){f(null);return}let t=y(e);if(!t){f(null);return}let n=t-Date.now();f(n),i>0&&n>0&&n<=i&&!c&&(l(!0),a?.()),n<=0&&(r.warning(`Your session has expired.`,{description:`Please log in again to continue.`}),d(),o?await o():s(`/talent/login`))}catch{}},[i,c,a,s,o]);return(0,_.useEffect)(()=>{let e=[`mousedown`,`keydown`,`scroll`,`touchstart`,`click`,`mousemove`],t=()=>{p()&&x()},n=()=>{e.forEach(e=>{window.addEventListener(e,t,{passive:!0})}),p()&&(x(),b.current||=!0),g.current=setInterval(S,3e4),S()},r=()=>{e.forEach(e=>{window.removeEventListener(e,t)}),m.current&&clearTimeout(m.current),h.current&&clearTimeout(h.current),g.current&&clearInterval(g.current)};n();let i=e=>{e.key===`auth_token`&&!e.newValue?(r(),b.current=!1):e.key===`auth_token`&&e.newValue&&(b.current=!1,r(),n())};return window.addEventListener(`storage`,i),()=>{window.removeEventListener(`storage`,i),r()}},[]),{showWarning:c,timeUntilExpiry:u,extendSession:()=>{x(),l(!1)},lastActivity:v.current}}var x=i(),S=(0,_.createContext)(void 0);function C({children:e}){let[n,i]=(0,_.useState)(null),[a,s]=(0,_.useState)(!0),h=t(),{data:y,isLoading:C}=v(n?.id);(0,_.useEffect)(()=>{(async()=>{try{let e=await o();if(e.success&&e.user)i({id:e.user.id,email:e.user.email,firstName:e.user.firstName,lastName:e.user.lastName,tenantId:e.user.tenantId});else{let t=!!p();i(null),e.error&&e.error!==`No token found`&&(d(),t&&r.warning(`Your session has expired. Please log in again.`,{description:e.error}))}}catch{let e=!!p();i(null),e&&r.warning(`Network error. Your session may be affected.`,{description:`Please check your internet connection.`})}finally{s(!1)}})()},[]),(0,_.useEffect)(()=>{let e=e=>{if(e.key===`auth_token`&&!e.newValue){let e=!!n;i(null),m(),e&&r.info(`You have been logged out from another tab or window.`)}};return window.addEventListener(`storage`,e),()=>window.removeEventListener(`storage`,e)},[n]);let w=(0,_.useCallback)(async()=>{let e=window.location.pathname,t=[`/assessment/public/`,`/3MTT`,`/3mttc4`].some(t=>e.startsWith(t)),n=c();d(),i(null),m(),!t&&h(n?`/creator/login`:`/talent/login`)},[h]);b({inactivityTimeout:g.SESSION_INACTIVITY,warningTime:0,onTimeout:async()=>{r.warning(`You have been logged out due to inactivity.`,{description:`Your session expired after 1 hour of inactivity.`}),await w()}});let T=(0,_.useMemo)(()=>!!(a||n&&C),[a,n,C]),E=(0,_.useMemo)(()=>e=>{if(e&&e.length!==0)return e.includes(`super_admin`)?`super_admin`:e.includes(`admin`)?`admin`:e[0]},[]),D=(0,_.useMemo)(()=>n?{id:n.id,email:n.email,firstName:n.firstName||y?.first_name||void 0,lastName:n.lastName||y?.last_name||void 0,role:E(y?.roles),tenantId:n.tenantId||y?.tenant_id||void 0}:null,[n,y?.first_name,y?.last_name,y?.roles,y?.tenant_id,E]),O=(0,_.useCallback)(async(e,t)=>{let n=await f({email:e,password:t});if(!n.success||!n.user)throw Error(n.error||`Login failed`);i({id:n.user.id,email:n.user.email,firstName:n.user.firstName,lastName:n.user.lastName,tenantId:n.user.tenantId}),c()&&!l(n.user.id)&&m()},[]),k=(0,_.useCallback)(async(e,t,n,r)=>{let a=await u({email:e,password:t,firstName:n,lastName:r});if(!a.success||!a.user)throw Error(a.error||`Signup failed`);i({id:a.user.id,email:a.user.email,firstName:a.user.firstName,lastName:a.user.lastName,tenantId:a.user.tenantId}),c()&&!l(a.user.id)&&m()},[]),A=(0,_.useMemo)(()=>({user:D,isAuthenticated:!!n,isLoading:T,login:O,signup:k,logout:w}),[D,n,T,O,k,w]);return(0,x.jsx)(S.Provider,{value:A,children:e})}function w(){let e=(0,_.useContext)(S);if(e===void 0)throw Error(`useAuth must be used within an AuthProvider`);return e}export{v as i,C as n,w as r,S as t};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.