1import{a as n,j as e}from"./dnd-CNQ8BvKU.js";import{be as fe,am as y,s as E,A as O,bf as L,L as ie,ak as R,k as he,Q as I,X as re,bg as pe,bh as je,bi as ke,h as ae,bj as _e,bk as ye,Z as ve}from"./index-DpuP5qk5.js";import"./xyflow-C0vxhnBz.js";import"./datefns-D4dTs_7e.js";const Ne=Object.keys(ve);function ze({question:r,revealed:q}){const[d,m]=n.useState(!1),[c,C]=n.useState(!1),l=r.coverUrl||(r.isbn?`https://covers.openlibrary.org/b/isbn/${r.isbn}-L.jpg`:null),j=!l||d,k=r.bookTitle.split("").reduce((_,T)=>_+T.charCodeAt(0),0)%360;return e.jsx("div",{className:"flex items-center justify-center py-6 px-4",style:{minHeight:280},children:e.jsxs("div",{className:"relative rounded-md overflow-hidden transition-transform duration-300",style:{width:160,height:240,boxShadow:"4px 4px 12px rgba(0,0,0,0.25), -1px 0 4px rgba(0,0,0,0.1)",transform:q?"scale(0.95)":"scale(1)"},children:[e.jsx("div",{className:"absolute left-0 top-0 bottom-0 w-2",style:{backgroundColor:`hsl(${k}, 40%, 35%)`}}),!j&&e.jsx("img",{src:l,alt:r.bookTitle,onError:()=>m(!0),onLoad:()=>C(!0),className:`absolute inset-0 w-full h-full object-cover transition-opacity duration-300 ${c?"opacity-100":"opacity-0"}`}),(j||!c)&&e.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center p-4 text-center",style:{background:`linear-gradient(135deg, hsl(${k}, 35%, 45%), hsl(${k}, 40%, 30%))`},children:[e.jsx(ae,{className:"size-8 text-white/40 mb-3"}),e.jsx("div",{className:"text-sm font-bold text-white leading-tight mb-2",children:r.bookTitle}),e.jsx("div",{className:"text-xs text-white/70",children:r.bookAuthor})]})]})})}function $e({data:r,onSendMessage:q,assignmentId:d,quizId:m}){const[c,C]=n.useState(0),[l,j]=n.useState(null),[h,k]=n.useState({}),[_,T]=n.useState(!1),$=n.useRef(!1),v=n.useRef(void 0),[N,ne]=n.useState(!!m),[A,F]=n.useState(!1),[b,ce]=n.useState(m||null),{elapsedMs:Q,startCardTimer:le,stopCardTimer:de,resetTimer:ue,getStats:U}=fe();n.useEffect(()=>(y.track("quiz.started",{quiz_type:"book_quiz",title:r.title||null,question_count:r.questions.length},{assignment_id:d,quiz_id:m}),()=>{if(!$.current){const t=U();y.track("quiz.abandoned",{quiz_type:"book_quiz",questions_answered:t.card_times.length,total_questions:r.questions.length,timing_stats:t},{assignment_id:d,quiz_id:b||m,duration_ms:t.total_duration_ms})}}),[]),n.useEffect(()=>{le()},[c]);const[i,H]=n.useState(null),[we,W]=n.useState(!1),[D,X]=n.useState(!1),[Z,J]=n.useState(!1),[z,G]=n.useState(""),[K,V]=n.useState(!1),M=n.useRef(!1),Y=async()=>{var t;F(!0);try{const{data:{session:a}}=await E.auth.getSession();if(!a)return;const s=await fetch(`${O}/quizzes`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${a.access_token}`},body:JSON.stringify({title:r.title||"Book Quiz",quizType:"book_quiz",content:r,topic:r.title||null,subject:(i==null?void 0:i.subject)||z||null,assignmentId:d||(i==null?void 0:i.assignmentId)||null})});if(s.ok){const o=await s.json();ne(!0),ce(((t=o.quiz)==null?void 0:t.id)||null)}}catch(a){console.warn("Failed to save quiz:",a)}finally{F(!1)}},g=r.questions,x=g.length,p=g[c],w=Object.values(h).filter(Boolean).length,B=Object.values(h).filter(t=>t===!1).length,ee=n.useCallback(async(t,a,s)=>{if(!$.current){$.current=!0;try{const{data:{session:o}}=await E.auth.getSession();if(!o)return;await fetch(`${O}/study-sessions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o.access_token}`},body:JSON.stringify({sessionType:"book_quiz",topic:r.title||null,totalCards:x,correctCount:t,incorrectCount:a,scorePct:Math.round(t/x*100),missedItems:s,subject:(i==null?void 0:i.subject)||z||null,assignmentId:d||null,quizId:b||null})})}catch(o){console.warn("Failed to save study session:",o)}}},[r.title,x,i,z,d,b]);n.useEffect(()=>{!_||M.current||(M.current=!0,W(!0),(async()=>{try{const{data:{session:t}}=await E.auth.getSession();if(!t)return;const a=g.slice(0,3).map(o=>`${o.bookTitle} by ${o.bookAuthor}: ${o.question}`).join("; "),s=await fetch(`${O}/agent/recommend-link`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t.access_token}`},body:JSON.stringify({quizTitle:r.title||"Book Quiz",quizTopic:r.title||null,quizType:"book_quiz",sampleC
1ontent:a})});if(s.ok){const o=await s.json();o.recommendation&&(H(o.recommendation),G(o.recommendation.subject||""))}}catch(t){console.warn("Agent recommendation failed:",t)}finally{W(!1)}})())},[_,r,g]);const te=async(t,a)=>{!b&&!N&&await Y();const s=b;if(s)try{const{data:{session:o}}=await E.auth.getSession();if(!o)return;await fetch(`${O}/quizzes/${s}`,{method:"PATCH",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o.access_token}`},body:JSON.stringify({subject:a,assignmentId:t})}),X(!0)}catch(o){console.warn("Failed to link quiz:",o)}},me=n.useCallback(t=>{if(l!==null)return;j(t);const a=p.choices[t].isCorrect,s={...h,[c]:a};k(s);const o=a?"correct":"incorrect",f=de(c,o);y.track("quiz.card_answered",{quiz_type:"book_quiz",question_index:c,result:o,time_on_card_ms:f.response_ms,hour_of_day:f.hour_of_day,day_of_week:f.day_of_week,response_timestamp:f.timestamp},{assignment_id:d,quiz_id:b||m}),v.current=setTimeout(()=>{if(c<x-1)j(null),C(c+1);else{T(!0);const P=Object.values(s).filter(Boolean).length,oe=Object.values(s).filter(S=>S===!1).length,be=g.filter((S,ge)=>s[ge]===!1).map(S=>`${S.bookTitle} - ${S.question}`);ee(P,oe,be);const u=U();y.track("quiz.completed",{quiz_type:"book_quiz",correct_count:P,incorrect_count:oe,total_questions:x,score_pct:Math.round(P/x*100),avg_response_ms:u.avg_response_ms,median_response_ms:u.median_response_ms,fastest_ms:u.fastest_ms,slowest_ms:u.slowest_ms,impulsive_count:u.impulsive_count,deliberate_count:u.deliberate_count,quiz_start_time:u.quiz_start_time,quiz_end_time:u.quiz_end_time,hour_of_day:u.hour_of_day,day_of_week:u.day_of_week,card_times:u.card_times},{assignment_id:d,quiz_id:b||m,duration_ms:u.total_duration_ms})}},1500)},[l,p,h,c,x,g,ee,d,b,m]);n.useEffect(()=>()=>{v.current&&clearTimeout(v.current)},[]);const xe=()=>{_&&y.track("quiz.retake_initiated",{quiz_type:"book_quiz"},{assignment_id:d,quiz_id:b||m}),v.current&&clearTimeout(v.current),C(0),j(null),k({}),T(!1),$.current=!1,M.current=!1,H(null),X(!1),J(!1),ue()};if(_){const t=Math.round(w/x*100),a=t>=80?"ð":t>=60?"ð":"ðª";return e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4 mt-2",children:[r.title&&e.jsx("h4",{className:"text-xs font-semibold text-foreground mb-3",children:r.title}),e.jsxs("div",{className:"text-center py-4",children:[e.jsx("div",{className:"text-3xl mb-2",children:a}),e.jsxs("div",{className:"text-lg font-bold text-foreground",children:[t,"%"]}),e.jsxs("div",{className:"text-sm text-muted-foreground mt-1",children:[w," of ",x," correct"]}),!Z&&!D&&i&&i.confidence>.7&&i.assignmentId&&e.jsxs(L.div,{initial:{opacity:0,y:-8},animate:{opacity:1,y:0},className:"mt-4 mx-auto max-w-sm p-3 rounded-lg border border-indigo-200 dark:border-indigo-800 bg-indigo-50 dark:bg-indigo-950/30",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs text-indigo-700 dark:text-indigo-300 mb-2",children:[e.jsx(ie,{className:"size-3.5"}),e.jsx("span",{className:"font-medium",children:"This looks like it belongs to"})]}),e.jsx("p",{className:"text-sm font-semibold text-indigo-900 dark:text-indigo-200 mb-2",children:i.assignmentTitle}),i.subject&&e.jsxs("span",{className:"inline-block text-[10px] px-1.5 py-0.5 rounded-full mb-2",style:{backgroundColor:R(i.subject).accentColor+"20",color:R(i.subject).accentColor},children:[R(i.subject).emoji," ",i.subject]}),e.jsxs("div",{className:"flex gap-2 justify-center",children:[e.jsx("button",{onClick:()=>te(i.assignmentId,i.subject),className:"px-3 py-1 text-xs font-medium rounded-md bg-indigo-600 text-white hover:bg-indigo-700 transition-colors",children:"Link"}),e.jsx("button",{onClick:()=>J(!0),className:"px-3 py-1 text-xs font-medium rounded-md bg-white dark:bg-gray-800 text-muted-foreground border border-border hover:bg-muted transition-colors",children:"Dismiss"})]})]}),!Z&&!D&&i&&(i.confidence<=.7||!i.assignmentId)&&e.jsxs(L.div,{initial:{opacity:0,y:-8},animate:{opacity:1,y:0},className:"mt-4 mx-auto max-w-sm p-3 rounded-lg border border-border bg-muted/50",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground mb-2",children:[e.jsx(ie,{className:"size-3.5"}),e.jsx("span",{children:"What subject is this?"})]}),e.jsxs("div",{className:"relative mb-2",children:[e.jsxs("button",{onClick:()=>V(!K),className:"w-full flex items-center justify-between px-2.5 py-1.5 text-xs rounded-md border border-border bg-background hover:bg-muted transition-colors",children:[e.jsx("span",{children:z||i.subject||"Select subject..."}),e.jsx(he,{className:"size-3"})]}),K&&e.jsx("div",{className:"absolute z-10 w-full mt-1 max-h-40 overflow-y-auto rounded-md border border-border bg-background shadow-lg",children:Ne.map(s=>e.jsxs("button",{onClick:()=>{G(s),V(!1)},className:"w-full text-left px-2.5 py-1.5 text-xs hover:bg-muted transition-colors",children:[R(s).emoji," ",s]},s))})]}),e.jsxs("div",{className:"flex gap-2 justify-center",children:[e.jsx("button",{onClick:()=>te(i.assignmentId,z||i.subject),className:"px-3 py-1 text-xs font-medium rounded-md bg-indigo-600 text-white hover:bg-indigo-700 transition-colors",children:"Save"}),e.jsx("button",{onClick:()=>J(!0),className:"px-3 py-1 text-xs font-medium rounded-md text-muted-foreground hover:bg-muted transition-colors",children:"Skip"})]})]}),D&&e.jsxs(L.div,{initial:{opacity:0}
1,animate:{opacity:1},className:"mt-3 text-xs text-emerald-600 dark:text-emerald-400 flex items-center justify-center gap-1",children:[e.jsx(I,{className:"size-3"}),"Linked to assignment"]}),e.jsx("div",{className:"mt-4 space-y-1.5 text-left max-w-sm mx-auto",children:g.map((s,o)=>{const f=h[o];return e.jsxs("div",{className:`flex items-center gap-2 text-xs px-2 py-1 rounded ${f?"bg-emerald-50 dark:bg-emerald-950/20 text-emerald-700 dark:text-emerald-400":"bg-red-50 dark:bg-red-950/20 text-red-700 dark:text-red-400"}`,children:[f?e.jsx(I,{className:"size-3 shrink-0"}):e.jsx(re,{className:"size-3 shrink-0"}),e.jsxs("span",{className:"truncate",children:[s.bookTitle," â ",s.question]})]},o)})}),e.jsxs("div",{className:"flex gap-2 justify-center mt-4 flex-wrap",children:[e.jsxs("button",{onClick:xe,className:"flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md bg-indigo-100 text-indigo-700 dark:bg-indigo-950/30 dark:text-indigo-400 hover:bg-indigo-200 dark:hover:bg-indigo-950/50 transition-colors",children:[e.jsx(pe,{className:"size-3"}),"Try Again"]}),e.jsxs("button",{onClick:Y,disabled:N||A,className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md transition-colors ${N?"bg-emerald-100 text-emerald-700 dark:bg-emerald-950/30 dark:text-emerald-400":"bg-violet-100 text-violet-700 dark:bg-violet-950/30 dark:text-violet-400 hover:bg-violet-200 dark:hover:bg-violet-950/50"} disabled:opacity-60`,children:[N?e.jsx(I,{className:"size-3"}):e.jsx(je,{className:"size-3"}),A?"Saving...":N?"Saved":"Save Quiz"]}),B>0&&q&&e.jsxs("button",{onClick:()=>{y.track("quiz.missed_items_requested",{quiz_type:"book_quiz",missed_count:B},{assignment_id:d,quiz_id:b||m});const s=g.filter((o,f)=>h[f]===!1).map(o=>`${o.bookTitle} by ${o.bookAuthor}`);q(`I just finished a book quiz${r.title?` "${r.title}"`:""} and scored ${t}% (${w}/${x}). I missed these: ${s.join(", ")}. Can you make another book quiz focusing on what I got wrong?`)},className:"flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md bg-amber-100 text-amber-700 dark:bg-amber-950/30 dark:text-amber-400 hover:bg-amber-200 dark:hover:bg-amber-950/50 transition-colors",children:[e.jsx(ke,{className:"size-3"}),"Quiz Missed"]})]})]})]})}const se=p.choices.findIndex(t=>t.isCorrect);return e.jsxs("div",{className:"rounded-lg border border-border bg-card overflow-hidden mt-2",children:[e.jsxs("div",{className:"flex items-center justify-between px-3 py-1.5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ae,{className:"size-3.5 text-muted-foreground"}),r.title&&e.jsx("h4",{className:"text-xs font-semibold text-foreground",children:r.title}),e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:[c+1," / ",x]}),e.jsx("span",{className:`text-[10px] tabular-nums ${ye(Q)}`,children:_e(Q)})]}),(w>0||B>0)&&e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:[e.jsx("span",{className:"text-emerald-600",children:w})," / ",e.jsx("span",{className:"text-red-500",children:B})]})]}),e.jsx("div",{className:"bg-muted/30",children:e.jsx(ze,{question:p,revealed:l!==null})}),e.jsxs("div",{className:"p-3",children:[e.jsx("p",{className:"text-sm font-medium text-foreground mb-2",children:p.question}),e.jsx("div",{className:"grid grid-cols-2 gap-2",children:p.choices.map((t,a)=>{let s="border-border bg-muted/50 hover:bg-muted text-foreground";return l!==null&&(a===se?s="border-emerald-400 bg-emerald-50 dark:bg-emerald-950/30 text-emerald-700 dark:text-emerald-400":a===l&&!t.isCorrect?s="border-red-400 bg-red-50 dark:bg-red-950/30 text-red-700 dark:text-red-400":s="border-border bg-muted/30 text-muted-foreground opacity-60"),e.jsxs("button",{onClick:()=>me(a),disabled:l!==null,className:`flex items-center gap-1.5 px-3 py-2 text-xs font-medium rounded-md border transition-colors ${s} disabled:cursor-default`,children:[l!==null&&a===se&&e.jsx(I,{className:"size-3 shrink-0"}),l!==null&&a===l&&!t.isCorrect&&e.jsx(re,{className:"size-3 shrink-0"}),t.label]},a)})}),l!==null&&p.explanation&&e.jsx(L.p,{initial:{opacity:0,y:-4}
1,animate:{opacity:1,y:0},className:"text-xs text-muted-foreground mt-2 px-1",children:p.explanation})]}),e.jsx("div",{className:"flex justify-center gap-1 pb-2",children:g.map((t,a)=>{const s=h[a];return e.jsx("div",{className:`size-1.5 rounded-full transition-colors ${a===c?"bg-indigo-500":s===!0?"bg-emerald-400":s===!1?"bg-red-400":"bg-muted-foreground/30"}`},a)})})]})}export{$e as BookQuizBlock};
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.