1import{r,j as e,W as A,b as P,a4 as oe,a5 as G,a6 as le,X as de,C as ce,w as me,a7 as pe,m as I,F as B,a1 as se,a8 as q,a9 as O,e as ae,s as F,i as $,u as ue,d as xe}from"./index-TfVn0xSy.js";import{L as fe,M as he,a as ge,u as H}from"./use-motion-value-CUi5uDuG.js";import{B as V}from"./book-open-BSRrVdOS.js";function be({onAdd:t,onTranslate:l}){const[p,f]=r.useState(""),[g,u]=r.useState(""),[m,w]=r.useState(!1),[x,_]=r.useState(0),y=r.useRef(null),[a,s]=r.useState(!1),[o,n]=r.useState([]),[h,d]=r.useState(new Set),[z,N]=r.useState(0),[v,i]=r.useState(""),[j,k]=r.useState(""),[M,b]=r.useState(""),[T,R]=r.useState([]);r.useEffect(()=>(m?(_(0),y.current=setInterval(()=>_(c=>c+1),1e3)):(y.current&&clearInterval(y.current),_(0)),()=>{y.current&&clearInterval(y.current)}),[m]);const L=async()=>{if(!p.trim()||m)return;w(!0);const c=g.split(",").map(S=>S.trim()).filter(Boolean),C=await l(p.trim());if(w(!1),!!C){if(C.translations.length<=1){await t(p.trim(),c,C.translations,C.translations[0]),f(""),u("");return}n(C.translations),d(new Set(C.translations.map((S,E)=>E))),N(C.primaryIndex),b(p.trim()),R(c),s(!0)}},ne=c=>{d(C=>{const S=new Set(C);return S.has(c)?(S.delete(c),z===c&&S.size>0&&N([...S][0])):S.add(c),S})},Y=()=>{if(!v.trim())return;const c={simplified:v.trim(),pinyin:j.trim()||"â"};n(S=>[...S,c]);const C=o.length;d(S=>new Set([...S,C])),i(""),k("")},re=async()=>{const c=o.filter((S,E)=>h.has(E));if(c.length===0)return;const C=h.has(z)?o[z]:c[0];await t(M,T,c,C),D(),f(""),u("")},D=()=>{s(!1),n([]),d(new Set),N(0),i(""),k("")};return e.jsxs("div",{className:"relative",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{value:p,onChange:c=>f(c.target.value),placeholder:"Type an English word or phraseâ¦",className:"w-56 bg-background/60 border-border/50 text-sm",onKeyDown:c=>c.key==="Enter"&&L(),disabled:m||a}),e.jsx(A,{value:g,onChange:c=>u(c.target.value),placeholder:"Tags (comma sep.)",className:"w-32 bg-background/60 border-border/50 text-xs",onKeyDown:c=>c.key==="Enter"&&L(),disabled:m||a}),e.jsx(P,{size:"sm",onClick:L,disabled:m||!p.trim()||a,className:"min-w-[90px]",children:m?e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"w-4 h-4 animate-spin mr-1"}),x>2?`${x}s`:"Translating"]}):e.jsxs(e.Fragment,{children:[e.jsx(G,{className:"w-4 h-4 mr-1"}),"Add"]})})]}),a&&le.createPortal(e.jsxs("div",{className:"fixed inset-0 z-[9999] flex items-start justify-center pt-24",children:[e.jsx("div",{className:"absolute inset-0 bg-black/40 backdrop-blur-sm",onClick:D}),e.jsxs("div",{className:"relative w-80 rounded-xl border border-border/50 bg-card/95 backdrop-blur-md shadow-2xl p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsxs("h3",{className:"text-sm font-semibold text-foreground",children:['Translations for "',M,'"']}),e.jsx("button",{onClick:D,className:"p-1 rounded hover:bg-accent/50",children:e.jsx(de,{className:"w-4 h-4 text-muted-foreground"})})]}),e.jsx("p",{className:"text-[10px] text-muted-foreground mb-2",children:"Select translations & choose a primary one"}),e.jsx("div",{className:"space-y-1.5 max-h-48 overflow-auto",children:o.map((c,C)=>{const S=h.has(C),E=z===C;return e.jsxs("div",{className:`flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm cursor-pointer transition-colors ${S?"bg-primary/10 border border-primary/30":"border border-transparent hover:bg-accent/30"}`,onClick:()=>ne(C),children:[e.jsx("div",{className:`w-4 h-4 rounded border flex items-center justify-center shrink-0 ${S?"bg-primary border-primary":"border-border"}`,children:S&&e.jsx(ce,{className:"w-3 h-3 text-primary-foreground"})}),e.jsx("span",{className:"font-bold text-foreground",children:c.simplified}),e.jsx("span",{className:"text-xs text-muted-foreground",children:c.pinyin}),e.jsx("div",{className:"ml-auto",children:S&&e.jsx("button",{onClick:ie=>{ie.stopPropagation(),N(C)},className:`text-[10px] px-1.5 py-0.5 rounded-full transition-colors ${E?"bg-primary text-primary-foreground":"bg-muted text-muted-foreground hover:bg-accent"}`,children:E?"â
Primary":"Set primary"})})]},C)})}),e.jsxs("div",{className:"mt-3 pt-3 border-t border-border/30",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground mb-1.5",children:"+ Add translation"}),e.jsxs("div",{className:"flex gap-1.5",children:[e.jsx(A,{value:v,onCh
1ange:c=>i(c.target.value),placeholder:"æ±å",className:"flex-1 h-7 text-xs bg-background/60 border-border/50",onKeyDown:c=>c.key==="Enter"&&Y()}),e.jsx(A,{value:j,onChange:c=>k(c.target.value),placeholder:"pÄ«nyÄ«n",className:"flex-1 h-7 text-xs bg-background/60 border-border/50",onKeyDown:c=>c.key==="Enter"&&Y()}),e.jsx(P,{size:"sm",variant:"outline",className:"h-7 px-2",onClick:Y,disabled:!v.trim(),children:e.jsx(G,{className:"w-3 h-3"})})]})]}),e.jsxs("div",{className:"mt-3 flex justify-end gap-2",children:[e.jsx(P,{size:"sm",variant:"ghost",onClick:D,children:"Cancel"}),e.jsxs(P,{size:"sm",onClick:re,disabled:h.size===0,children:["Add Card (",h.size,")"]})]})]})]}),document.body)]})}const X=["comfortable","compact","tiny"];function ye({view:t,onViewChange:l,settings:p,onSettingsChange:f,searchQuery:g,onSearchChange:u,onTidy:m,onAddCard:w,onTranslate:x,flipAll:_,onFlipAllToggle:y}){const a=()=>{const s=X.indexOf(p.board_density);f({board_density:X[(s+1)%X.length]})};return e.jsxs("div",{className:"flex items-center justify-between gap-3 px-4 py-2.5 bg-card/80 backdrop-blur-md border-b border-border/30",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(be,{onAdd:w,onTranslate:x}),e.jsxs("div",{className:"relative",children:[e.jsx(me,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground"}),e.jsx(A,{value:g,onChange:s=>u(s.target.value),placeholder:"Searchâ¦",className:"w-40 pl-8 bg-background/60 border-border/50 text-xs h-8"})]})]}),e.jsx("div",{className:"flex items-center bg-muted/50 rounded-lg p-0.5 gap-0.5",children:["board","parked","deck"].map(s=>e.jsx("button",{onClick:()=>l(s),className:`px-3 py-1 rounded-md text-xs font-medium capitalize transition-colors ${t===s?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"}`,children:s},s))}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsxs(P,{size:"sm",variant:_?"secondary":"ghost",onClick:y,className:"text-xs h-7 px-2",children:[e.jsx(fe,{className:"w-3.5 h-3.5 mr-1"}),_?"Show English":"Show Chinese"]}),e.jsxs(P,{size:"sm",variant:p.snap_to_grid?"secondary":"ghost",onClick:()=>f({snap_to_grid:!p.snap_to_grid}),className:"text-xs h-7 px-2",children:[e.jsx(he,{className:"w-3.5 h-3.5 mr-1"}),"Snap ",p.snap_to_grid?"On":"Off"]}),e.jsxs(P,{size:"sm",variant:"ghost",onClick:a,className:"text-xs h-7 px-2",children:[e.jsx(pe,{className:"w-3.5 h-3.5 mr-1"}),p.board_density]}),t==="board"&&e.jsxs(P,{size:"sm",variant:"ghost",onClick:m,className:"text-xs h-7 px-2",children:[e.jsx(ge,{className:"w-3.5 h-3.5 mr-1"}),"Tidy"]})]})]})}const ve={comfortable:{w:200,h:150,engFont:"text-lg",zhFont:"text-2xl",pyFont:"text-sm",secFont:"text-xs"},compact:{w:160,h:120,engFont:"text-base",zhFont:"text-xl",pyFont:"text-xs",secFont:"text-[10px]"},tiny:{w:130,h:95,engFont:"text-sm",zhFont:"text-lg",pyFont:"text-[10px]",secFont:"text-[9px]"}},K=20;function je({card:t,size:l,snapToGrid:p,onUpdate:f,onDelete:g,draggable:u=!0,globalFlip:m}){var M;const[w,x]=r.useState(!1),[_,y]=r.useState(!1),a=ve[l],s=r.useRef(!1),o=H(t.position_x),n=H(t.position_y);s.current||(o.set(t.position_x),n.set(t.position_y)),m!==void 0||t.is_flipped;const h=()=>{s.current||(m!==void 0?y(b=>!b):f(t.id,{is_flipped:!t.is_flipped}))},d=b=>p?Math.round(b/K)*K:Math.round(b),N={board:[{label:"Park",icon:q,action:()=>f(t.id,{state:"parked"})},{label:"Deck",icon:V,action:()=>f(t.id,{state:"deck"})}],parked:[{label:"Board",icon:O,action:()=>f(t.id,{state:"board"})},{label:"Deck",icon:V,action:()=>f(t.id,{state:"deck"})}],deck:[{label:"Board",icon:O,action:()=>f(t.id,{state:"board"})},{label:"Park",icon:q,action:()=>f(t.id,{state:"parked"})}]}[t.state]||[],v=m!==void 0?m?!_:_:t.is_flipped,i=((M=t.translations)==null?void 0:M.length)>0?t.translations:[{simplified:t.chinese_simplified,pinyin:t.pinyin}],j=t.primary_translation||i[0],k=i.filter(b=>b.simplified!==j.simplified);return e.jsxs(I.div,{className:"absolute select-none",style:{x:o,y:n,zIndex:w?50:10,width:a.w,height:a.h},drag:u,dragMomentum:!1,dragElastic:0,onDragStart:()=>{s.current=!1},onDrag:()=>{s.current=!0},onDragEnd:()=>
1{if(!s.current)return;const b=d(Math.max(0,o.get())),T=d(Math.max(0,n.get()));o.set(b),n.set(T),f(t.id,{position_x:b,position_y:T}),setTimeout(()=>{s.current=!1},50)},onMouseEnter:()=>x(!0),onMouseLeave:()=>x(!1),whileHover:{scale:1.03},children:[e.jsx("div",{className:"relative cursor-pointer w-full h-full",style:{perspective:600},onClick:h,children:e.jsxs(I.div,{className:"absolute inset-0 rounded-xl",style:{transformStyle:"preserve-3d"},animate:{rotateY:v?180:0},transition:{duration:.45,ease:[.4,0,.2,1]},children:[e.jsxs("div",{className:"absolute inset-0 rounded-xl border border-border/40 bg-card/90 backdrop-blur-md shadow-lg flex flex-col items-center justify-center p-3 gap-1",style:{backfaceVisibility:"hidden"},children:[t.is_flagged&&e.jsx("div",{className:"absolute top-2 right-2",children:e.jsx(B,{className:"w-3 h-3 text-destructive fill-destructive"})}),e.jsx("span",{className:`${a.engFont} font-semibold text-foreground text-center leading-tight`,children:t.english_text}),t.tags.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1 mt-1",children:t.tags.slice(0,3).map(b=>e.jsx("span",{className:"text-[9px] px-1.5 py-0.5 rounded-full bg-primary/10 text-primary font-medium",children:b},b))})]}),e.jsxs("div",{className:"absolute inset-0 rounded-xl border border-primary/30 bg-gradient-to-br from-primary/10 via-card/95 to-secondary/10 backdrop-blur-md shadow-lg flex flex-col items-center justify-center p-3 gap-0.5 overflow-hidden",style:{backfaceVisibility:"hidden",transform:"rotateY(180deg)"},children:[e.jsx("span",{className:`${a.zhFont} font-bold text-foreground`,children:j.simplified}),e.jsx("span",{className:`${a.pyFont} text-muted-foreground font-medium`,children:j.pinyin}),k.length>0&&e.jsx("div",{className:"mt-1 flex flex-col items-center gap-0.5",children:k.slice(0,3).map((b,T)=>e.jsxs("span",{className:`${a.secFont} text-muted-foreground/70`,children:[b.simplified," ",e.jsx("span",{className:"opacity-60",children:b.pinyin})]},T))})]})]})}),w&&e.jsxs(I.div,{initial:{opacity:0,y:4},animate:{opacity:1,y:0},className:"absolute -bottom-8 left-1/2 -translate-x-1/2 flex gap-1 bg-popover/95 backdrop-blur-sm border border-border rounded-lg px-1.5 py-1 shadow-lg",children:[N.map(({label:b,icon:T,action:R})=>e.jsx("button",{onClick:L=>{L.stopPropagation(),R()},className:"p-1 rounded hover:bg-accent/50 transition-colors",title:b,children:e.jsx(T,{className:"w-3.5 h-3.5 text-muted-foreground"})},b)),e.jsx("button",{onClick:b=>{b.stopPropagation(),f(t.id,{is_flagged:!t.is_flagged})},className:"p-1 rounded hover:bg-accent/50 transition-colors",title:"Flag",children:e.jsx(B,{className:`w-3.5 h-3.5 ${t.is_flagged?"text-destructive fill-destructive":"text-muted-foreground"}`})}),e.jsx("button",{onClick:b=>{b.stopPropagation(),g(t.id)},className:"p-1 rounded hover:bg-destructive/20 transition-colors",title:"Delete",children:e.jsx(se,{className:"w-3.5 h-3.5 text-destructive"})})]})]})}const Ne={comfortable:{w:200,h:150,gapX:20,gapY:20},compact:{w:160,h:120,gapX:16,gapY:16},tiny:{w:130,h:95,gapX:12,gapY:12}};function _e({cards:t,density:l,snapToGrid:p,onUpdate:f,onDelete:g,globalFlip:u}){const m=r.useRef(null),w=r.useRef({top:0,left:0}),[x,_]=r.useState(1200);r.useEffect(()=>{const i=m.current;if(!i)return;const j=new ResizeObserver(k=>{var b;const M=(b=k[0])==null?void 0:b.contentRect.width;M&&M>0&&_(M)});return j.observe(i),()=>j.disconnect()},[]);const y=r.useCallback(()=>{m.current&&(w.current={top:m.current.scrollTop,left:m.current.scrollLeft})},[]);r.useEffect(()=>{const i=m.current;i&&requestAnimationFrame(()=>{i.scrollTop=w.current.top,i.scrollLeft=w.current.left})},[t]),r.useEffect(()=>{const i=m.current;if(!i)return;const j=()=>{w.current={top:i.scrollTop,left:i.scrollLeft}};return i.addEventListener("scroll",j,{passive:!0}),()=>i.removeEventListener("scroll",j)},[]);const a=Ne[l],s=24,o=Math.max(1,Math.floor((x-s)/(a.w+a.gapX))),n=r.useCallback(i=>({x:s+i%o*(a.w+a.gapX),y:s+Math.floor(i/o)*(a.h+a.gapY)}),[o,a,s]),h=Math.ceil(t.length/o),d=Math.max(600,s+h*(a.h+a.gapY)+80),z=Math.max(x,s+o*(a.w+a.gapX)+s),N=r.useCallback((i,j)=>{y(),f(i,j)},[f,y]),v=r.useCallback(i=>{y(),g(i)},[g,y]);return t.length===0?e.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center text-center px-8",children:[e.jsx("div",{className:"text-5xl mb-4",children:"ð"}),e.jsx("h3",{className:"text-lg font-semibold text-foreground mb-2",children:"Your board is empty"}),e.jsx("p",{className:"text-sm text-muted-foreground max-w-sm mb-4",children:'Type an English word above to create your first Mandarin flashcard. Try "hello", "thank you", or "cake".'}),e.jsx("div",{className:"flex gap-2 flex-wrap justify-center",children:["hello","thank you","cake","go to work"].map(i=>e.jsx("span",{className:"text-xs px-2.5 py-1 rounded-full bg-primary/10 text-primary curs
1or-default",children:i},i))})]}):e.jsx("div",{ref:m,className:"flex-1 overflow-auto",style:{position:"relative"},children:e.jsxs("div",{className:"relative",style:{width:z,height:d,minHeight:"100%"},children:[e.jsx("div",{className:"absolute inset-0 opacity-[0.04] pointer-events-none",style:{backgroundImage:"radial-gradient(circle, hsl(var(--foreground)) 1px, transparent 1px)",backgroundSize:"20px 20px"}}),t.map((i,j)=>{const k=n(j),M={...i,position_x:k.x,position_y:k.y};return e.jsx(je,{card:M,size:l,snapToGrid:p,onUpdate:N,onDelete:v,globalFlip:u},i.id)})]})})}function W({cards:t,emptyLabel:l,emptyIcon:p,onUpdate:f,onDelete:g}){return t.length===0?e.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center text-center px-8",children:[e.jsx("div",{className:"text-4xl mb-3",children:p}),e.jsx("p",{className:"text-sm text-muted-foreground",children:l})]}):e.jsx("div",{className:"flex-1 overflow-auto p-4",children:e.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-3",children:t.map(u=>e.jsx(we,{card:u,onUpdate:f,onDelete:g},u.id))})})}function we({card:t,onUpdate:l,onDelete:p}){var w;const f=t.state==="parked"?[{label:"Board",icon:O,state:"board"},{label:"Deck",icon:V,state:"deck"}]:[{label:"Board",icon:O,state:"board"},{label:"Park",icon:q,state:"parked"}],g=((w=t.translations)==null?void 0:w.length)>0?t.translations:[{simplified:t.chinese_simplified,pinyin:t.pinyin}],u=t.primary_translation||g[0],m=g.filter(x=>x.simplified!==u.simplified);return e.jsxs(I.div,{layout:!0,initial:{opacity:0,scale:.95},animate:{opacity:1,scale:1},className:"group relative rounded-xl border border-border/40 bg-card/80 backdrop-blur-sm p-3 hover:border-primary/30 transition-colors cursor-pointer",onClick:()=>l(t.id,{is_flipped:!t.is_flipped}),children:[t.is_flagged&&e.jsx(B,{className:"absolute top-2 right-2 w-3 h-3 text-destructive fill-destructive"}),t.is_flipped?e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-lg font-bold text-foreground",children:u.simplified}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:u.pinyin}),m.length>0&&e.jsx("div",{className:"mt-1 space-y-0.5",children:m.slice(0,2).map((x,_)=>e.jsxs("p",{className:"text-[10px] text-muted-foreground/60",children:[x.simplified," ",x.pinyin]},_))})]}):e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm font-semibold text-foreground",children:t.english_text}),t.tags.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1 mt-1.5 justify-center",children:t.tags.slice(0,2).map(x=>e.jsx("span",{className:"text-[8px] px-1.5 py-0.5 rounded-full bg-primary/10 text-primary",children:x},x))})]}),e.jsxs("div",{className:"absolute -bottom-1 left-1/2 -translate-x-1/2 opacity-0 group-hover:opacity-100 transition-opacity flex gap-0.5 bg-popover/95 border border-border rounded-md px-1 py-0.5 shadow-md z-20",children:[f.map(({label:x,icon:_,state:y})=>e.jsx("button",{onClick:a=>{a.stopPropagation(),l(t.id,{state:y})},className:"p-1 rounded hover:bg-accent/50",title:x,children:e.jsx(_,{className:"w-3 h-3 text-muted-foreground"})},x)),e.jsx("button",{onClick:x=>{x.stopPropagation(),l(t.id,{is_flagged:!t.is_flagged})},className:"p-1 rounded hover:bg-accent/50",title:"Flag",children:e.jsx(B,{className:`w-3 h-3 ${t.is_flagged?"text-destructive fill-destructive":"text-muted-foreground"}`})}),e.jsx("button",{onClick:x=>{x.stopPropagation(),p(t.id)},className:"p-1 rounded hover:bg-destructive/20",title:"Delete",children:e.jsx(se,{className:"w-3 h-3 text-destructive"})})]})]})}const Q=6,ke=220,Ce=170,J=30;function Z(t){return{x:t%Q*ke+J,y:Math.floor(t/Q)*Ce+J}}function U(t){return t.trim().toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g," ")}function ee(t){return t?Array.isArray(t)?t.filter(l=>(l==null?void 0:l.simplified)&&(l==null?void 0:l.pinyin)):[]:[]}function te(t){return!t||!t.simplified?null:{simplified:t.simplified,pinyin:t.pinyin}}function Se(){const{user:t}=ae(),[l,p]=r.useState([]),[f,g]=r.useState(!0),u=r.useCallback(async()=>{if(!t)return;const{data:a,error:s}=await F.from("mandarin_cards").select("*").eq("user_id",t.id).order("created_at",{ascending:!1});s?console.error("Failed to fetch cards:",s):p((a??[]).map(o=>
1({...o,translations:ee(o.translations),primary_translation:te(o.primary_translation)}))),g(!1)},[t]);r.useEffect(()=>{u()},[u]);const m=r.useCallback(async a=>{const s=U(a),{data:o}=await F.from("mandarin_dictionary").select("english_text, chinese_simplified, pinyin").eq("english_normalized",s).maybeSingle();if(o)return{english:s,simplified:o.chinese_simplified,pinyin:o.pinyin,translations:[{simplified:o.chinese_simplified,pinyin:o.pinyin}],primaryIndex:0};const{data:n,error:h}=await F.functions.invoke("translate-mandarin",{body:{english:a.trim()}});return h||!(n!=null&&n.simplified)?($.error((n==null?void 0:n.error)||"Translation failed â please try again"),null):(await F.from("mandarin_dictionary").upsert({english_normalized:s,english_text:a.trim(),chinese_simplified:n.simplified,pinyin:n.pinyin,source:"ai_confirmed"},{onConflict:"english_normalized"}).then(({error:d})=>{d&&console.warn("Failed to cache translation:",d)}),{english:n.english||s,simplified:n.simplified,pinyin:n.pinyin,translations:n.translations||[{simplified:n.simplified,pinyin:n.pinyin}],primaryIndex:n.primaryIndex??0})},[]),w=r.useCallback(async(a,s=[],o,n)=>{if(!t)return null;let h=o,d=n;if(!h){const k=await m(a);if(!k)return null;h=k.translations,d=h[k.primaryIndex]||h[0]}d||(d=h[0]);const z=l.filter(k=>k.state==="board"),N=Z(z.length),{data:v,error:i}=await F.from("mandarin_cards").insert({user_id:t.id,english_text:a.trim(),english_normalized:U(a),chinese_simplified:d.simplified,pinyin:d.pinyin,translations:h,primary_translation:d,tags:s,state:"board",position_x:N.x,position_y:N.y,sort_index:Date.now()}).select().single();if(i)return $.error("Failed to save card"),null;const j={...v,translations:ee(v.translations),primary_translation:te(v.primary_translation)};return p(k=>[j,...k]),v},[t,l,m]),x=r.useCallback(async(a,s)=>{p(h=>h.map(d=>d.id===a?{...d,...s}:d));const o={...s,updated_at:new Date().toISOString()};s.translations&&s.primary_translation&&(o.chinese_simplified=s.primary_translation.simplified,o.pinyin=s.primary_translation.pinyin);const{error:n}=await F.from("mandarin_cards").update(o).eq("id",a);n&&(console.error("Update failed:",n),u())},[u]),_=r.useCallback(async a=>{p(o=>o.filter(n=>n.id!==a));const{error:s}=await F.from("mandarin_cards").delete().eq("id",a);s&&($.error("Failed to delete card"),u())},[u]),y=r.useCallback(async()=>{const a=l.filter(n=>n.state==="board").sort((n,h)=>n.sort_index-h.sort_index),s=[...l],o=[];a.forEach((n,h)=>{const d=Z(h),z=s.findIndex(N=>N.id===n.id);z>=0&&(s[z]={...s[z],position_x:d.x,position_y:d.y}),o.push({id:n.id,position_x:d.x,position_y:d.y})}),p(s),await Promise.all(o.map(({id:n,position_x:h,position_y:d})=>F.from("mandarin_cards").update({position_x:h,position_y:d}).eq("id",n)))},[l]);return{cards:l,loading:f,addCard:w,updateCard:x,deleteCard:_,tidyBoard:y,refetch:u,translate:m}}const ze={snap_to_grid:!0,board_density:"comfortable",tidy_mode:"grid"};function Me(){const{user:t}=ae(),[l,p]=r.useState(ze);r.useEffect(()=>{t&&F.from("mandarin_station_settings").select("*").eq("user_id",t.id).maybeSingle().then(({data:g})=>{g&&p({snap_to_grid:g.snap_to_grid??!0,board_density:g.board_density??"comfortable",tidy_mode:g.tidy_mode??"grid"})})},[t]);const f=r.useCallback(async g=>{if(!t)return;const u={...l,...g};p(u),await F.from("mandarin_station_settings").upsert({user_id:t.id,...u,updated_at:new Date().toISOString()},{onConflict:"user_id"})},[t,l]);return{settings:l,updateSettings:f}}function Ee(){const t=ue(),{cards:l,loading:p,addCard:f,updateCard:g,deleteCard:u,tidyBoard:m,translate:w}=Se(),{settings:x,updateSettings:_}=Me(),[y,a]=r.useState("board"),[s,o]=r.useState(""),[n,h]=r.useState(!1),d=r.useMemo(()=>{let N=l.filter(v=>v.state===y);if(s.trim()){const v=s.toLowerCase();N=N.filter(i=>i.english_text.toLowerCase().includes(v)||i.chinese_simplified.includes(v)||i.pinyin.toLowerCase().includes(v)||i.tags.some(j=>j.toLowerCase().includes(v)))}return N},[l,y,s]),z=r.useCallback(async(N,v,i,j)=>f(N,v,i,j),[f]);return e.jsxs(I.div,{initial:{opacity:0}
1,animate:{opacity:1},className:"fixed inset-0 flex flex-col bg-background",children:[e.jsxs("div",{className:"absolute inset-0 pointer-events-none",children:[e.jsx("div",{className:"absolute inset-0 bg-gradient-to-br from-primary/[0.03] via-transparent to-secondary/[0.03]"}),e.jsx("div",{className:"absolute top-1/4 left-1/4 w-96 h-96 rounded-full bg-primary/[0.02] blur-3xl"}),e.jsx("div",{className:"absolute bottom-1/4 right-1/4 w-96 h-96 rounded-full bg-secondary/[0.02] blur-3xl"})]}),e.jsxs("div",{className:"relative z-10 flex items-center gap-3 px-4 py-2 border-b border-border/30 bg-card/60 backdrop-blur-md",children:[e.jsx(P,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>t("/universe"),children:e.jsx(xe,{className:"w-4 h-4"})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-lg",children:"ð"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-sm font-bold text-foreground leading-tight",children:"Mandarin Station"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Build your living Mandarin workspace"})]})]}),e.jsxs("div",{className:"ml-auto text-xs text-muted-foreground",children:[l.length," card",l.length!==1?"s":""]})]}),e.jsx("div",{className:"relative z-10",children:e.jsx(ye,{view:y,onViewChange:a,settings:x,onSettingsChange:_,searchQuery:s,onSearchChange:o,onTidy:m,onAddCard:z,onTranslate:w,flipAll:n,onFlipAllToggle:()=>h(N=>!N)})}),e.jsx("div",{className:"relative z-10 flex-1 flex flex-col min-h-0",children:p?e.jsx("div",{className:"flex-1 flex items-center justify-center",children:e.jsx("div",{className:"w-6 h-6 border-2 border-primary border-t-transparent rounded-full animate-spin"})}):y==="board"?e.jsx(_e,{cards:d,density:x.board_density,snapToGrid:x.snap_to_grid,onUpdate:g,onDelete:u,globalFlip:n}):y==="parked"?e.jsx(W,{cards:d,emptyLabel:"No parked cards. Move cards here to reduce board clutter.",emptyIcon:"ð¦",onUpdate:g,onDelete:u}):e.jsx(W,{cards:d,emptyLabel:"Your deck is empty. Mastered cards will appear here.",emptyIcon:"ð",onUpdate:g,onDelete:u})})]})}export{Ee as default};
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.