Files
quiz-test/chunk-GPI43RSU.js
2025-12-29 22:23:30 +02:00

2 lines
3.7 KiB
JavaScript

import{a as m,b as v}from"./chunk-RC3JXGN6.js";import{a as E}from"./chunk-52JZ5I3Y.js";import{n as p,w as d}from"./chunk-COFYR5MX.js";import{D as a,W as n,_ as S,a as i,b as r,da as u,o,ta as h}from"./chunk-2MUIW2M6.js";var I=class g{http=u(p);storageService=u(v);toastService=u(E);router=u(d);API_URL=`${m.apiUrl}/guest`;GUEST_TOKEN_KEY="guest_token";GUEST_ID_KEY="guest_id";DEVICE_ID_KEY="device_id";SESSION_EXPIRY_HOURS=24;guestStateSignal=h({session:null,isGuest:this.hasActiveGuestSession(),isLoading:!1,error:null,quizLimit:null});guestState=this.guestStateSignal.asReadonly();startSession(){this.setLoading(!0);let e=this.getOrCreateDeviceId();return this.http.post(`${this.API_URL}/start-session`,{deviceId:e}).pipe(n(t=>{this.storageService.setItem(this.GUEST_ID_KEY,t.data.guestId),this.storageService.setGuestToken(t.data.sessionToken),this.guestStateSignal.update(s=>r(i({},s),{session:t.data,isGuest:!0,isLoading:!1,error:null})),this.toastService.success("Welcome! You're browsing as a guest.")}),a(t=>(this.setError("Failed to start guest session"),this.toastService.error("Unable to start guest session. Please try again."),o(()=>t))))}getSession(e){return this.setLoading(!0),this.http.get(`${this.API_URL}/session/${e}`).pipe(n(t=>{this.guestStateSignal.update(s=>r(i({},s),{session:t,isGuest:!0,isLoading:!1,error:null}))}),a(t=>(t.status===404?(this.clearGuestSession(),this.toastService.warning("Guest session expired. Please start a new session.")):this.setError("Failed to fetch guest session"),o(()=>t))))}getQuizLimit(){return this.setLoading(!0),this.http.get(`${this.API_URL}/quiz-limit`).pipe(n(e=>{this.guestStateSignal.update(t=>r(i({},t),{quizLimit:e,isLoading:!1,error:null}))}),a(e=>(this.setError("Failed to fetch quiz limit"),o(()=>e))))}convertToUser(e,t){return this.setLoading(!0),this.http.post(`${this.API_URL}/convert`,i({guestSessionId:e},t)).pipe(n(()=>{this.clearGuestSession(),this.toastService.success("Guest data successfully migrated to your account!")}),a(s=>(this.setError("Failed to convert guest session"),o(()=>s))))}getOrCreateDeviceId(){let e=this.storageService.getItem(this.DEVICE_ID_KEY);return e||(e=this.generateUUID(),this.storageService.setItem(this.DEVICE_ID_KEY,e)),e}generateUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{let t=Math.random()*16|0;return(e==="x"?t:t&3|8).toString(16)})}hasActiveGuestSession(){let e=this.storageService.getItem(this.GUEST_TOKEN_KEY),t=this.storageService.getItem(this.GUEST_ID_KEY);return!!(e&&t)}getGuestToken(){return this.storageService.getItem(this.GUEST_TOKEN_KEY)}getGuestId(){return this.storageService.getItem(this.GUEST_ID_KEY)}isSessionExpired(){let e=this.guestState().session;if(!e)return!0;let t=new Date(e.createdAt);return(new Date().getTime()-t.getTime())/(1e3*60*60)>=this.SESSION_EXPIRY_HOURS}clearGuestSession(){this.storageService.removeItem(this.GUEST_TOKEN_KEY),this.storageService.removeItem(this.GUEST_ID_KEY),this.guestStateSignal.update(e=>r(i({},e),{session:null,isGuest:!1,isLoading:!1,error:null,quizLimit:null}))}setLoading(e){this.guestStateSignal.update(t=>r(i({},t),{isLoading:e}))}setError(e){this.guestStateSignal.update(t=>r(i({},t),{isLoading:!1,error:e}))}hasReachedQuizLimit(){let e=this.guestState().quizLimit;return e?e.quizzesRemaining<=0:!1}getTimeRemaining(){let e=this.guestState().session;if(!e)return"0h 0m";let t=new Date(e.createdAt),s=new Date(t.getTime()+this.SESSION_EXPIRY_HOURS*60*60*1e3),c=new Date,l=s.getTime()-c.getTime();if(l<=0)return"0h 0m";let f=Math.floor(l/(1e3*60*60)),x=Math.floor(l%(1e3*60*60)/(1e3*60));return`${f}h ${x}m`}static \u0275fac=function(t){return new(t||g)};static \u0275prov=S({token:g,factory:g.\u0275fac,providedIn:"root"})};export{I as a};