import {
  ol_coordinate_dist2d,
  ol_coordinate_equal
} from "./chunk-3UNEODO2.js";
import {
  Vector_default as Vector_default2
} from "./chunk-PD2E5XZ4.js";
import {
  element_default
} from "./chunk-S3QBQTEW.js";
import {
  LineString_default
} from "./chunk-7JXPN73Q.js";
import {
  Feature_default
} from "./chunk-E53S5GN6.js";
import {
  Vector_default
} from "./chunk-T3TT2KJN.js";
import {
  Interaction_default
} from "./chunk-MSWSBYBR.js";
import {
  altKeyOnly,
  always,
  primaryAction
} from "./chunk-QCJTGAWF.js";
import {
  Circle_default,
  Fill_default,
  Stroke_default,
  Style_default
} from "./chunk-PAB2HIXK.js";
import {
  Point_default
} from "./chunk-AZGMK675.js";
import {
  boundingExtent,
  buffer
} from "./chunk-SRXHWJOY.js";

// node_modules/ol-ext/geom/LineStringSplitAt.js
LineString_default.prototype.splitAt = function(pt, tol) {
  var i;
  if (!pt) return [this];
  if (!tol) tol = 1e-10;
  if (pt.length && pt[0].length) {
    var result = [this];
    for (i = 0; i < pt.length; i++) {
      var r = [];
      for (var k = 0; k < result.length; k++) {
        var ri = result[k].splitAt(pt[i], tol);
        r = r.concat(ri);
      }
      result = r;
    }
    return result;
  }
  if (ol_coordinate_equal(pt, this.getFirstCoordinate()) || ol_coordinate_equal(pt, this.getLastCoordinate())) {
    return [this];
  }
  var c0 = this.getCoordinates();
  var ci = [c0[0]];
  var c = [];
  for (i = 0; i < c0.length - 1; i++) {
    if (ol_coordinate_equal(c0[i], c0[i + 1])) continue;
    if (ol_coordinate_equal(pt, c0[i + 1])) {
      ci.push(c0[i + 1]);
      c.push(new LineString_default(ci));
      ci = [];
    } else if (!ol_coordinate_equal(pt, c0[i])) {
      var d1, d2, split = false;
      if (c0[i][0] == c0[i + 1][0]) {
        d1 = (c0[i][1] - pt[1]) / (c0[i][1] - c0[i + 1][1]);
        split = c0[i][0] == pt[0] && (0 < d1 && d1 <= 1);
      } else if (c0[i][1] == c0[i + 1][1]) {
        d1 = (c0[i][0] - pt[0]) / (c0[i][0] - c0[i + 1][0]);
        split = c0[i][1] == pt[1] && (0 < d1 && d1 <= 1);
      } else {
        d1 = (c0[i][0] - pt[0]) / (c0[i][0] - c0[i + 1][0]);
        d2 = (c0[i][1] - pt[1]) / (c0[i][1] - c0[i + 1][1]);
        split = Math.abs(d1 - d2) <= tol && 0 < d1 && d1 <= 1;
      }
      if (split) {
        ci.push(pt);
        c.push(new LineString_default(ci));
        ci = [pt];
      }
    }
    ci.push(c0[i + 1]);
  }
  if (ci.length > 1) c.push(new LineString_default(ci));
  if (c.length) return c;
  else return [this];
};

