"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["default"] = void 0;
var _lineFlowMaterialProperty = _interopRequireDefault(require("./lineFlowMaterialProperty"));
var _createFeatureCollection = require("../feature-layer/createFeatureCollection");
var _lodash = require("lodash");
var _transformRgb = _interopRequireDefault(require("../_util/transformRgb"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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 _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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function set(target, property, value, receiver) { if (typeof Reflect !== "undefined" && Reflect.set) { set = Reflect.set; } else { set = function set(target, property, value, receiver) { var base = _superPropBase(target, property); var desc; if (base) { desc = Object.getOwnPropertyDescriptor(base, property); if (desc.set) { desc.set.call(receiver, value); return true; } else if (!desc.writable) { return false; } } desc = Object.getOwnPropertyDescriptor(receiver, property); if (desc) { if (!desc.writable) { return false; } desc.value = value; Object.defineProperty(receiver, property, desc); } else { _defineProperty(receiver, property, value); } return true; }; } return set(target, property, value, receiver); }
function _set(target, property, value, receiver, isStrict) { var s = set(target, property, value, receiver || target); if (!s && isStrict) { throw new TypeError('failed to set property'); } return value; }
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); }
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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var _Cesium = Cesium,
  CustomDataSource = _Cesium.CustomDataSource,
  Cartesian3 = _Cesium.Cartesian3,
  Color = _Cesium.Color;
;
/**
 * @description: 抛物线构造函数（方程 y=-(4h/L^2)*x^2+h h:顶点高度 L：横纵间距较大者）
 * @param {*} startPosition: 起点
 * @param {*} endPosition: 终点
 * @param {*} height: 高度
 * @param {*} count: 数量
 * @return {*}
 */
