const w="/assets/sfx/key-down.m4a",g="/assets/sfx/key-up.m4a",m="sfxEnabled",S="sfxVolume";let i=localStorage.getItem(m)!=="false",n=parseFloat(localStorage.getItem(S)??"0.8");(Number.isNaN(n)||n<0||n>1)&&(n=.8);function x(){return i}function b(){return n}function _(e){i=e,localStorage.setItem(m,String(e))}function I(e){n=Math.max(0,Math.min(1,e)),localStorage.setItem(S,String(n))}let t=null,r=null,c=null,u=null,s=!1,l=!1;async function d(e,a){const y=await(await fetch(a)).arrayBuffer();return e.decodeAudioData(y)}async function p(){if(!(s||l)){l=!0;try{t=new AudioContext;const[e,a]=await Promise.all([d(t,w),d(t,g)]);r=e,c=a,s=!0}catch(e){console.warn("[keycapSfx] Failed to load audio buffers:",e)}finally{l=!1}}}function f(e){const a=t.createGain();return a.gain.value=e,a}function A(){if(!i)return;if(!s||!t||!r){p();return}t.state==="suspended"&&t.resume();const e=t.createBufferSource();e.buffer=r,e.connect(f(n)).connect(t.destination),e.start(t.currentTime+.01),u=e}function B(){if(!i||!s||!t||!c)return;if(t.state==="suspended"&&t.resume(),u){try{u.stop()}catch{}u=null}const e=t.createBufferSource();e.buffer=c,e.connect(f(n*.75)).connect(t.destination),e.start()}async function k(){if((!s||!t||!r||!c)&&(await p(),!s||!t||!r||!c))return;t.state==="suspended"&&await t.resume();const e=t.createBufferSource();return e.buffer=r,e.connect(f(n)).connect(t.destination),e.start(t.currentTime+.01),new Promise(a=>{setTimeout(()=>{try{e.stop()}catch{}const o=t.createBufferSource();o.buffer=c,o.connect(f(n*.75)).connect(t.destination),o.start(),o.onended=()=>a()},120)})}export{I as a,A as b,B as c,b as g,x as i,k,_ as s};
