import { secondsToPixels, pixelsToSeconds } from '../utils/conversions';

/*
 * virtual-dom hook for scrolling the track container.
 */
export default class {
  constructor(playlist) {
    this.playlist = playlist;
  }

  hook(node) {
    const playlist = this.playlist;
    if (!playlist.isScrolling) {
      const el = node;

      if (playlist.isAutomaticScroll) {
        const rect = node.getBoundingClientRect();
        const controlWidth = playlist.controls.show
          ? playlist.controls.width
          : 0;
        const width = pixelsToSeconds(
          rect.width - controlWidth,
          playlist.samplesPerPixel,
          playlist.sampleRate,
        );

        if (playlist.isPlaying()) {
          if (playlist.playbackSeconds < playlist.scrollLeft || playlist.playbackSeconds >= (playlist.scrollLeft + width)) {
            playlist.scrollLeft = Math.min(
              playlist.playbackSeconds,
              playlist.duration - width,
            );
          }
        } else {
          const selection = playlist.getTimeSelection();

          if (selection.start < playlist.scrollLeft || selection.start >= (playlist.scrollLeft + width)) {
            playlist.scrollLeft = Math.min(
              selection.start,
              playlist.duration - width,
            );
          }
        }
      }

      const left = secondsToPixels(
        playlist.scrollLeft,
        playlist.samplesPerPixel,
        playlist.sampleRate,
      );

      el.scrollLeft = left;
    }
  }
}

@@ -42,17 +42,17 @@
  %7D from 
-'
+%22
 ../utils
@@ -67,9 +67,9 @@
 ions
-'
+%22
 ;%0A%0A/
@@ -652,25 +652,24 @@
 t.sampleRate
-,
 %0A        );%0A
@@ -681,129 +681,26 @@
     
-if (playlist.isPlaying()) %7B%0A          if (playlist.playbackSeconds %3C playlist.scrollLeft %7C%7C playlist.playbackSeconds %3E
+const timePoint 
 = 
-(
 play
@@ -708,74 +708,19 @@
 ist.
-scrollLeft + width)) %7B%0A            playlist.scrollLeft = Math.min(
+isPlaying()
 %0A   
@@ -718,35 +718,33 @@
 ing()%0A          
-   
+?
  playlist.playba
@@ -752,22 +752,17 @@
 kSeconds
-,%0A    
+%0A
         
@@ -767,106 +767,9 @@
     
-playlist.duration - width,%0A            );%0A          %7D%0A        %7D else %7B%0A          const selection =
+:
  pla
@@ -792,16 +792,22 @@
 ection()
+.start
 ;%0A%0A     
@@ -813,28 +813,31 @@
     
- 
- 
 if (
-selection.star
+%0A          timePoin
 t %3C 
@@ -862,29 +862,32 @@
 t %7C%7C
- selection.star
+%0A          timePoin
 t %3E= 
-(
 play
@@ -913,15 +913,21 @@
 idth
-)
+%0A        
 ) %7B%0A
-  
     
@@ -967,107 +967,46 @@
 min(
-%0A              selection.start,%0A              playlist.duration - width,%0A            );%0A          %7D
+timePoint, playlist.duration - width);
 %0A   
@@ -1147,17 +1147,16 @@
 mpleRate
-,
 %0A      )

refactor ScrollHook