import{bj as g,s as U}from"./index-DzVf5QXp.js";const b="https://pldrgdplxacehsmcyjnx.supabase.co",f="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBsZHJnZHBseGFjZWhzbWN5am54Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDM0MzU5MzEsImV4cCI6MjA1OTAxMTkzMX0.YnKo7pfbjg52NF6TL2pNilHkND5ZgkhbDuAtC_-IQL4";let y=0;const m=500,i=new Map;let w=!1;const R=()=>{if(!w){w=!0;try{U.auth.getSession().catch(()=>{}),fetch(`${b}/functions/v1/pronounce-word`,{method:"OPTIONS",headers:{"Access-Control-Request-Method":"POST","Access-Control-Request-Headers":"authorization, content-type, apikey"}}).catch(()=>{})}catch{}}};let d=null;const I=()=>{if(d)return d;const e=8e3,a=400,c=new ArrayBuffer(44+a),t=new DataView(c),o=(s,u)=>{for(let n=0;n<u.length;n++)t.setUint8(s+n,u.charCodeAt(n))};o(0,"RIFF"),t.setUint32(4,36+a,!0),o(8,"WAVE"),o(12,"fmt "),t.setUint32(16,16,!0),t.setUint16(20,1,!0),t.setUint16(22,1,!0),t.setUint32(24,e,!0),t.setUint32(28,e,!0),t.setUint16(32,1,!0),t.setUint16(34,8,!0),o(36,"data"),t.setUint32(40,a,!0);for(let s=0;s<a;s++)t.setUint8(44+s,128);return d=URL.createObjectURL(new Blob([c],{type:"audio/wav"})),d},k=()=>{const e=new Audio;return e.setAttribute("playsinline","true"),e.src=I(),e.play().catch(()=>{}),e},E=async(e,a)=>{try{if(!e)return;const c=g(e);Date.now()-y<m&&await new Promise(l=>setTimeout(l,m));let o;if(i.has(e))console.log(`Using cached audio for "${e}"`),o=i.get(e);else{y=Date.now(),console.log(`Requesting pronunciation for: "${e}"`);const{data:{session:l}}=await U.auth.getSession(),p=l?.access_token||f,r=await fetch(`${b}/functions/v1/pronounce-word`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${p}`,apikey:f},body:JSON.stringify({word:e})});if(!r.ok){const h=await r.text();throw console.error("Edge function error:",r.status,h),new Error("Failed to get pronunciation from server")}if(o=await r.arrayBuffer(),o.byteLength===0)throw new Error("Received empty audio data");console.log(`Received audio data: ${o.byteLength} bytes`),i.set(e,o),i.size>50&&Array.from(i.keys()).slice(0,10).forEach(A=>i.delete(A))}const s=new Blob([o],{type:"audio/mp3"}),u=URL.createObjectURL(s),n=a??new Audio;n.setAttribute("playsinline","true"),n.src=u,await new Promise(l=>{const p=()=>{URL.revokeObjectURL(u),l()};n.onended=p,n.onerror=p,n.play().catch(r=>{console.error("Pronunciation playback blocked:",r?.name,r?.message),p()})})}catch(c){throw console.error("Speech synthesis error:",c),new Error("Failed to pronounce word")}};export{k as c,E as p,R as w};