"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 _cesium = require("cesium");
var _createFeatureCollection = require("../feature-layer/createFeatureCollection");
var _heatmap = _interopRequireDefault(require("heatmap.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": 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 _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 _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); }
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 _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 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 _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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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 _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 _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 _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(r) { if (Array.isArray(r)) return r; }
var getData = function getData(data, width, height) {
  var list = data.map(function (v) {
    var geometryInfo = v.geometryInfo;
    var _transformGeometry$co = _slicedToArray((0, _createFeatureCollection.transformGeometry)(geometryInfo).coordinates, 2),
      x = _transformGeometry$co[0],
      y = _transformGeometry$co[1];
    return _objectSpread(_objectSpread({}, v), {}, {
      x: x,
      y: y
    });
  });

  // 获取边界数据
  var value = 0.1;
  var latMin = Math.min.apply(Math, _toConsumableArray(list.map(function (point) {
    return point.y;
  }))) - value;
  var latMax = Math.max.apply(Math, _toConsumableArray(list.map(function (point) {
    return point.y;
  }))) + value;
  var lonMin = Math.min.apply(Math, _toConsumableArray(list.map(function (point) {
    return point.x;
  }))) - value;
  var lonMax = Math.max.apply(Math, _toConsumableArray(list.map(function (point) {
    return point.x;
  }))) + value;
  list = list.map(function (v) {
    return _objectSpread(_objectSpread({}, v), {}, {
      x: Math.floor((v.x - lonMin) / (lonMax - lonMin) * width),
      y: Math.floor((v.y - latMin) / (latMax - latMin) * height)
    });
  });
  return {
    data: list,
    latMin: latMin,
    latMax: latMax,
    lonMin: lonMin,
    lonMax: lonMax,
    max: 10,
    min: 0
  };
};
var HeatmapLayer = /*#__PURE__*/function (_CustomDataSource) {
  function HeatmapLayer(options) {
    var _options$data;
    var _this;
    _classCallCheck(this, HeatmapLayer);
    _this = _callSuper(this, HeatmapLayer);
    _this.map = options.map;
    _this.container = _this.createDom();
    _this.layer = _heatmap["default"].create({
      container: _this.container
    });
    if ((_options$data = options.data) !== null && _options$data !== void 0 && _options$data.length) {
      _this.loadData(options.data);
    }
    return _this;
  }
  _inherits(HeatmapLayer, _CustomDataSource);
  return _createClass(HeatmapLayer, [{
    key: "createDom",
    value: function createDom() {
      var map = this.map;
      var _map$container$getBou = map.container.getBoundingClientRect(),
        width = _map$container$getBou.width,
        height = _map$container$getBou.height;
      var heatMapDom = document.createElement('div');
      heatMapDom.style.width = width + 'px';
      heatMapDom.style.height = height + 'px';
      heatMapDom.style.display = 'none';
      map.container.appendChild(heatMapDom);
      return heatMapDom;
    }
  }, {
    key: "loadData",
    value: function loadData(_data) {
      var map = this.map;
      this.entities.removeAll();
      var _map$container$getBou2 = map.container.getBoundingClientRect(),
        width = _map$container$getBou2.width,
        height = _map$container$getBou2.height;
      var _getData = getData(_data, width, height),
        data = _getData.data,
        max = _getData.max,
        min = _getData.min,
        lonMin = _getData.lonMin,
        latMin = _getData.latMin,
        lonMax = _getData.lonMax,
        latMax = _getData.latMax;
      this.layer.setData({
        max: max,
        min: min,
        data: data
      });

      // 生成的热力图 canvas 元素类名为 heatmap-canvas
      var canvas = document.getElementsByClassName('heatmap-canvas');
      if (!canvas[0]) return;
      this.entities.add({
        name: 'heatmap',
        rectangle: {
          coordinates: _cesium.Rectangle.fromDegrees(lonMin, latMin, lonMax, latMax),
          material: new _cesium.ImageMaterialProperty({
            image: canvas[0],
            transparent: true
          })
        }
      });
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var map = this.map;
      this.container && map.container.removeChild(this.container);
    }
  }]);
}(_cesium.CustomDataSource);
var _default = exports["default"] = HeatmapLayer;
//# sourceMappingURL=HeatmapLayer.js.map