// node_modules/ol-ext/interaction/ModifyFeature.js
var ol_interaction_ModifyFeature = class olinteractionModifyFeature extends Interaction_default {
  constructor(options) {
    options = options || {};
    var dragging, modifying;
    super({
      handleEvent: function(e) {
        switch (e.type) {
          case "pointerdown": {
            dragging = this.handleDownEvent(e);
            modifying = dragging || this._deleteCondition(e);
            return !dragging;
          }
          case "pointerup": {
            dragging = false;
            return this.handleUpEvent(e);
          }
          case "pointerdrag": {
            if (dragging)
              return this.handleDragEvent(e);
            else
              return true;
          }
          case "pointermove": {
            if (!dragging) {
              return this.handleMoveEvent(e);
            } else {
              return false;
            }
          }
          case "singleclick":
          case "click": {
            return !modifying;
          }
          default:
            return true;
        }
      }
    });
    this.snapDistance_ = options.pixelTolerance || 10;
    this.tolerance_ = 1e-10;
    this.cursor_ = options.cursor;
    this.sources_ = options.sources ? options.sources instanceof Array ? options.sources : [options.sources] : [];
    if (options.source) {
      this.sources_.push(options.source);
    }
    if (options.features) {
      this.sources_.push(new Vector_default2({ features: options.features }));
    }
    this.filterSplit_ = options.filter || function() {
      return true;
    };
    this._condition = options.condition || primaryAction;
    this._deleteCondition = options.deleteCondition || altKeyOnly;
    this._insertVertexCondition = options.insertVertexCondition || always;
    var sketchStyle = function() {
      return [
        new Style_default({
          image: new Circle_default({
            radius: 6,
            fill: new Fill_default({ color: [0, 153, 255, 1] }),
            stroke: new Stroke_default({ color: "#FFF", width: 1.25 })
          })
        })
      ];
    };
    if (options.style) {
      if (typeof options.style === "function") {
        sketchStyle = options.style;
      } else {
        sketchStyle = function() {
          return options.style;
        };
      }
    }
    this.overlayLayer_ = new Vector_default({
      source: new Vector_default2({
        useSpatialIndex: false
      }),
      name: "Modify overlay",
      displayInLayerSwitcher: false,
      style: sketchStyle,
      wrapX: options.wrapX
    });
  }
  /**
   * Remove the interaction from its current map, if any,  and attach it to a new
   * map, if any. Pass `null` to just remove the interaction from the current map.
   * @param {ol.Map} map Map.
   * @api stable
   */
  setMap(map) {
    if (this.getMap()) this.getMap().removeLayer(this.overlayLayer_);
    super.setMap(map);
    this.overlayLayer_.setMap(map);
  }
  /**
   * Activate or deactivate the interaction + remove the sketch.
   * @param {boolean} active.
   * @api stable
   */
  setActive(active) {
    super.setActive(active);
    if (this.overlayLayer_) this.overlayLayer_.getSource().clear();
  }
  /** Change the filter function
   * @param {function|undefined} options.filter a filter that takes a feature and return true if it can be modified, default always true.
   */
  setFilter(filter) {
    if (typeof filter === "function")
      this.filterSplit_ = filter;
    else if (filter === void 0)
      this.filterSplit_ = function() {
        return true;
      };
  }
  /** Get closest feature at pixel
   * @param {ol.Pixel}
   * @return {*}
   * @private
   */
  getClosestFeature(e) {
    var f, c, d = this.snapDistance_ + 1;
    for (var i = 0; i < this.sources_.length; i++) {
      var source = this.sources_[i];
      f = source.getClosestFeatureToCoordinate(e.coordinate);
      if (f && this.filterSplit_(f)) {
        var ci = f.getGeometry().getClosestPoint(e.coordinate);
        var di = ol_coordinate_dist2d(e.coordinate, ci) / e.frameState.viewState.resolution;
        if (di < d) {
          d = di;
          c = ci;
        }
        break;
      }
    }
    if (d > this.snapDistance_) {
      if (this.currentFeature)
        this.dispatchEvent({ type: "select", selected: [], deselected: [this.currentFeature] });
      this.currentFeature = null;
      return false;
    } else {
      var coord = this.getNearestCoord(c, f.getGeometry());
      if (coord) {
        coord = coord.coord;
        var p = this.getMap().getPixelFromCoordinate(coord);
        if (ol_coordinate_dist2d(e.pixel, p) < this.snapDistance_) {
          c = coord;
        }
        if (this.currentFeature !== f)
          this.dispatchEvent({ type: "select", selected: [f], deselected: [this.currentFeature] });
        this.currentFeature = f;
        return { source, feature: f, coord: c };
      }
    }
  }
  /** Get nearest coordinate in a list
  * @param {ol.coordinate} pt the point to find nearest
  * @param {ol.geom} coords list of coordinates
  * @return {*} the nearest point with a coord (projected point), dist (distance to the geom), ring (if Polygon)
  */
  getNearestCoord(pt, geom) {
    var i, l, p, p0, dm;
    switch (geom.getType()) {
      case "Point": {
        return { coord: geom.getCoordinates(), dist: ol_coordinate_dist2d(geom.getCoordinates(), pt) };
      }
      case "MultiPoint": {
        return this.getNearestCoord(pt, new LineString_default(geom.getCoordinates()));
      }
      case "LineString":
      case "LinearRing": {
        var d;
        dm = Number.MAX_VALUE;
        var coords = geom.getCoordinates();
        for (i = 0; i < coords.length; i++) {
          d = ol_coordinate_dist2d(pt, coords[i]);
          if (d < dm) {
            dm = d;
            p0 = coords[i];
          }
        }
        return { coord: p0, dist: dm };
      }
      case "MultiLineString": {
        var lstring = geom.getLineStrings();
        p0 = false, dm = Number.MAX_VALUE;
        for (i = 0; l = lstring[i]; i++) {
          p = this.getNearestCoord(pt, l);
          if (p && p.dist < dm) {
            p0 = p;
            dm = p.dist;
            p0.ring = i;
          }
        }
        return p0;
      }
      case "Polygon": {
        var lring = geom.getLinearRings();
        p0 = false;
        dm = Number.MAX_VALUE;
        for (i = 0; l = lring[i]; i++) {
          p = this.getNearestCoord(pt, l);
          if (p && p.dist < dm) {
            p0 = p;
            dm = p.dist;
            p0.ring = i;
          }
        }
        return p0;
      }
      case "MultiPolygon": {
        var poly = geom.getPolygons();
        p0 = false;
        dm = Number.MAX_VALUE;
        for (i = 0; l = poly[i]; i++) {
          p = this.getNearestCoord(pt, l);
          if (p && p.dist < dm) {
            p0 = p;
            dm = p.dist;
            p0.poly = i;
          }
        }
        return p0;
      }
      case "GeometryCollection": {
        var g = geom.getGeometries();
        p0 = false;
        dm = Number.MAX_VALUE;
        for (i = 0; l = g[i]; i++) {
          p = this.getNearestCoord(pt, l);
          if (p && p.dist < dm) {
            p0 = p;
            dm = p.dist;
            p0.geom = i;
          }
        }
        return p0;
      }
      default:
        return false;
    }
  }
  /** Get arcs concerned by a modification
   * @param {ol.geom} geom the geometry concerned
   * @param {ol.coordinate} coord pointed coordinates
   */
  getArcs(geom, coord) {
    var arcs = false;
    var coords, i, s, l, g;
    switch (geom.getType()) {
      case "Point": {
        if (ol_coordinate_equal(coord, geom.getCoordinates())) {
          arcs = {
            geom,
            type: geom.getType(),
            coord1: [],
            coord2: [],
            node: true
          };
        }
        break;
      }
      case "MultiPoint": {
        coords = geom.getCoordinates();
        for (i = 0; i < coords.length; i++) {
          if (ol_coordinate_equal(coord, coords[i])) {
            arcs = {
              geom,
              type: geom.getType(),
              index: i,
              coord1: [],
              coord2: [],
              node: true
            };
            break;
          }
        }
        break;
      }
      case "LinearRing":
      case "LineString": {
        var p = geom.getClosestPoint(coord);
        if (ol_coordinate_dist2d(p, coord) < 1.5 * this.tolerance_) {
          var split;
          if (geom.getType() === "LinearRing") {
            g = new LineString_default(geom.getCoordinates());
            split = g.splitAt(coord, this.tolerance_);
          } else {
            split = geom.splitAt(coord, this.tolerance_);
          }
          if (split.length > 2) {
            coords = split[1].getCoordinates();
            for (i = 2; s = split[i]; i++) {
              var c = s.getCoordinates();
              c.shift();
              coords = coords.concat(c);
            }
            split = [split[0], new LineString_default(coords)];
          }
          if (split.length === 2) {
            var c0 = split[0].getCoordinates();
            var c1 = split[1].getCoordinates();
            var nbpt = c0.length + c1.length - 1;
            c0.pop();
            c1.shift();
            arcs = {
              geom,
              type: geom.getType(),
              coord1: c0,
              coord2: c1,
              node: geom.getCoordinates().length === nbpt,
              closed: false
            };
          } else if (split.length === 1) {
            s = split[0].getCoordinates();
            var start = ol_coordinate_equal(s[0], coord);
            var end = ol_coordinate_equal(s[s.length - 1], coord);
            if (start) {
              s.shift();
              if (end)
                s.pop();
              arcs = {
                geom,
                type: geom.getType(),
                coord1: [],
                coord2: s,
                node: true,
                closed: end
              };
            } else if (end) {
              s.pop();
              arcs = {
                geom,
                type: geom.getType(),
                coord1: s,
                coord2: [],
                node: true,
                closed: false
              };
            }
          }
        }
        break;
      }
      case "MultiLineString": {
        var lstring = geom.getLineStrings();
        for (i = 0; l = lstring[i]; i++) {
          arcs = this.getArcs(l, coord);
          if (arcs) {
            arcs.geom = geom;
            arcs.type = geom.getType();
            arcs.lstring = i;
            break;
          }
        }
        break;
      }
      case "Polygon": {
        var lring = geom.getLinearRings();
        for (i = 0; l = lring[i]; i++) {
          arcs = this.getArcs(l, coord);
          if (arcs) {
            arcs.geom = geom;
            arcs.type = geom.getType();
            arcs.index = i;
            break;
          }
        }
        break;
      }
      case "MultiPolygon": {
        var poly = geom.getPolygons();
        for (i = 0; l = poly[i]; i++) {
          arcs = this.getArcs(l, coord);
          if (arcs) {
            arcs.geom = geom;
            arcs.type = geom.getType();
            arcs.poly = i;
            break;
          }
        }
        break;
      }
      case "GeometryCollection": {
        g = geom.getGeometries();
        for (i = 0; l = g[i]; i++) {
          arcs = this.getArcs(l, coord);
          if (arcs) {
            arcs.geom = geom;
            arcs.g = i;
            arcs.typeg = arcs.type;
            arcs.type = geom.getType();
            break;
          }
        }
        break;
      }
      default: {
        console.error("ol/interaction/ModifyFeature " + geom.getType() + " not supported!");
        break;
      }
    }
    return arcs;
  }
  /**
   * @param {ol.MapBrowserEvent} evt Map browser event.
   * @return {boolean} `true` to start the drag sequence.
   */
  handleDownEvent(evt) {
    if (!this.getActive())
      return false;
    var current = this.getClosestFeature(evt);
    if (current && (this._condition(evt) || this._deleteCondition(evt))) {
      var features = [];
      this.arcs = [];
      this.sources_.forEach((function(s) {
        var extent = buffer(boundingExtent([current.coord]), this.tolerance_);
        features = features.concat(features, s.getFeaturesInExtent(extent));
      }).bind(this));
      this._modifiedFeatures = [];
      features.forEach((function(f) {
        var a = this.getArcs(f.getGeometry(), current.coord);
        if (a) {
          if (this._insertVertexCondition(evt) || a.node) {
            a.feature = f;
            this._modifiedFeatures.push(f);
            this.arcs.push(a);
          }
        }
      }).bind(this));
      if (this._modifiedFeatures.length) {
        if (this._deleteCondition(evt)) {
          return !this._removePoint(current, evt);
        } else {
          this.dispatchEvent({
            type: "modifystart",
            coordinate: current.coord,
            originalEvent: evt.originalEvent,
            features: this._modifiedFeatures
          });
          this.handleDragEvent({
            coordinate: current.coord,
            originalEvent: evt.originalEvent
          });
          return true;
        }
      } else {
        return true;
      }
    } else {
      return false;
    }
  }
  /** Get modified features
   * @return {Array<ol.Feature>} list of modified features
   */
  getModifiedFeatures() {
    return this._modifiedFeatures || [];
  }
  /** Removes the vertex currently being pointed.
   */
  removePoint() {
    this._removePoint({}, {});
  }
  /**
   * @private
   */
  _getModification(a) {
    var coords = a.coord1.concat(a.coord2);
    switch (a.type) {
      case "LineString": {
        if (a.closed)
          coords.push(coords[0]);
        if (coords.length > 1) {
          if (a.geom.getCoordinates().length != coords.length) {
            a.coords = coords;
            return true;
          }
        }
        break;
      }
      case "MultiLineString": {
        if (a.closed)
          coords.push(coords[0]);
        if (coords.length > 1) {
          var c = a.geom.getCoordinates();
          if (c[a.lstring].length != coords.length) {
            c[a.lstring] = coords;
            a.coords = c;
            return true;
          }
        }
        break;
      }
      case "Polygon": {
        if (a.closed)
          coords.push(coords[0]);
        if (coords.length > 3) {
          c = a.geom.getCoordinates();
          if (c[a.index].length != coords.length) {
            c[a.index] = coords;
            a.coords = c;
            return true;
          }
        }
        break;
      }
      case "MultiPolygon": {
        if (a.closed)
          coords.push(coords[0]);
        if (coords.length > 3) {
          c = a.geom.getCoordinates();
          if (c[a.poly][a.index].length != coords.length) {
            c[a.poly][a.index] = coords;
            a.coords = c;
            return true;
          }
        }
        break;
      }
      case "GeometryCollection": {
        a.type = a.typeg;
        var geom = a.geom;
        var geoms = geom.getGeometries();
        a.geom = geoms[a.g];
        var found = this._getModification(a);
        geom.setGeometries(geoms);
        a.geom = geom;
        a.type = "GeometryCollection";
        return found;
      }
      default: {
        break;
      }
    }
    return false;
  }
  /** Removes the vertex currently being pointed.
   * @private
   */
  _removePoint(current, evt) {
    if (!this.arcs)
      return false;
    this.overlayLayer_.getSource().clear();
    var found = false;
    this.arcs.forEach((function(a) {
      found = found || this._getModification(a);
    }).bind(this));
    if (found) {
      this.dispatchEvent({
        type: "modifystart",
        coordinate: current.coord,
        originalEvent: evt.originalEvent,
        features: this._modifiedFeatures
      });
      this.arcs.forEach((function(a) {
        if (a.geom.getType() === "GeometryCollection") {
          if (a.coords) {
            var geoms = a.geom.getGeometries();
            geoms[a.g].setCoordinates(a.coords);
            a.geom.setGeometries(geoms);
          }
        } else {
          if (a.coords)
            a.geom.setCoordinates(a.coords);
        }
      }).bind(this));
      this.dispatchEvent({
        type: "modifyend",
        coordinate: current.coord,
        originalEvent: evt.originalEvent,
        features: this._modifiedFeatures
      });
    }
    this.arcs = [];
    return found;
  }
  /**
   * @private
   */
  handleUpEvent(e) {
    if (!this.getActive())
      return false;
    if (!this.arcs || !this.arcs.length)
      return true;
    this.overlayLayer_.getSource().clear();
    this.dispatchEvent({
      type: "modifyend",
      coordinate: e.coordinate,
      originalEvent: e.originalEvent,
      features: this._modifiedFeatures
    });
    this.arcs = [];
    return true;
  }
  /**
   * @private
   */
  setArcCoordinates(a, coords) {
    var c;
    switch (a.type) {
      case "Point": {
        a.geom.setCoordinates(coords[0]);
        break;
      }
      case "MultiPoint": {
        c = a.geom.getCoordinates();
        c[a.index] = coords[0];
        a.geom.setCoordinates(c);
        break;
      }
      case "LineString": {
        a.geom.setCoordinates(coords);
        break;
      }
      case "MultiLineString": {
        c = a.geom.getCoordinates();
        c[a.lstring] = coords;
        a.geom.setCoordinates(c);
        break;
      }
      case "Polygon": {
        c = a.geom.getCoordinates();
        c[a.index] = coords;
        a.geom.setCoordinates(c);
        break;
      }
      case "MultiPolygon": {
        c = a.geom.getCoordinates();
        c[a.poly][a.index] = coords;
        a.geom.setCoordinates(c);
        break;
      }
      case "GeometryCollection": {
        a.type = a.typeg;
        var geom = a.geom;
        var geoms = geom.getGeometries();
        a.geom = geoms[a.g];
        this.setArcCoordinates(a, coords);
        geom.setGeometries(geoms);
        a.geom = geom;
        a.type = "GeometryCollection";
        break;
      }
    }
  }
  /**
   * @private
   */
  handleDragEvent(e) {
    if (!this.getActive()) return false;
    if (!this.arcs) return true;
    this.overlayLayer_.getSource().clear();
    var p = new Feature_default(new Point_default(e.coordinate));
    this.overlayLayer_.getSource().addFeature(p);
    if (!this.arcs.length) return true;
    this.arcs.forEach((function(a) {
      var coords = a.coord1.concat([e.coordinate], a.coord2);
      if (a.closed) coords.push(e.coordinate);
      this.setArcCoordinates(a, coords);
    }).bind(this));
    this.dispatchEvent({
      type: "modifying",
      coordinate: e.coordinate,
      originalEvent: e.originalEvent,
      features: this._modifiedFeatures
    });
    return true;
  }
  /**
   * @param {ol.MapBrowserEvent} evt Event.
   * @private
   */
  handleMoveEvent(e) {
    if (!this.getActive()) return true;
    this.overlayLayer_.getSource().clear();
    var current = this.getClosestFeature(e);
    if (current) {
      var p = new Feature_default(new Point_default(current.coord));
      this.overlayLayer_.getSource().addFeature(p);
    }
    var element = e.map.getTargetElement();
    if (this.cursor_) {
      if (current) {
        if (element.style.cursor != this.cursor_) {
          this.previousCursor_ = element.style.cursor;
          element_default.setCursor(element, this.cursor_);
        }
      } else if (this.previousCursor_ !== void 0) {
        element_default.setCursor(element, this.previousCursor_);
        this.previousCursor_ = void 0;
      }
    }
    return true;
  }
  /** Get the current feature to modify
   * @return {ol.Feature}
   */
  getCurrentFeature() {
    return this.currentFeature;
  }
};
var ModifyFeature_default = ol_interaction_ModifyFeature;

export {
  ModifyFeature_default
};
//# sourceMappingURL=chunk-YNX27GDF.js.map
