// Per-element MediaElementSourceNode cache.
// See ADR-004 §B. WeakMap so entries die with the audio element.

import { getAudioContext } from './audioContext';

const cache = new WeakMap<HTMLAudioElement, MediaElementAudioSourceNode>();

export function getMediaElementSource(el: HTMLAudioElement): MediaElementAudioSourceNode {
  const hit = cache.get(el);
  if (hit) return hit;
  const ctx = getAudioContext();
  const node = ctx.createMediaElementSource(el);
  node.connect(ctx.destination);
  cache.set(el, node);
  return node;
}

export function hasMediaElementSource(el: HTMLAudioElement): boolean {
  return cache.has(el);
}
