// Deterministic seed helpers shared between GhostWaveform (track row) and
// the inline GhostLine rendered inside TrackLabel. We want the two to look
// like siblings — same shape, same envelope, same colour for the same track.

export function hashSeed(s: string): number {
  let h = 2166136261;
  for (let i = 0; i < s.length; i++) {
    h ^= s.charCodeAt(i);
    h = Math.imul(h, 16777619);
  }
  return h >>> 0;
}

export function mulberry32(seed: number) {
  return function () {
    seed |= 0;
    seed = (seed + 0x6d2b79f5) | 0;
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

/** Sample N bucket values for a given seed, in [0..1]. */
export function ghostBuckets(seed: number, n: number): Float32Array {
  const rand = mulberry32(seed);
  const arr = new Float32Array(n);
  for (let i = 0; i < n; i++) {
    const t = i / n;
    const wave = Math.sin(t * Math.PI * 4) * 0.4 + Math.sin(t * Math.PI * 8 + rand() * 6) * 0.2;
    const env = Math.sin(t * Math.PI) * 0.3 + 0.1;
    const noise = (rand() - 0.5) * 0.6;
    arr[i] = Math.max(0.05, Math.min(1, Math.abs(wave) * env + noise));
  }
  return arr;
}