function parabola(startPosition, endPosition) {
  var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var count = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 50;
  if (!height) {
    return [_construct(Cartesian3.fromDegrees, _toConsumableArray(startPosition).concat([0])), _construct(Cartesian3.fromDegrees, _toConsumableArray(endPosition).concat([0]))];
  }
  var result = [];
  height = Math.max(+height, 100);
  count = Math.max(+count, 50);
  var diffLon = Math.abs(startPosition[0] - endPosition[0]);
  var diffLat = Math.abs(startPosition[1] - endPosition[1]);
  var L = Math.max(diffLon, diffLat);
  var dlt = L / count;
  if (diffLon > diffLat) {
    //base on lon
    var delLat = (endPosition[1] - startPosition[1]) / count;
    if (startPosition[0] - endPosition[0] > 0) {
      dlt = -dlt;
    }
    for (var i = 0; i < count; i++) {
      var h = height - Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * height / Math.pow(L, 2);
      var lon = startPosition[0] + dlt * i;
      var lat = startPosition[1] + delLat * i;
      var point = new Cartesian3.fromDegrees(lon, lat, h);
      result.push(point);
    }
  } else {
    //base on lat
    var delLon = (endPosition[0] - startPosition[0]) / count;
    if (startPosition[1] - endPosition[1] > 0) {
      dlt = -dlt;
    }
    for (var _i = 0; _i < count; _i++) {
      var _h = height - Math.pow(-0.5 * L + Math.abs(dlt) * _i, 2) * 4 * height / Math.pow(L, 2);
      var _lon = startPosition[0] + delLon * _i;
      var _lat = startPosition[1] + dlt * _i;
      var _point = new Cartesian3.fromDegrees(_lon, _lat, _h);
      result.push(_point);
    }
  }
  return result;
}
var getCoordinates = function getCoordinates(geometryInfo) {
  if (geometryInfo) {
    if ((0, _lodash.isString)(geometryInfo)) {
      var _geometryInfo$split = geometryInfo.split(','),
        _geometryInfo$split2 = _slicedToArray(_geometryInfo$split, 2),
        start = _geometryInfo$split2[0],
        end = _geometryInfo$split2[1];
      return [Number(start), Number(end)];
    } else if ((0, _lodash.isArray)(geometryInfo)) {
      var _geometryInfo = _slicedToArray(geometryInfo, 2),
        _start = _geometryInfo[0],
        _end = _geometryInfo[1];
      return [Number(_start), Number(_end)];
    } else if ((0, _lodash.isObject)(geometryInfo)) {
      if (geometryInfo.geometryInfo) {
        return getCoordinates(geometryInfo.geometryInfo);
      }
      var _transformGeometry$co = _slicedToArray((0, _createFeatureCollection.transformGeometry)(geometryInfo).coordinates, 2),
        _start2 = _transformGeometry$co[0],
        _end2 = _transformGeometry$co[1];
      return [Number(_start2), Number(_end2)];
    }
  }
};
var FlyLineLayer = /*#__PURE__*/function (_CustomDataSource) {
  _inherits(FlyLineLayer, _CustomDataSource);
  var _super = _createSuper(FlyLineLayer);
  function FlyLineLayer(option) {
    var _thisSuper, _this;
    _classCallCheck(this, FlyLineLayer);
    var data = option.data,
      key = option.key,
      labelField = option.labelField,
      template = option.template,
      _option$height = option.height,
      height = _option$height === void 0 ? 1000 : _option$height,
      count = option.count,
      color = option.color,
      speed = option.speed,
      width = option.width,
      flyWidth = option.flyWidth,
      name = option.name,
      layerControl = option.layerControl,
      _option$show = option.show,
      show = _option$show === void 0 ? true : _option$show,
      map = option.map;
    _this = _super.call(this, option);
    _set((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(FlyLineLayer.prototype)), "show", show, _thisSuper, true);
    // 图层标识
    _this.key = key;

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

    // 标签字段
    _this.labelField = labelField;

    // 模版内容
    _this.template = template;
    _this.data = data;

    // 颜色
    _this.color = color || '#0080ff';

    // 高度
    _this.height = height;

    // 数量
    _this.count = count || 1;

    // 速度
    _this.speed = speed || 10;

    // 轨迹线宽度
    _this.width = width || 1;
    // 地图
    _this.map = map;
    // 飞线宽度
    _this.flyWidth = flyWidth || 3;
    _this.layerControl = layerControl;
    if (data !== null && data !== void 0 && data.length) {
      _this.loadData(data);
    }
    return _this;
  }
  // 重写父类的属性访问器
  _createClass(FlyLineLayer, [{
    key: "show",
    get: function get() {
      return _get(_getPrototypeOf(FlyLineLayer.prototype), "show", this);
    },
    set: function set(value) {
      this.setVisible(value);
    }

    // 获取地图
  }, {
    key: "getMap",
    value: function getMap() {
      return this.map;
    }

    // 设置图层显示隐藏
  }, {
    key: "setVisible",
    value: function setVisible(visible) {
      _set(_getPrototypeOf(FlyLineLayer.prototype), "show", visible, this, true); // 将属性传递给父类

      var map = this.getMap && this.getMap();
      if (map) {
        map.layers.emit('visible', this, visible);
      }
    }
  }, {
    key: "loadData",
    value: function loadData(data) {
      var _this2 = this;
      this.entities.removeAll();
      data.map(function (v) {
        var positions = [];
        var height = _this2.height;
        var count = _this2.count;
        var color = _this2.color;
        if ((0, _lodash.isArray)(v)) {
          positions = parabola(v[0], v[1], height, count);
        } else {
          var start = v.start,
            end = v.end;
          height = v.height || height;
          count = v.count || count;
          color = v.color || color;
          positions = parabola(getCoordinates(start), getCoordinates(end), height, count);
        }

        // 创建飞线
        var entity_fly = _this2.entities.add({
          polyline: {
            positions: positions,
            width: _this2.flyWidth,
            material: new _lineFlowMaterialProperty["default"]({
              color: Color.fromCssColorString(color),
              speed: _this2.speed,
              percent: 0.1,
              gradient: 0.01
            })
          }
        });
        entity_fly.type = _this2.modalType || _this2.key;
        entity_fly.key = _this2.key;
        entity_fly.properties = v.properties || {};

        // 创建轨迹线
        var entity = _this2.entities.add({
          polyline: {
            positions: positions,
            width: _this2.width,
            material: Color.fromCssColorString((0, _transformRgb["default"])(color, 0.3))
          }
        });
        entity.type = _this2.modalType || _this2.key;
        entity.key = _this2.key;
        entity.properties = v.properties || {};
      });
    }
  }]);
  return FlyLineLayer;
}(CustomDataSource);
exports["default"] = FlyLineLayer;
//# sourceMappingURL=FlyLineLayer.js.map
