"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.drawStyle = exports["default"] = void 0;
var _layer = require("ol/layer");
var _source = require("ol/source");
var _interaction = require("ol/interaction");
var _Draw = require("ol/interaction/Draw");
var _style = require("ol/style");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var drawStyle = exports.drawStyle = function drawStyle() {
  var style = [new _style.Style({
    fill: new _style.Fill({
      color: 'rgba(255, 255, 255, 0.2)'
    }),
    stroke: new _style.Stroke({
      color: '#009BFF',
      width: 2
    }),
    image: new _style.Circle({
      radius: 4,
      fill: new _style.Fill({
        color: 'rgba(0,155,255, 1)'
      })
    })
  })];
  return style;
};
var _default = exports["default"] = /*#__PURE__*/function () {
  function _default(props) {
    var _this = this;
    _classCallCheck(this, _default);
    _defineProperty(this, "draw", function (drawType, callback, tip) {
      if (drawType) {
        _this.init(drawType, callback, tip);
      }
    });
    this.map = props.map;
    this.drawTool = null;
    this.drawFeature = null;
    this.drawType = props.drawType;
    this.source = new _source.Vector();
    this.layer = new _layer.Vector({
      source: this.source,
      zIndex: props.zIndex || 20,
      style: function style(feature) {
        return drawStyle(feature);
      }
    });
    this.map.addLayer(this.layer);
  }
  return _createClass(_default, [{
    key: "drawType",
    set: function set(val) {
      this.init(val);
    }
  }, {
    key: "init",
    value:
    /**
     * @description:
     * @param {*} drawType DragBox-框选 Circle-圈选 Polygon-多边形选择
     * @return {*}
     */
    function init(drawType, callback) {
      var _this2 = this;
      var tip = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '鼠标左键确定起始点';
      this.clearFeatures();
      this.removeDrawTool();
      if (drawType) {
        var drawTool = new _interaction.Draw(_objectSpread({
          type: drawType,
          style: function style(feature) {
            return drawStyle(feature);
          }
        }, drawType === 'DragBox' ? {
          type: 'Circle',
          geometryFunction: (0, _Draw.createBox)()
        } : {}));
        drawTool.on('drawstart', function () {
          _this2.map.showTip('再次点击确定结束点');
          _this2.clearFeatures();
        });
        drawTool.on('drawend', function (event) {
          _this2.map.showTip(tip);
          var drawSource = _this2.layer.getSource();
          _this2.drawFeature = event.feature;
          drawSource.addFeature(event.feature);
          callback && callback(event.feature);
        });
        this.drawTool = drawTool;
        this.drawTool.setActive(true);
        this.map.showTip(tip);
        this.map.addInteraction(drawTool);
      }
    }
  }, {
    key: "clearFeatures",
    value: function clearFeatures() {
      this.source && this.source.clear();
    }
  }, {
    key: "removeDrawTool",
    value: function removeDrawTool() {
      this.map.removeInteraction(this.drawTool);
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.map.removeLayer(this.layer);
      this.removeDrawTool();
      this.drawTool && this.drawTool.setActive(false);
      this.map.hideTip();
    }
  }]);
}();
//# sourceMappingURL=Draw.js.map
