"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _geom = require("ol/geom");
var _layer = require("ol/layer");
var _style = require("ol/style");
var _interaction = require("ol/interaction");
var _sphere = require("ol/sphere");
var _source = require("ol/source");
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var labelStyle = new _style.Style({
  text: new _style.Text({
    font: '14px Calibri,sans-serif',
    fill: new _style.Fill({
      color: 'rgba(255, 255, 255, 1)'
    }),
    backgroundFill: new _style.Fill({
      color: 'rgba(0, 0, 0, 0.7)'
    }),
    padding: [3, 3, 3, 3],
    textBaseline: 'bottom',
    offsetY: -15
  }),
  image: new _style.RegularShape({
    radius: 8,
    points: 3,
    angle: Math.PI,
    displacement: [0, 10],
    fill: new _style.Fill({
      color: 'rgba(0, 0, 0, 0.7)'
    })
  })
});
var segmentStyle = new _style.Style({
  text: new _style.Text({
    font: '12px Calibri,sans-serif',
    fill: new _style.Fill({
      color: 'rgba(255, 255, 255, 1)'
    }),
    backgroundFill: new _style.Fill({
      color: 'rgba(0, 0, 0, 0.4)'
    }),
    padding: [2, 2, 2, 2],
    textBaseline: 'bottom',
    offsetY: -12
  }),
  image: new _style.RegularShape({
    radius: 6,
    points: 3,
    angle: Math.PI,
    displacement: [0, 8],
    fill: new _style.Fill({
      color: 'rgba(0, 0, 0, 0.4)'
    })
  })
});
var segmentStyles = [segmentStyle];
var formatLength = function formatLength(line) {
  var length = (0, _sphere.getLength)(line, {
    projection: 'EPSG:4326'
  });
  var output;
  if (length > 100) {
    output = "".concat(Math.round(length / 1000 * 100) / 100, " km");
  } else {
    output = "".concat(Math.round(length * 100) / 100, " m");
  }
  return output;
};
var formatArea = function formatArea(polygon) {
  var area = (0, _sphere.getArea)(polygon, {
    projection: 'EPSG:4326'
  });
  var output;
  if (area > 10000) {
    output = "".concat(Math.round(area / 1000000 * 100) / 100, " km\xB2");
  } else {
    output = "".concat(Math.round(area * 100) / 100, " m\xB2");
  }
  return output;
};
var styleFunction = function styleFunction(_ref) {
  var feature = _ref.feature,
    segments = _ref.segments,
    drawType = _ref.drawType;
  var styles = [new _style.Style({
    fill: new _style.Fill({
      color: 'rgba(255, 255, 255, 0.2)'
    }),
    stroke: new _style.Stroke({
      color: 'rgba(0, 0, 0, 0.5)',
      lineDash: [10, 10],
      width: 2
    }),
    image: new _style.Circle({
      radius: 5,
      stroke: new _style.Stroke({
        color: 'rgba(0, 0, 0, 0.7)'
      }),
      fill: new _style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
      })
    })
  })];
  var geometry = feature.getGeometry();
  var type = geometry.getType();
  var point, label, line;
  if (!drawType || drawType === type) {
    if (type === 'Polygon') {
      point = geometry.getInteriorPoint();
      label = formatArea(geometry);
      line = new _geom.LineString(geometry.getCoordinates()[0]);
    } else if (type === 'LineString') {
      point = new _geom.Point(geometry.getLastCoordinate());
      label = formatLength(geometry);
      line = geometry;
    }
  }
  if (segments && line) {
    var count = 0;
    line.forEachSegment(function (a, b) {
      var segment = new _geom.LineString([a, b]);
      var label = formatLength(segment);
      if (segmentStyles.length - 1 < count) {
        segmentStyles.push(segmentStyle.clone());
      }
      var segmentPoint = new _geom.Point(segment.getCoordinateAt(0.5));
      segmentStyles[count].setGeometry(segmentPoint);
      segmentStyles[count].getText().setText(label);
      styles.push(segmentStyles[count]);
      count++;
    });
  }
  if (label) {
    labelStyle.setGeometry(point);
    labelStyle.getText().setText(label);
    styles.push(labelStyle);
  }
  return styles;
};
var _default = /*#__PURE__*/function () {
  function _default(options) {
    var _this = this;
    _classCallCheck(this, _default);
    this.map = options.map;
    this.type = options.type || 'MeasureDistance';
    this.clearPrevious = options.clearPrevious;
    var segments = options.segments;
    this.source = new _source.Vector();
    this.layer = new _layer.Vector({
      source: this.source,
      style: function style(feature) {
        return styleFunction({
          feature: feature,
          segments: segments
        });
      },
      zIndex: 99
    });
    this.layer.key = this.type;

    // 测距
    var drawType = this.type === 'MeasureDistance' ? 'LineString' : 'Polygon';
    this.tool = new _interaction.Draw({
      source: this.source,
      type: drawType,
      style: function style(feature) {
        return styleFunction({
          feature: feature,
          segments: segments,
          drawType: drawType
        });
      }
    });
    this.map.addInteraction(this.tool);
    this.setActive();
    this.map.addLayer(this.layer);
    this.map.showTip('鼠标单击开始绘制');
    this.tool.on('drawstart', function () {
      if (_this.clearPrevious) {
        _this.source.clear();
      }
      _this.map.showTip('鼠标单击继续绘制');
    });
    this.tool.on('drawend', function () {
      _this.map.showTip('鼠标单击开始绘制');
    });
  }
  _createClass(_default, [{
    key: "setActive",
    value: function setActive() {
      this.tool.setActive(true);
    }
  }, {
    key: "segments",
    set: function set(val) {
      this.layer && this.layer.setStyle(function (feature) {
        return styleFunction({
          feature: feature,
          segments: val
        });
      });
      this.tool && this.tool.getOverlay().setStyle(function (feature) {
        return styleFunction({
          feature: feature,
          segments: val
        });
      });
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.map.hideTip();
      this.map.removeInteraction(this.tool);
      this.map.removeLayer(this.layer);
    }
  }]);
  return _default;
}();
exports["default"] = _default;
//# sourceMappingURL=Measure.js.map
