/* global React */
// Tiny WebAudio "sound design" — warm, muted taps. No external files.

function makeSfx() {
  let ctx = null;
  function ensure() {
    if (!ctx) {
      try { ctx = new (window.AudioContext || window.webkitAudioContext)(); } catch { ctx = null; }
    }
    if (ctx && ctx.state === "suspended") ctx.resume();
    return ctx;
  }
  function tone({ freq = 440, dur = 0.16, type = "sine", gain = 0.08, attack = 0.005, decay = 0.12, detune = 0 }) {
    const c = ensure(); if (!c) return;
    const t0 = c.currentTime;
    const osc = c.createOscillator();
    const g = c.createGain();
    osc.type = type;
    osc.frequency.value = freq;
    osc.detune.value = detune;
    osc.connect(g); g.connect(c.destination);
    g.gain.setValueAtTime(0, t0);
    g.gain.linearRampToValueAtTime(gain, t0 + attack);
    g.gain.exponentialRampToValueAtTime(0.0001, t0 + attack + decay);
    osc.start(t0);
    osc.stop(t0 + attack + decay + 0.02);
  }
  return {
    tap:   () => tone({ freq: 520, type: "sine",    dur: 0.12, gain: 0.06, decay: 0.09 }),
    soft:  () => tone({ freq: 280, type: "triangle",dur: 0.16, gain: 0.05, decay: 0.12 }),
    pop:   () => { tone({ freq: 720, type: "sine", gain: 0.07, decay: 0.08 }); setTimeout(() => tone({ freq: 960, type: "sine", gain: 0.05, decay: 0.1 }), 50); },
    grow:  () => { [220, 330, 440, 587, 740].forEach((f, i) => setTimeout(() => tone({ freq: f, type: "sine", gain: 0.05, decay: 0.18 }), i * 110)); },
    bloom: () => { [440, 554, 659, 880].forEach((f, i) => setTimeout(() => tone({ freq: f, type: "triangle", gain: 0.06, decay: 0.28 }), i * 80)); },
    chime: () => { [659, 784, 988, 1319].forEach((f, i) => setTimeout(() => tone({ freq: f, type: "sine", gain: 0.05, decay: 0.4 }), i * 90)); },
    ding:  () => tone({ freq: 988, type: "sine", gain: 0.07, decay: 0.5 })
  };
}

const SFX = makeSfx();
Object.assign(window, { SFX });
