import{r as l,j as e}from"./vendor-BKmmeuxg.js";import{B as y}from"./button-BpNwv2aE.js";import{L as M}from"./label-HmIcv5Yx.js";import{S as N,a as v,b as S,c as C,d as k}from"./select-CD9o3HN1.js";import"./index-WW_1X1qx.js";import"./utils-DxSsnXxD.js";const V=({question:d,answer:c,onAnswerChange:u})=>{var j;const i=((j=d.options)==null?void 0:j.matches)||[],n=()=>Array.isArray(c==null?void 0:c.matches)?c.matches.reduce((s,r)=>(s[r.id]=r.answer,s),{}):c!=null&&c.matches&&typeof c.matches=="object"?{...c.matches}:{},[a,o]=l.useState(n);l.useEffect(()=>{o(n())},[d.id]);const h=t=>{o(t);const s=Object.entries(t).map(([r,b])=>({id:Number(r),answer:b}));u({matches:s})},p=(t,s)=>{h({...a,[t]:s})},f=()=>{h({})},m=l.useMemo(()=>i.map(t=>({id:t.id,text:t.question})),[i]),x=l.useMemo(()=>i.map(t=>({id:t.id,text:t.answer})),[i]),g=l.useMemo(()=>[...x].sort(()=>Math.random()-.5),[x]);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-sm text-gray-600",children:"Match each item on the left with the correct item on the right:"}),e.jsx(y,{variant:"ghost",size:"sm",onClick:f,disabled:Object.keys(a).length===0,children:"Clear All"})]}),e.jsx("div",{className:"space-y-3",children:m.map(t=>e.jsxs("div",{className:"grid grid-cols-1 gap-3 rounded-lg border p-4 md:grid-cols-2",children:[e.jsx("div",{className:"flex items-center",children:e.jsx("div",{className:"flex-1",children:e.jsx(M,{className:"font-normal",children:t.text})})}),e.jsx("div",{children:e.jsxs(N,{value:a[t.id]||"",onValueChange:s=>p(t.id,s),children:[e.jsx(v,{children:e.jsx(S,{placeholder:"Select a match..."})}),e.jsx(C,{children:g.map(s=>e.jsx(k,{value:s.text,children:s.text},s.id))})]})})]},t.id))}),Object.keys(a).length>0&&e.jsxs("p",{className:"text-sm text-gray-600",children:["Matched: ",e.jsx("span",{className:"font-semibold",children:Object.keys(a).length})," of ",m.length]})]})};export{V as default};