"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
var _transformRgb = _interopRequireDefault(require("../_util/transformRgb"));
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 _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 _cache = {};

// 获取立方体
var getBoxGeometry = function getBoxGeometry() {
  if (_cache.boxGeometry) return _cache.boxGeometry;
  _cache.boxGeometry = _cesium.BoxGeometry.fromDimensions({
    vertexFormat: _cesium.VertexFormat.POSITION_AND_NORMAL,
    dimensions: new _cesium.Cartesian3(2.0, 2.0, 5.0) // 长宽高
  });
  return _cache.boxGeometry;
};

// 获取圆柱
var getCylinderGeometry = function getCylinderGeometry() {
  if (_cache.cylinderGeometry) return _cache.cylinderGeometry;
  var cylinder = new _cesium.CylinderGeometry({
    length: 5,
    // 长度
    topRadius: 2,
    // 顶部半径
    bottomRadius: 2 // 底部半径
  });
  _cache.cylinderGeometry = _cesium.CylinderGeometry.createGeometry(cylinder);
  return _cache.cylinderGeometry;
};

// 根据类型获取几何体
var getGeometryByType = function getGeometryByType(type) {
  return {
    box: getBoxGeometry,
    cylinder: getCylinderGeometry
  }[type || 'cylinder']();
};
var ThreePointLayer = /*#__PURE__*/function () {
  function ThreePointLayer(options) {
    var _this$data;
    _classCallCheck(this, ThreePointLayer);
    var _options$show = options.show,
      show = _options$show === void 0 ? true : _options$show;
    this.map = options.map;

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

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

    // 标签字段
    this.labelField = options.labelField;

    // 模版内容
    this.template = options.template;

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

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

    // 取消气泡
    this.cancelPopup = options.cancelPopup;

    // 气泡模板
    this.tipTemplate = options.tipTemplate;

    // 仅展示
    this.viewOnly = options.viewOnly;
    this.data = options.data;
    this.color = options.color;
    this.primitives = [];
    this.show = show;
    if ((_this$data = this.data) !== null && _this$data !== void 0 && _this$data.length) {
      this.loadData(this.data);
    }
  }
  return _createClass(ThreePointLayer, [{
    key: "show",
    set: function set(value) {
      var _this$primitives;
      if ((_this$primitives = this.primitives) !== null && _this$primitives !== void 0 && _this$primitives.length) {
        this.primitives.map(function (primitive) {
          primitive.show = value;
        });
      }
    }
  }, {
    key: "loadData",
    value: function loadData(data) {
      var _this = this;
      this.data = data;
      var map = this.map;
      var show = this.show;
      this.destroy();
      this.primitives = [];
      var colors = {};
      data.map(function (item) {
        var data = item.data,
          type = item.type;
        var color_str = item.color || _this.color || '#0080ff';

        // 缓存颜色
        colors[color_str] = colors[color_str] || {
          item: item,
          _instances: [],
          data: [],
          color: color_str
        };
        var geometry = getGeometryByType(type || 'cylinder');

        // 缓存颜色对应的数据
        colors[color_str]._instances.push(new _cesium.GeometryInstance({
          geometry: geometry,
          modelMatrix: _cesium.Matrix4.multiplyByTranslation(_cesium.Transforms.eastNorthUpToFixedFrame(_cesium.Cartesian3.fromDegrees(data[0], data[1])), new _cesium.Cartesian3(0.0, 0.0, data[2] || 0.0), new _cesium.Matrix4()),
          id: item.id
        }));
        colors[color_str].data.push(item);
      });
      for (var color_str in colors) {
        var _colors$color_str = colors[color_str],
          color = _colors$color_str.color,
          _data = _colors$color_str.data,
          _instances = _colors$color_str._instances;
        var line = new _cesium.Primitive({
          geometryInstances: _instances,
          show: show,
          appearance: new _cesium.MaterialAppearance({
            material: new _cesium.Material({
              fabric: {
                type: 'Color',
                uniforms: {
                  color: _cesium.Color.fromCssColorString((0, _transformRgb["default"])(color, 1))
                }
              }
            }),
            faceForward: true
          }),
          asynchronous: false
        });
        this.setAttribute(line, _data);
        map.scene.primitives.add(line);
        this.primitives.push(line);
      }
    }
  }, {
    key: "setAttribute",
    value: function setAttribute(primitive, data) {
      // 图层标识
      primitive.key = this.key;

      // 数据
      primitive.data = data;

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

      // 标签字段
      primitive.labelField = this.labelField;

      // 模版内容
      primitive.template = this.template;

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

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

      // 取消气泡
      primitive.cancelPopup = this.cancelPopup;

      // 气泡模板
      primitive.tipTemplate = this.tipTemplate;

      // 仅展示
      primitive.viewOnly = this.viewOnly;
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var _this$primitives2;
      var map = this.map;
      if ((_this$primitives2 = this.primitives) !== null && _this$primitives2 !== void 0 && _this$primitives2.length) {
        this.primitives.map(function (primitive) {
          map.scene.primitives.remove(primitive);
        });
        this.primitives = null;
      }
    }
  }]);
}();
var _default = exports["default"] = ThreePointLayer;
//# sourceMappingURL=ThreePointLayer.js.map
