"use strict";

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); }
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 _Draw2 = require("ol/interaction/Draw");
var _style4 = require("ol/style");
var _Marker = _interopRequireDefault(require("../marker/Marker"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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 _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); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var ModifyConstrain = /*#__PURE__*/function (_Modify) {
  function ModifyConstrain(options) {
    var _this;
    _classCallCheck(this, ModifyConstrain);
    _this = _callSuper(this, ModifyConstrain, [options]);
    _this.constrainGeom_ = options.constrainGeom;
    return _this;
  }
  _inherits(ModifyConstrain, _Modify);
  return _createClass(ModifyConstrain, [{
    key: "handleDragEvent",
    value: function handleDragEvent(evt) {
      if (this.constrainGeom_ && this.constrainGeom_.intersectsCoordinate && !this.constrainGeom_.intersectsCoordinate(evt.coordinate)) {
        evt.coordinate = this.constrainGeom_.getClosestPoint(evt.coordinate);
      }
      _superPropGet(ModifyConstrain, "handleDragEvent", this, 3)([evt]);
    }
  }]);
}(_interaction.Modify);
var DrawConstrain = /*#__PURE__*/function (_Draw) {
  function DrawConstrain(options) {
    var _this2;
    _classCallCheck(this, DrawConstrain);
    _this2 = _callSuper(this, DrawConstrain, [options]);
    _this2.constrainGeom_ = options.constrainGeom;
    return _this2;
  }
  _inherits(DrawConstrain, _Draw);
  return _createClass(DrawConstrain, [{
    key: "handlePointerMove_",
    value: function handlePointerMove_(evt) {
      if (this.constrainGeom_ && this.constrainGeom_.intersectsCoordinate && !this.constrainGeom_.intersectsCoordinate(evt.coordinate)) {
        evt.coordinate = this.constrainGeom_.getClosestPoint(evt.coordinate);
      }
      _superPropGet(DrawConstrain, "handlePointerMove_", this, 3)([evt]);
    }
  }]);
}(_interaction.Draw);
var drawStyle = exports.drawStyle = function drawStyle(feature, style) {
  var _style = [];
  var styles = [new _style4.Style({
    fill: new _style4.Fill({
      color: 'rgba(255, 255, 255, 0.6)'
    }),
    stroke: new _style4.Stroke({
      color: '#009BFF',
      width: 2
    }),
    image: new _style4.Circle({
      radius: 4,
      fill: new _style4.Fill({
        color: 'rgba(0,155,255, 0.8)'
      })
    })
  })];
  if (style) {
    var _style2;
    if (typeof style === 'function') {
      _style = style(feature);
    }
    if (Array.isArray(_style) && (_style2 = _style) !== null && _style2 !== void 0 && _style2.length) styles.push.apply(styles, _toConsumableArray(_style));
  }
  return styles;
};
var _default = exports["default"] = /*#__PURE__*/function () {
  function _default(props, callback) {
    var _this3 = this;
    _classCallCheck(this, _default);
    _defineProperty(this, "initModify", function () {
      var t = _this3;
      var modify = new ModifyConstrain({
        source: _this3.source,
        constrainGeom: _this3.constrainGeom
      });
      _this3.map.addInteraction(modify);
      modify.on('modifyend', function (event) {
        var features = event.features;
        var feature = features.item(features.getLength() - 1);
        _this3.drawFeature = feature;
        var _feature = feature.clone();
        t.callback && t.callback(_feature);
      });
    });
    var _props$continuousDraw = props.continuousDraw,
      continuousDraw = _props$continuousDraw === void 0 ? true : _props$continuousDraw,
      _style3 = props.style,
      markerStyle = props.markerStyle,
      constrainGeom = props.constrainGeom;
    this.map = props.map;
    this.TextEnum = props.TextEnum;
    this.drawTool = null;
    this.drawFeature = null;
    this.maxPoints = props.maxPoints;
    this.drawType = props.drawType;
    this.callback = callback;
    this.continuousDraw = continuousDraw; // 连续绘制
    this.constrainGeom = constrainGeom; // 绘制限制范围：geometry
    this.style = _style3;
    this.source = new _source.Vector();
    this.layer = new _layer.Vector({
      source: this.source,
      zIndex: props.zIndex || 20,
      style: function style(feature) {
        return drawStyle(feature, _style3);
      }
    });
    this.map.addLayer(this.layer);
    if (!props.onlyDraw) {
      this.initModify();
    }
  }

  /**
   * @description:
   * @param {*} drawType DragBox-框选 Circle-圈选 Polygon-多边形选择
   * @return {*}
   */
  return _createClass(_default, [{
    key: "init",
    value: function init(drawType) {
      var _this4 = this;
      var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.callback;
      var markerStyle = arguments.length > 2 ? arguments[2] : undefined;
      var t = this;
      if (drawType) {
        var drawTool = new DrawConstrain(_objectSpread(_objectSpread({
          type: drawType,
          maxPoints: this.maxPoints,
          constrainGeom: this.constrainGeom,
          style: function style(feature) {
            return drawStyle(feature, t.style);
          }
        }, drawType === 'DragBox' ? {
          type: 'Circle',
          geometryFunction: (0, _Draw2.createBox)()
        } : {}), drawType === 'Marker' ? {
          type: 'Point'
        } : {}));
        drawTool.on('drawstart', function () {
          _this4.drawTool.setActive(true);
          _this4.map.showTip(_this4.TextEnum.clickAgainToEnd);
          _this4.clearFeatures();
          _this4.callbackFn && _this4.callbackFn();
        });
        drawTool.on('drawend', function (event) {
          _this4.map.showTip(_this4.TextEnum.leftClickToStart);
          var drawSource = _this4.layer.getSource();
          _this4.drawFeature = event.feature;
          var _feature = event.feature.clone();
          if (drawType == 'Marker') {
            event.feature.setStyle(new _Marker["default"](markerStyle));
          }
          drawSource.addFeature(event.feature);
          callback && callback(_feature);
          _this4.drawTool.setActive(t.continuousDraw);
        });
        this.drawTool = drawTool;
        this.map.showTip(this.TextEnum.leftClickToStart);
        this.map.addInteraction(drawTool);
      }
    }
  }, {
    key: "clearFeatures",
    value: function clearFeatures() {
      this.source && this.source.clear();
      this.map.hideTip();
    }
  }, {
    key: "loadFeatures",
    value: function loadFeatures(feature) {
      if (this.style) {
        feature.setStyle(drawStyle(feature, this.style));
      }
      // feature.setStyle(drawStyle(feature, this.style));
      this.source && this.source.addFeatures([feature]);
      this.drawFeature = feature;
    }
  }, {
    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=FeatureDraw.js.map
