"use strict";

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); }
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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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 _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); }
function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var turfFormat = new _GeoJSON["default"]();
var FlyLine = /*#__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,
      map = options.map,
      lineStyle = options.lineStyle,
      dotStyle = options.dotStyle;

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

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

    // 数据
    this.data = data;

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

    // 地图
    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.setVisible(show);
    this.map.addLayer(vectorLayer);
    this.show = show;
  }
  // 重写父类的属性访问器
  _createClass(FlyLine, [{
    key: "show",
    get: function get() {
      return _get(_getPrototypeOf(FlyLine.prototype), "show", this);
    },
    set: function set(value) {
      this.vectorLayer.setVisible(value);
      if (value) {
        this.onRender();
      } else {
        this.unRender();
      }
    }
  }, {
    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() {
      this._on_event = this.vectorLayer.on('postrender', this.render.bind(this));
    }
  }, {
    key: "unRender",
    value: function unRender() {
      this.vectorLayer.un('postrender', this._on_event.listener);
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.vectorLayer.un('postrender', this._on_event.listener);
      this.linesFeature = [];
    }
  }]);
  return FlyLine;
}();
exports["default"] = FlyLine;
//# sourceMappingURL=FlyLineLayer.js.map
