"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _Vector = _interopRequireDefault(require("ol/layer/Vector"));
var _Vector2 = _interopRequireDefault(require("ol/source/Vector"));
var _render = require("ol/render");
var _Style = _interopRequireDefault(require("ol/style/Style"));
var _Stroke = _interopRequireDefault(require("ol/style/Stroke"));
var _Fill = _interopRequireDefault(require("ol/style/Fill"));
var _Circle = _interopRequireDefault(require("ol/style/Circle"));
var _Feature = _interopRequireDefault(require("ol/Feature"));
var _GeoJSON = _interopRequireDefault(require("ol/format/GeoJSON"));
var _Point = _interopRequireDefault(require("ol/geom/Point"));
var _turf = require("@turf/turf");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _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 _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); }
var turfFormat = new _GeoJSON["default"]();
var FlyLine = exports["default"] = /*#__PURE__*/function () {
  function FlyLine(options) {
    _classCallCheck(this, FlyLine);
    var data = options.data,
      key = options.key,
      speed = options.speed,
      name = options.name,
      layerControl = options.layerControl,
      _options$show = options.show,
      show = _options$show === void 0 ? true : _options$show,
      legend = options.legend,
      map = options.map,
      lineStyle = options.lineStyle,
      dotStyle = options.dotStyle;

    // 图层标识
    this.key = key;

    // 图层名称
    this.name = name;

    // 数据
    this.data = data;

    // 速度
    this.speed = speed || 5;
    this.legend = legend;

    // 地图
    this.map = map;
    this.layerControl = layerControl;
    this._distance = 0;
    this._lastTime = Date.now();

    // 线样式
    this.lineStyle = new _Style["default"]({
      stroke: new _Stroke["default"](_objectSpread({
        color: '#2491fc',
        width: 3
      }, lineStyle))
    });
    this.dotStyle = new _Style["default"]({
      image: new _Circle["default"](_objectSpread({
        fill: new _Fill["default"]({
          color: (dotStyle === null || dotStyle === void 0 ? void 0 : dotStyle.color) || '#FFF'
        }),
        radius: 3
      }, dotStyle))
    });
    this.linesFeature = this.getArcLinesFeature(data);
    var vectorLayer = new _Vector["default"]({
      source: new _Vector2["default"]({
        features: this.linesFeature
      }),
      style: new _Style["default"]({
        stroke: new _Stroke["default"]({
          color: 'rgba(0, 0, 0, 0)'
        })
      })
    });
    this.vectorLayer = vectorLayer;

    // 图层标识
    this.vectorLayer.key = key;

    // 图层名称
    this.vectorLayer.name = name;

    // 图例
    this.vectorLayer.legend = legend;

    // 图层控制
    this.vectorLayer.layerControl = layerControl;

    // 图层展示
    this.vectorLayer.show = show;
    this.vectorLayer.setVisible(show);
    this.map.addLayer(vectorLayer);
    this.show = show;
  }
  // 重写父类的属性访问器
  return _createClass(FlyLine, [{
    key: "show",
    get: function get() {
      return this.vectorLayer.show;
    },
    set: function set(value) {
      this.vectorLayer.setVisible(value);
      this.vectorLayer.show = value;
      if (value) {
        this.onRender();
      } else {
        this.unRender();
      }
    }
  }, {
    key: "setVisible",
    value: function setVisible(value) {
      this.show = value;
    }
  }, {
    key: "setShow",
    value: function setShow(value) {
      this.show = value;
    }
  }, {
    key: "loadData",
    value: function loadData(data) {
      this.linesFeature = this.getArcLinesFeature(data);
    }
  }, {
    key: "getArcLinesFeature",
    value: function getArcLinesFeature(data) {
      var _this = this;
      var linesFeature = [];
      if (data !== null && data !== void 0 && data.length) {
        data.forEach(function (item) {
          var po = item.geometryInfo.lngLats.split(';');
          var tempF = _this.getTurfArcFeature(po[0], po[1], item);
          linesFeature.push(tempF);
        });
      }
      return linesFeature;
    }
  }, {
    key: "getTurfArcFeature",
    value: function getTurfArcFeature(_start, _end, opt) {
      var start = _start.split(',').map(function (v) {
        return Number(v);
      });
      var end = _end.split(',').map(function (v) {
        return Number(v);
      });
      var line = (0, _turf.lineString)([start, [start[0] + (end[0] - start[0]) * 0.5, start[1] + (end[1] - start[1]) * 0.65], end]);
      var curved = (0, _turf.bezierSpline)(line);
      var length = (0, _turf.length)(curved, {
        units: 'meters'
      });
      var bF = turfFormat.readFeature(curved);
      bF.setProperties(opt);
      bF.set('length', length);
      return bF;
    }
  }, {
    key: "render",
    value: function render(evt) {
      var _this2 = this;
      var speed = Number(this.speed);
      var linesFeature = this.linesFeature;
      var veContext = (0, _render.getVectorContext)(evt);
      linesFeature.forEach(function (item) {
        veContext.drawFeature(item, _this2.lineStyle);
        var time = (evt.frameState.time - item.get('start')) / 1000;
        var frac = time / speed;
        if (!item.get('start')) item.set('start', new Date().getTime());
        if (frac >= 1) {
          item.set('start', new Date().getTime());
          frac = 0;
        }
        var along = item.getGeometry().getCoordinateAt(frac);
        var pF = new _Feature["default"](new _Point["default"](along));
        veContext.drawFeature(pF, _this2.dotStyle);
      });
      this.map.render();
    }
  }, {
    key: "onRender",
    value: function onRender() {
      if (this.vectorLayer) {
        this._on_event = this.vectorLayer.on('postrender', this.render.bind(this));
      }
    }
  }, {
    key: "unRender",
    value: function unRender() {
      var _this$_on_event;
      if (this.vectorLayer && this !== null && this !== void 0 && (_this$_on_event = this._on_event) !== null && _this$_on_event !== void 0 && _this$_on_event.listener) {
        this.vectorLayer.un('postrender', this._on_event.listener);
      }
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.linesFeature = [];
      this.unRender();
    }
  }]);
}();
//# sourceMappingURL=FlyLineLayer.js.map
