1import{f as W,e as ee,r as n,s as l,Q as te,j as e,J as X,B as _,q as se,D as ae,l as re,aE as ne,m as ie,n as oe,aF as ce}from"./index-BPJdZEqy.js";import{C as A}from"./card-BwOPaeW9.js";import{b as Y,A as T,f as de}from"./examConstants-BX5xtapa.js";import{F as le}from"./flag-DF9i3-VH.js";import{C as me}from"./chevron-left-tO_dpntP.js";const ue=[{type:"visibility",setup:d=>{const c=()=>{document.hidden&&d("tab_hidden")};return document.addEventListener("visibilitychange",c),()=>document.removeEventListener("visibilitychange",c)}},{type:"blur",setup:d=>{const c=()=>d("window_blur");return window.addEventListener("blur",c),()=>window.removeEventListener("blur",c)}},{type:"contextmenu",setup:d=>{const c=u=>{u.preventDefault(),d("contextmenu")};return document.addEventListener("contextmenu",c),()=>document.removeEventListener("contextmenu",c)}},{type:"copy",setup:d=>{const c=u=>{u.preventDefault(),d("copy")};return document.addEventListener("copy",c),()=>document.removeEventListener("copy",c)}}];function pe(){const{user:d,loading:c}=W(),u=ee(),[k,Z]=n.useState(!0),[a,C]=n.useState(null),[M,$]=n.useState([]),[I,G]=n.useState([]),[h,O]=n.useState({}),[g,D]=n.useState(0),[z,H]=n.useState(Date.now()),[b,F]=n.useState(!1),[Q,y]=n.useState(!1),J=n.useRef({});n.useEffect(()=>{if(!c){if(!d){u("/auth?returnTo=/sample-exam/start");return}(async()=>{const{data:t}=await l.from("aidp_attempts").select("*").eq("user_id",d.id).eq("status","in_progress").maybeSingle();if(!t){u("/sample-exam/start",{replace:!0});return}C(t);const[{data:s},{data:r},{data:o}]=await Promise.all([l.from("aidp_exam_scenarios").select("*").order("id"),l.from("aidp_exam_items").select("id, scenario_id, question, options, cognitive_level, domain_ids, display_order"),l.from("aidp_answers").select("*").eq("attempt_id",t.id)]);$(s??[]);const f=(r??[]).map(m=>{let i=m.options;return Array.isArray(i)&&(i=i.reduce((w,v)=>(v&&v.letter&&(w[v.letter]=v.text??""),w),{})),{...m,options:i}});G(f);const x={};(o??[]).forEach(m=>{x[m.item_id]=m}),O(x),Z(!1)})()}},[d,c,u]);const N=n.useMemo(()=>{if(!a||I.length===0)return[];const t=a.scenario_order??[1,2,3,4,5,6,7,8],s={};return I.forEach(r=>{(s[r.scenario_id]||=[]).push(r)}),Object.values(s).forEach(r=>r.sort((o,f)=>o.display_order-f.display_order)),t.flatMap(r=>s[r]??[])},[a,I]),U=n.useMemo(()=>{const t={};return M.forEach(s=>{t[s.id]=s}),t},[M]);n.useEffect(()=>{const t=setInterval(()=>H(Date.now()),1e3);return()=>clearInterval(t)},[]);const S=n.useMemo(()=>{if(!a)return Y;const t=Number(a.active_seconds??0),s=a.last_resume_at?Math.max(0,Math.floor((z-new Date(a.last_resume_at).getTime())/1e3)):0;return Math.max(0,Y-(t+s))},[a,z]),L=n.useCallback(async(t=!1)=>{if(!(!a||b)){F(!0);try{const{data:s,error:r}=await l.functions.invoke("aidp-submit-attempt",{body:{attempt_id:a.id,auto:t}});if(r||s?.error)throw new Error(r?.message||s?.error||"Submit failed");u(`/sample-exam/results/${a.id}`,{replace:!0})}catch(s){te.error(s instanceof Error?s.message:"Submit failed"),F(!1)}}},[a,b,u]);n.useEffect(()=>{!k&&a&&S<=0&&!b&&L(!0)},[S,a,k,b,L]);const q=n.useCallback(async t=>{if(a)try{const{data:s}=await l.from("aidp_attempts").select("flags_raised").eq("id",a.id).single(),o=[...Array.isArray(s?.flags_raised)?s.flags_raised:[],{kind:t,at:new Date().toISOString()}].slice(-200);await l.from("aidp_attempts").update({flags_raised:o}).eq("id",a.id)}catch{}},[a]);n.useEffect(()=>{if(!a)return;const t=ue.map(s=>s.setup(r=>{q(r)}));return document.body.classList.add("aidp-exam-noselect"),()=>{t.forEach(s=>s()),document.body.classList.remove("aidp-exam-noselect")}},[a,q]),n.useEffect(()=>{if(!a)return;const t=a.id,s=()=>{C(i=>{if(!i||!i.last_resume_at)return i;const w=Math.max(0,Math.floor((Date.now()-new Date(i.last_resume_at).getTime())/1e3));return{...i,active_seconds:Number(i.active_seconds??0)+w,last_resume_at:null}})},r=()=>{C(i=>!i||i.last_resume_at?i:{...i,last_resume_at:new Date().toISOString()})},o=async i=>{try{await l.functions.invoke("aidp-pause-attempt",{body:{attempt_id:t,action:i}})}catch{}},f=()=>{try{const i="https://gptocewwlzjadqlhbxtw.supabase.co/functions/v1/aidp-pause-attempt",w=JSON.stringify({attempt_id:t,action:"pause"}),v=(l.auth?.getSession,null);l.auth.getSession().then(({data:K})=>{const R=K.session?.access_token;R&&fetch(i,{method:"POST",keepalive:!0,headers:{"Content-Type":"application/json",Authorization:`Bearer ${R}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImdwdG9jZXd3bHpqYWRxbGhieHR3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjAwNDE4MzYsImV4cCI6MjA3NTYxNzgzNn0.AS2ZG7tQOGhRl69BQyrDTfE_FpXIDw6FxXlJZJQ6J_4"},body:w}).catch(()=>{})})}catch{}},x=()=>{document.hidden?(s(),o("pause")):(r(),o("resume"))},m=()=>{s(),f()};return document.addEventListener("visibilitychange",x),window.addEventListener("pagehide",m),window.addEventListener("beforeunload",m),r(),o("resume"),()=>{document.removeEventListener("visibilitychange",x),window.removeEventListener("pagehide",m),window.removeEventListener("beforeunload",m),s(),o("pause")}},[a?.id]);const P=n.useCallback((t,s)=>{O(o=>({...o,[t]:{...o[t],...s}}));const r=h[t]?.id;r&&(clearTimeout(J.current[t]),J.current[t]=setTimeout(async()=>{const o={...h[t],...s};await l.from("aidp_answers").update({selected_answer:o.selected_answer??null,marked_for_review:!!o.marked_for_review}).eq("id",r)},400))},[h]);if(c||k)return e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background",children:e.jsx(X,{className:"h-6 w-6 animate-spin text-muted-foreground"})});const p=N[g];if(!p)return null;const j=U[p.scenario_id],E=h[p.id],B=Object.values(h).filter(t=>!!t.selected_answer).length,V=S<600;return e.jsxs("div",{className:"min-h-screen bg-background text-foreground flex flex-col aidp-exam-shell",children:[e.jsx("header",{className:"sticky top-0 z-30 border-b bg-background/95 backdrop-blur-sm",children:e.jsxs("div",{className:"container mx-auto px-4 h-14 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[e.jsx("span",{className:"font-semibold tracking-wide",children:"AIDP Sample Examination"}),e.jsxs("span",{className:"text-muted-foreground hidden sm:inline",children:["Item ",g+1," of ",T]}
1),e.jsxs("span",{className:"text-muted-foreground hidden md:inline",children:["Scenario ",p.scenario_id,": ",j?.title]})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:`font-mono text-base tabular-nums ${V?"text-destructive font-semibold":""}`,"aria-label":"Time remaining",children:de(S)}),e.jsx(_,{size:"sm",variant:"default",onClick:()=>y(!0),children:"Submit"})]})]})}),e.jsx("main",{className:"flex-1",children:e.jsxs("div",{className:"container mx-auto px-4 py-6 grid grid-cols-1 lg:grid-cols-[260px_1fr] gap-6",children:[e.jsx("aside",{className:"order-2 lg:order-1",children:e.jsxs(A,{className:"p-4",children:[e.jsxs("p",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-3",children:["Navigator · ",B,"/",T]}),e.jsx("div",{className:"grid grid-cols-9 gap-1.5",children:N.map((t,s)=>{const r=h[t.id],o=s===g,f=!!r?.selected_answer,x=!!r?.marked_for_review;return e.jsxs("button",{onClick:()=>D(s),className:["h-8 w-8 text-xs font-mono rounded-sm border transition-colors flex items-center justify-center relative",o?"border-primary ring-2 ring-primary/30":"border-border",f?"bg-primary text-primary-foreground":"bg-background text-foreground",x&&!f?"bg-amber-100 dark:bg-amber-900/40":""].join(" "),"aria-label":`Item ${s+1}`,children:[s+1,x&&e.jsx("span",{className:"absolute -top-1 -right-1 h-2 w-2 rounded-full bg-amber-500"})]},t.id)})}),e.jsxs("div",{className:"mt-4 space-y-1.5 text-xs text-muted-foreground",children:[e.jsxs("p",{children:[e.jsx("span",{className:"inline-block w-3 h-3 bg-primary mr-2 align-middle"}),"Answered"]}),e.jsxs("p",{children:[e.jsx("span",{className:"inline-block w-3 h-3 border border-border mr-2 align-middle"}),"Unanswered"]}),e.jsxs("p",{children:[e.jsx("span",{className:"inline-block w-3 h-3 bg-amber-300 mr-2 align-middle"}),"Flagged"]})]})]})}),e.jsxs("section",{className:"order-1 lg:order-2",children:[j&&e.jsxs(A,{className:"p-5 mb-4 bg-muted/30",children:[e.jsx("p",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-1",children:j.industry}),e.jsx("h2",{className:"text-lg font-semibold mb-2",children:j.title}),e.jsx("p",{className:"text-sm leading-relaxed whitespace-pre-wrap",children:j.setup})]}),e.jsxs(A,{className:"p-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 mb-4",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-1",children:["Item ",g+1]}),e.jsx("p",{className:"text-base font-medium leading-relaxed",children:p.question})]}),e.jsxs(_,{size:"sm",variant:E?.marked_for_review?"default":"outline",onClick:()=>P(p.id,{marked_for_review:!E?.marked_for_review}),children:[e.jsx(le,{className:"h-3.5 w-3.5 mr-1.5"}),E?.marked_for_review?"Flagged":"Flag"]})]}),e.jsx("div",{className:"space-y-2 mt-6",role:"radiogroup",children:["A","B","C","D"].map(t=>{const s=E?.selected_answer===t;return e.jsxs("button",{role:"radio","aria-checked":s,onClick:()=>P(p.id,{selected_answer:t}),className:["w-full text-left p-4 rounded border transition-colors flex gap-3",s?"border-primary bg-primary/5":"border-border hover:bg-muted/50"].join(" "),children:[e.jsxs("span",{className:`font-mono text-sm font-semibold w-6 ${s?"text-primary":"text-muted-foreground"}`,children:[t,"."]}),e.jsx("span",{className:"text-sm leading-relaxed",children:p.options[t]})]},t)})}),e.jsxs("div",{className:"flex items-center justify-between mt-8 pt-4 border-t",children:[e.jsxs(_,{variant:"outline",disabled:g===0,onClick:()=>D(t=>Math.max(0,t-1)),children:[e.jsx(me,{className:"h-4 w-4 mr-1"})," Previous"]}),e.jsxs(_,{variant:"outline",disabled:g===N.length-1,onClick:()=>D(t=>Math.min(N.length-1,t+1)),children:["Next ",e.jsx(se,{className:"h-4 w-4 ml-1"})]})]})]})]})]})}),e.jsx(ae,{open:Q,onOpenChange:y,children:e.jsxs(re,{children:[e.jsxs(ne,{children:[e.jsx(ie,{children:"Submit your examination?"}),e.jsxs(oe,{children:["You have answered ",B," of ",T," items. Once submitted, you cannot change your answers."]})]}),e.jsxs(ce,{children:[e.jsx(_,{variant:"outline",onClick:()=>y(!1),children:"Keep going"}),e.jsxs(_,{onClick:()=>{y(!1),L(!1)},disabled:b,children:[b&&e.jsx(X,{className:"h-4 w-4 mr-2 animate-spin"}),"Submit"]})]})]})})]})}const we=pe;export{we as component};
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.