import { CURRENT } from '../Constants';

import CircuitComponents from '../components';
const lookupComponent = viewProps => CircuitComponents[viewProps.typeID];

const TWO_PI = 2 * Math.PI;

const distance = (v1, v2) => {
  const d = {
    x: v1.x - v2.x,
    y: v1.y - v2.y
  };
  return Math.sqrt(d.x * d.x + d.y * d.y);
};

const lerp = (n1, n2, d) => ((1 - d) * n1) + (d * n2);

const renderCurrent = (ctx, circuitState) => (component) => {
  const renderBetween = (p1, p2, startPos) => {
    const length = distance(p1, p2);
    for (let position = startPos; position <= length; position += CURRENT.DOT_DISTANCE) {
      const d = position / length;
      const x = lerp(p1.x, p2.x, d);
      const y = lerp(p1.y, p2.y, d);

      ctx.beginPath();
      ctx.arc(x, y, CURRENT.RADIUS, 0, TWO_PI);
      ctx.fill();
    }
  };

  const ComponentType = lookupComponent(component);
  ComponentType.renderCurrent(component, circuitState[component.id], renderBetween);
};

export default function ({ctx, theme, circuitState, components}) {
  const render = renderCurrent(ctx, circuitState);

  ctx.save();
  ctx.fillStyle = theme.COLORS.current;

  components.forEach(component => {
    const ComponentType = lookupComponent(component);
    ComponentType.transform.transformCanvas(ctx, component,
      () => render(component)
    );
  });

  ctx.restore();
}

@@ -393,81 +393,26 @@
 nder
-Current = (ctx, circuitState)
+Between
  =
-%3E
  (c
-omponent) =%3E %7B%0A  const renderBetween =
+tx) =%3E
  (p1
@@ -428,26 +428,24 @@
 rtPos) =%3E %7B%0A
-  
   const leng
@@ -467,18 +467,16 @@
 1, p2);%0A
-  
   for (l
@@ -555,26 +555,24 @@
 ANCE) %7B%0A    
-  
 const d = po
@@ -588,26 +588,24 @@
 length;%0A    
- 
- 
 const x = le
@@ -623,18 +623,16 @@
 .x, d);%0A
-  
     cons
@@ -659,26 +659,24 @@
 y, d);%0A%0A    
- 
- 
 ctx.beginPat
@@ -680,18 +680,16 @@
 Path();%0A
-  
     ctx.
@@ -730,18 +730,16 @@
 I);%0A    
-  
 ctx.fill
@@ -748,17 +748,75 @@
 ;%0A  
-  %7D%0A  %7D;%0A
+%7D%0A%7D;%0A%0Aconst renderCurrent = (ctx, circuitState) =%3E (component) =%3E %7B
 %0A  c
@@ -946,16 +946,21 @@
 rBetween
+(ctx)
 );%0A%7D;%0A%0Ae

Remove some indentation