import R from 'ramda';
import inside from 'point-in-polygon';
import Vector from 'immutable-vector2d';

import CircuitComponents from './components/AllViews.js';
import DragPoint from './DragPoint.js';
import { BOUNDING_BOX_PADDING } from './Constants.js';
import { getRectPointsBetween } from '../utils/DrawingUtils.js';

// Bounding box stuff

/*
 * A bounding box is represented e.g. [[1, 2], [3, 4], [5, 6]]
 */

export const get2PointBoundingBox = width => connectors => {
  const [p1, p2] = connectors;
  const rectanglePoints = getRectPointsBetween(p1, p2, width + 2 * BOUNDING_BOX_PADDING);
  return R.map(p => [p.x, p.y], rectanglePoints);
};

function isPointIn(p: Vector, polygon) {
  const point = [p.x, p.y];
  return inside(point, polygon);
}

export const hoverFor = (mousePos: Vector) => (typeID, dragPoints) => {
  const CircuitComp = CircuitComponents[typeID];

  const hoveredDragPointIndex = R.findIndex(DragPoint.isPointIn(mousePos), dragPoints);
  const isIndex = R.is(Number, hoveredDragPointIndex) && hoveredDragPointIndex >= 0;
  return {
    hovered: isPointIn(mousePos, CircuitComp.getBoundingBox(dragPoints)) || isIndex,
    dragPointIndex: isIndex ? hoveredDragPointIndex : false
  };
};

@@ -224,16 +224,35 @@
 _PADDING
+, DRAG_POINT_RADIUS
  %7D from 
@@ -265,24 +265,24 @@
 stants.js';%0A
-
 import %7B get
@@ -335,16 +335,189 @@
 s.js';%0A%0A
+const MIN_WIDTH = DRAG_POINT_RADIUS * 2;%0Aconst sanitise = width =%3E %7B%0A  width = width %3E MIN_WIDTH%0A    ? width%0A    : MIN_WIDTH;%0A  return width + 2 * BOUNDING_BOX_PADDING;%0A%7D;%0A%0A
 // Bound
@@ -694,16 +694,53 @@
 ectors;%0A
+  const fullWidth = sanitise(width);%0A
   const 
@@ -790,40 +790,17 @@
 p2, 
-width + 2 * BOUNDING_BOX_PADDING
+fullWidth
 );%0A 

Increase min width for bounding box