'use client';

import { useVisualization } from '@/lib/visualizations/use-visualization';
import type { AlgorithmDefinition } from '@/lib/visualizations/types';
import { VisualizationContainer } from '../core/visualization-container';
import { IntervalView } from '../data-structures/interval-view';

interface IntervalsVisualizationProps {
  algorithm: AlgorithmDefinition;
  className?: string;
}

export function IntervalsVisualization({
  algorithm,
  className,
}: IntervalsVisualizationProps) {
  const {
    currentStep,
    currentStepIndex,
    totalSteps,
    playback,
    controls,
    currentPhase,
    progress,
  } = useVisualization(algorithm);

  const { dataState } = currentStep;
  const inputIntervals = dataState.intervals?.[0] ?? null;
  const resultIntervals = dataState.intervals?.[1] ?? null;

  return (
    <VisualizationContainer
      title={algorithm.title}
      pattern={algorithm.pattern}
      code={algorithm.code}
      currentLine={currentStep.codeLine}
      highlightLines={currentStep.codeHighlightLines}
      explanation={currentStep.explanation}
      decision={currentStep.decision}
      phase={currentPhase}
      variables={dataState.variables}
      currentStepIndex={currentStepIndex}
      totalSteps={totalSteps}
      isPlaying={playback.isPlaying}
      speed={playback.speed}
      controls={controls}
      progress={progress}
      className={className}
    >
      <div className="flex flex-col items-center gap-6">
        {/* Input intervals */}
        {inputIntervals && (
          <IntervalView intervalList={inputIntervals} />
        )}

        {/* Result intervals (shown when building merged result) */}
        {resultIntervals && resultIntervals.intervals.length > 0 && (
          <IntervalView intervalList={resultIntervals} />
        )}
      </div>
    </VisualizationContainer>
  );
}
