"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");
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); }
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 getTop = function getTop(top, height, type) {
  if (type === 'top') {
    return top;
  } else if (type === 'bottom') {
    return top - height;
  } else if (type === 'center') {
    return top - height / 2;
  }
  return top;
};
var getLeft = function getLeft(left, width, type) {
  if (type === 'left') {
    return left;
  } else if (type === 'right') {
    return left - width;
  } else if (type === 'center') {
    return left - width / 2;
  }
  return left;
};
var getTopLeft = function getTopLeft(ele, canvasPosition, positioning) {
  var width = ele.offsetWidth;
  var height = ele.offsetHeight;
  var top = canvasPosition.y;
  var left = canvasPosition.x;
  var _positioning$split = positioning.split('-'),
    _positioning$split2 = _slicedToArray(_positioning$split, 2),
    v = _positioning$split2[0],
    h = _positioning$split2[1];
  return [getTop(top, height, v), getLeft(left, width, h)];
};
var Dom = exports["default"] = /*#__PURE__*/function () {
  function Dom(options) {
    _classCallCheck(this, Dom);
    var element = options.element,
      position = options.position,
      positioning = options.positioning,
      map = options.map;
    this.map = map;
    this.element = element;
    this.position = position;
    this.positioning = positioning || 'bottom-center';
    this.loadData();
  }
  return _createClass(Dom, [{
    key: "setPosition",
    value: function setPosition(position) {
      this.position = position;
      this.loadData();
    }
  }, {
    key: "loadData",
    value: function loadData() {
      var _this = this;
      var map = this.map;
      var position = this.position;
      map.scene.preRender.removeEventListener(_this.render.bind(_this));
      if (position !== null && position !== void 0 && position.length) {
        this._style = {};
        map.scene.preRender.addEventListener(_this.render.bind(_this));
      }
    }
  }, {
    key: "render",
    value: function render() {
      var map = this.map;
      var position = this.position;
      if (position !== null && position !== void 0 && position.length) {
        var scratch = new _cesium.Cartesian2();
        var point = _cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0);
        var canvasPosition = map.scene.cartesianToCanvasCoordinates(point, scratch);
        if ((0, _cesium.defined)(canvasPosition)) {
          var _getTopLeft = getTopLeft(this.element, canvasPosition, this.positioning),
            _getTopLeft2 = _slicedToArray(_getTopLeft, 2),
            top = _getTopLeft2[0],
            left = _getTopLeft2[1];
          if (this._style.left) {
            this._style.left = this._style.left - left > 1 || this._style.left - left < -1 ? left : this._style.left;
          } else {
            this._style.left = left;
          }
          this._style.top = top;
          this.element.style.top = top + "px";
          this.element.style.left = left + "px";

          // const cameraPosition = map.camera.position;
          // let height = map.scene.globe.ellipsoid.cartesianToCartographic(cameraPosition)
          //     .height;
          // height += map.scene.globe.ellipsoid.maximumRadius;
          // if (!(Cartesian3.distance(cameraPosition, position) > height)) {
          //     this.element.style.display = 'block';
          // } else {
          //     this.element.style.display = 'none';
          // }
        }
      }
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var _this = this;
      this.map.scene.preRender.removeEventListener(_this.render.bind(_this));
    }
  }]);
}();
//# sourceMappingURL=Dom.js.map
