import type { AudioProcessor, ProcessorCategory } from "./AudioProcessor";

export class HaasEffect implements AudioProcessor {
  getName(): string {
    return "Haas Effect";
  }

  getDescription(): string {
    return "Creates stereo width with micro-delay (precedence effect)";
  }

  getCategory(): ProcessorCategory {
    return 'Space';
  }

  async process(
    leftChannel: Float32Array,
    rightChannel: Float32Array
  ): Promise<[Float32Array, Float32Array]> {
    const sampleRate = 44100;
    const delayMs = 5 + Math.random() * 25;
    const delaySamples = Math.floor((delayMs / 1000) * sampleRate);
    const attenuationDb = -1 - Math.random() * 2;
    const attenuation = Math.pow(10, attenuationDb / 20);

    const newLeft = new Float32Array(leftChannel.length);
    const newRight = new Float32Array(rightChannel.length);

    for (let i = 0; i < leftChannel.length; i++) {
      newLeft[i] = leftChannel[i];
    }

    for (let i = 0; i < rightChannel.length; i++) {
      if (i >= delaySamples) {
        newRight[i] = rightChannel[i - delaySamples] * attenuation;
      } else {
        newRight[i] = 0;
      }
    }

    return [newLeft, newRight];
  }
}
