import {pickVisit} from '../visit';

var trueFunc = function() { return true; };

export function pick(test) {
  if (!test) test = trueFunc;

  return function(context, scene, x, y, gx, gy) {
    x *= context.pixelRatio;
    y *= context.pixelRatio;

    return pickVisit(scene, function(item) {
      var b = item.bounds;
      // first hit test against bounding box
      if ((b && !b.contains(gx, gy)) || !b) return;
      // if in bounding box, perform more careful test
      if (test(context, item, x, y, gx, gy)) return item;
    });
  };
}

export function hitPath(path, filled) {
  return function(context, o, x, y) {
    var item = Array.isArray(o) ? o[0] : o,
        fill = (filled == null) ? item.fill : filled,
        stroke = item.stroke && context.isPointInStroke, lw, lc;

    if (stroke) {
      lw = item.strokeWidth;
      lc = item.strokeCap;
      context.lineWidth = lw != null ? lw : 1;
      context.lineCap   = lc != null ? lc : 'butt';
    }

    return path(context, o) ? false :
      (fill && context.isPointInPath(x, y)) ||
      (stroke && context.isPointInStroke(x, y));
  };
}

export function pickPath(path) {
  return pick(hitPath(path));
}

@@ -33,51 +33,40 @@
 t';%0A
-%0Avar trueFunc = function() %7B return true; %7D
+import %7Btruthy%7D from 'vega-util'
 ;%0A%0Ae
@@ -99,34 +99,29 @@
 %7B%0A  
-if (!
 test
-)
+ =
  test 
-=
+%7C%7C
  tru
-eFunc
+thy
 ;%0A%0A 

Update pick utils to use vega-util.