"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 _createFeatureCollection = require("../feature-layer/createFeatureCollection");
var _heatmap = _interopRequireDefault(require("heatmap.js"));
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 _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 _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 _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 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(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 _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 _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 _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; }
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; }
var _Cesium = Cesium,
  CustomDataSource = _Cesium.CustomDataSource,
  Rectangle = _Cesium.Rectangle,
  ImageMaterialProperty = _Cesium.ImageMaterialProperty;
;
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) {
  _inherits(HeatmapLayer, _CustomDataSource);
  var _super = _createSuper(HeatmapLayer);
  function HeatmapLayer(options) {
    var _options$data;
    var _this;
    _classCallCheck(this, HeatmapLayer);
    _this = _super.call(this);
    _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;
  }
  _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: Rectangle.fromDegrees(lonMin, latMin, lonMax, latMax),
          material: new ImageMaterialProperty({
            image: canvas[0],
            transparent: true
          })
        }
      });
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var map = this.map;
      this.container && map.container.removeChild(this.container);
    }
  }]);
  return HeatmapLayer;
}(CustomDataSource);
var _default = HeatmapLayer;
exports["default"] = _default;
//# sourceMappingURL=HeatmapLayer.js.map
