"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(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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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 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);
  }
  _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();
    }
  }]);
  return _default;
}();
//# sourceMappingURL=Draw.js.map
