import{a as b}from"./chunk-3GOV5KJM.js";import{a as Q}from"./chunk-GPI43RSU.js";import{b as h}from"./chunk-RC3JXGN6.js";import{a as z}from"./chunk-52JZ5I3Y.js";import{n as S,w as v}from"./chunk-COFYR5MX.js";import{D as n,Ec as u,W as i,_ as m,a as d,b as p,da as a,o,r as c,ta as r}from"./chunk-2MUIW2M6.js";var y=class l{http=a(S);router=a(v);toastService=a(z);storageService=a(h);guestService=a(Q);apiUrl=`${b.apiUrl}/quiz`;_activeSession=r(null);activeSession=this._activeSession.asReadonly();_questions=r([]);questions=this._questions.asReadonly();_quizResults=r(null);_completedQuiz=r(null);_sessionHistoryQuiz=r(null);quizResults=this._quizResults.asReadonly();sessionQuizHistory=this._sessionHistoryQuiz.asReadonly();completedQuiz=this._completedQuiz.asReadonly();_isStartingQuiz=r(!1);isStartingQuiz=this._isStartingQuiz.asReadonly();_isSubmittingAnswer=r(!1);isSubmittingAnswer=this._isSubmittingAnswer.asReadonly();_isCompletingQuiz=r(!1);isCompletingQuiz=this._isCompletingQuiz.asReadonly();hasActiveSession=u(()=>this._activeSession()!==null);currentQuestionIndex=u(()=>this._activeSession()?.currentQuestionIndex??0);totalQuestions=u(()=>this._activeSession()?.totalQuestions??0);progress=u(()=>{let s=this.totalQuestions(),e=this.currentQuestionIndex();return s>0?e/s*100:0});startQuiz(s){if(!this.canAccessCategory(s.categoryId))return this.toastService.error("You do not have access to this category"),o(()=>new Error("Category not accessible"));if(!this.storageService.isAuthenticated()){let t=this.guestService.guestState().quizLimit?.quizzesRemaining??null;if(t!==null&&t<=0)return this.toastService.warning("Guest quiz limit reached. Please sign up to continue."),this.router.navigate(["/register"]),o(()=>new Error("Guest quiz limit reached"))}return this._isStartingQuiz.set(!0),this.http.post(`${this.apiUrl}/start`,s).pipe(i(e=>{if(e.success){let t={id:e.data.sessionId,userId:this.storageService.getUserData()?.id,guestSessionId:this.guestService.guestState().session?.guestId,categoryId:s.categoryId,quizType:s.quizType||"practice",difficulty:s.difficulty||"mixed",totalQuestions:e.data.totalQuestions,currentQuestionIndex:0,score:0,correctAnswers:0,incorrectAnswers:0,skippedAnswers:0,status:"in_progress",startedAt:new Date().toISOString()};this._activeSession.set(t),e.data.questions&&this._questions.set(e.data.questions),this.storeSessionId(e.data.sessionId),this.toastService.success("Quiz started successfully!")}}),n(e=>(this.toastService.error(e.error?.message||"Failed to start quiz"),o(()=>e))),i(()=>this._isStartingQuiz.set(!1)))}submitAnswer(s){return this._isSubmittingAnswer.set(!0),this.http.post(`${this.apiUrl}/submit`,s).pipe(c(e=>{let t=e.data;return{success:e.success,isCorrect:t.isCorrect,correctAnswer:t.feedback?.correctAnswer||"",explanation:t.feedback?.explanation||"",points:t.pointsEarned||0,score:t.sessionProgress?.currentScore||0,message:e.message}}),i(e=>{if(e.success){let t=this._activeSession();if(t){let g=p(d({},t),{score:e.score,correctAnswers:e.isCorrect?t.correctAnswers+1:t.correctAnswers,incorrectAnswers:e.isCorrect?t.incorrectAnswers:t.incorrectAnswers+1,currentQuestionIndex:t.currentQuestionIndex+1});this._activeSession.set(g)}}}),n(e=>(this.toastService.error(e.error?.message||"Failed to submit answer"),o(()=>e))),i(()=>this._isSubmittingAnswer.set(!1)))}completeQuiz(s){return this._isCompletingQuiz.set(!0),this.http.post(`${this.apiUrl}/complete`,{sessionId:s}).pipe(i(e=>{if(e.success){this._completedQuiz.set(e.data);let t=this._activeSession();t&&this._activeSession.set(p(d({},t),{status:"completed",completedAt:new Date().toISOString()})),this.toastService.success("Quiz completed successfully!"),this.router.navigate(["/quiz",s,"results"])}}),n(e=>(this.toastService.error(e.error?.message||"Failed to complete quiz"),o(()=>e))),i(()=>this._isCompletingQuiz.set(!1)))}getSession(s){return this.http.get(`${this.apiUrl}/session/${s}`).pipe(i(e=>{e.success&&e.data&&this._activeSession.set(e.data)}),n(e=>(e.status===404?this.toastService.error("Quiz session not found"):this.toastService.error(e.error?.message||"Failed to load session"),o(()=>e))),c(e=>e.data))}reviewQuiz(s){return this.http.get(`${this.apiUrl}/review/${s}`).pipe(i(e=>{e.success&&this._quizResults.set(e.data)}),n(e=>(e.status===404?this.toastService.error("Quiz session not found"):this.toastService.error(e.error?.message||"Failed to load quiz review"),o(()=>e))))}abandonQuiz(s){return this.http.post(`${this.apiUrl}/abandon`,{sessionId:s}).pipe(i(()=>{this._activeSession.set(null),this.toastService.info("Quiz abandoned")}),n(e=>(this.toastService.error(e.error?.message||"Failed to abandon quiz"),o(()=>e))))}checkIncompleteSession(){let s=localStorage.getItem("activeQuizSessionId");if(s){let e=this._activeSession();if(e&&e.status==="in_progress")return s}return null}restoreSession(s){return this.getSession(s).pipe(i(e=>{localStorage.setItem("activeQuizSessionId",s),this._questions().length>0||console.log("Session restored, questions need to be loaded")}),c(e=>({session:e,hasQuestions:this._questions().length>0})),n(e=>(localStorage.removeItem("activeQuizSessionId"),o(()=>e))))}storeSessionId(s){localStorage.setItem("activeQuizSessionId",s)}clearStoredSessionId(){localStorage.removeItem("activeQuizSessionId")}clearSession(){this._activeSession.set(null),this._questions.set([]),this._quizResults.set(null),this.clearStoredSessionId()}canAccessCategory(s){return this.storageService.isAuthenticated(),!0}getEstimatedTime(s,e){return Math.ceil(s*(e==="timed"?1.5:2))}calculateTimeLimit(s){return s*1.5}static \u0275fac=function(e){return new(e||l)};static \u0275prov=m({token:l,factory:l.\u0275fac,providedIn:"root"})};export{y as a};