import {
  drag_default
} from "./chunk-KYBIQBXE.js";
import {
  pointer_default
} from "./chunk-I6Y4O3RR.js";

// common/lasso.js
function d3lasso() {
  let items = [], targetArea, on = { start: function() {
  }, draw: function() {
  }, end: function() {
  } };
  function lasso(_this) {
    const g = _this.append("g").attr("class", "lasso");
    const drawn_path = g.append("path").attr("class", "drawn").style("stroke", "#505050").style("stroke-width", "2px").style("fill-opacity", ".05");
    const origin_node = g.append("circle").attr("class", "origin").style("fill", "#3399FF").style("fill-opacity", ".5");
    let tpath, origin, torigin, drawnCoords;
    let drag = drag_default().on("start", dragstart).on("drag", dragmove).on("end", dragend);
    targetArea.call(drag);
    function dragstart(event) {
      drawnCoords = [];
      tpath = "";
      drawn_path.attr("d", null);
      items.nodes().forEach(function(e) {
        e.__lasso.possible = false;
        e.__lasso.selected = false;
        e.__lasso.loopSelect = false;
        let box = e.getBoundingClientRect();
        e.__lasso.lassoPoint = [Math.round(box.left + box.width / 2), Math.round(box.top + box.height / 2)];
      });
      on.start(event);
    }
    function dragmove(event) {
      let x, y;
      if (event.sourceEvent.type === "touchmove") {
        x = event.sourceEvent.touches[0].clientX;
        y = event.sourceEvent.touches[0].clientY;
      } else {
        x = event.sourceEvent.clientX;
        y = event.sourceEvent.clientY;
      }
      let [tx, ty] = pointer_default(event, this);
      if (tpath === "") {
        tpath = tpath + "M " + tx + " " + ty;
        origin = [x, y];
        torigin = [tx, ty];
        origin_node.attr("cx", tx).attr("cy", ty).attr("r", 7).attr("display", null);
      } else {
        tpath = tpath + " L " + tx + " " + ty;
      }
      drawnCoords.push([x, y]);
      drawn_path.attr("d", tpath);
      items.nodes().forEach(function(n) {
        n.__lasso.loopSelect = pointInPolygon(n.__lasso.lassoPoint, drawnCoords);
        n.__lasso.possible = n.__lasso.loopSelect;
      });
      on.draw(event);
    }
    function dragend(event) {
      items.on("mouseover.lasso", null);
      items.nodes().forEach(function(n) {
        n.__lasso.selected = n.__lasso.possible;
        n.__lasso.possible = false;
      });
      drawn_path.attr("d", null);
      origin_node.attr("display", "none");
      on.end(event);
    }
    function pointInPolygon(point, polygon) {
      let xi, xj, yi, yj, intersect, x = point[0], y = point[1], inside = false;
      for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
        xi = polygon[i][0];
        yi = polygon[i][1];
        xj = polygon[j][0];
        yj = polygon[j][1];
        intersect = yi > y != yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi;
        if (intersect) inside = !inside;
      }
      return inside;
    }
  }
  lasso.items = function(_) {
    if (!arguments.length) return items;
    items = _;
    const nodes = items.nodes();
    nodes.forEach(function(n) {
      n.__lasso = {
        possible: false,
        selected: false
      };
    });
    return lasso;
  };
  lasso.possibleItems = function() {
    return items.filter(function() {
      return this.__lasso.possible;
    });
  };
  lasso.selectedItems = function() {
    return items.filter(function() {
      return this.__lasso.selected;
    });
  };
  lasso.notPossibleItems = function() {
    return items.filter(function() {
      return !this.__lasso.possible;
    });
  };
  lasso.notSelectedItems = function() {
    return items.filter(function() {
      return !this.__lasso.selected;
    });
  };
  lasso.on = function(type, _) {
    if (!arguments.length) return on;
    if (arguments.length === 1) return on[type];
    const types = ["start", "draw", "end"];
    if (types.indexOf(type) > -1) {
      on[type] = _;
    }
    return lasso;
  };
  lasso.targetArea = function(_) {
    if (!arguments.length) return targetArea;
    targetArea = _;
    return lasso;
  };
  return lasso;
}

export {
  d3lasso
};
//# sourceMappingURL=chunk-K5XPMCKP.js.map
