"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;
require("./index.less");
var _util = _interopRequireDefault(require("./util"));
var _class;
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 _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 _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
function _classStaticPrivateMethodGet(receiver, classConstructor, method) { _classCheckPrivateStaticAccess(receiver, classConstructor); return method; }
function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } }
var _Cesium = Cesium,
  Cesium3DTileFeature = _Cesium.Cesium3DTileFeature,
  Cartesian2 = _Cesium.Cartesian2,
  Cartesian3 = _Cesium.Cartesian3,
  Cartographic = _Cesium.Cartographic,
  SceneTransforms = _Cesium.SceneTransforms,
  defined = _Cesium.defined,
  Math = _Cesium.Math;
;
/**
 * 信息工具。
 *
 * @alias InfoTool
 * @constructor
 * @param {viewer} viewer Cesium视窗。
 */
var _element = /*#__PURE__*/new WeakMap();
var InfoTool = /*#__PURE__*/function () {
  function InfoTool(options) {
    _classCallCheck(this, InfoTool);
    _classPrivateFieldInitSpec(this, _element, {
      writable: true,
      value: void 0
    });
    _defineProperty(this, "viewer", void 0);
    this.viewer = options.map;
    // 在Cesium容器中添加元素
    _classPrivateFieldSet(this, _element, document.createElement('div'));
  }

  /**
   * 添加。
   * @param {Object} options 选项。
   * @param {Element} options.element 弹窗元素。
   * @param {Cartesian2|Cartesian3} options.position 点击位置。
   * @param {Cesium3DTileFeature} [options.inputFeature] 模型要素。
   * @param {String} options.type 类型（默认值为default，即任意点击模式；如果设置为info，即信息模式，只有点击Feature才会响应）。
   * @param {String} options.content 内容（只有类型为default时才起作用）。
   * @param {Function} callback 回调函数。
   */
  _createClass(InfoTool, [{
    key: "add",
    value: function add(options) {
      var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
      this.element = options.element;
      // 判断参数为空返回
      if (!options) {
        return;
      }
      // 点
      var position, cartesian2d, cartesian3d, inputFeature;
      if (options instanceof Cesium3DTileFeature) {
        inputFeature = options;
        options = {};
      } else {
        if (options instanceof Cartesian2 || options instanceof Cartesian3) {
          position = options;
          options = {};
        } else {
          position = options.position;
          inputFeature = options.inputFeature;
        }
        // 判断点位为空返回
        if (!position) {
          return;
        }
        if (position instanceof Cartesian2) {
          // 二维转三维
          // 如果支持拾取模型则取模型值
          cartesian3d = this.viewer.scene.pickPositionSupported && defined(this.viewer.scene.pick(options.position)) ? this.viewer.scene.pickPosition(position) : this.viewer.camera.pickEllipsoid(position, this.viewer.scene.globe.ellipsoid);
          cartesian2d = position;
        } else {
          cartesian3d = position;
          cartesian2d = SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, cartesian3d);
        }
        // 判断点位为空返回
        if (!cartesian3d) {
          return;
        }
      }
      var that = this;

      // 1.组织信息
      var info = '';
      if (options.type === 'info') {
        // 拾取要素
        var feature = inputFeature || this.viewer.scene.pick(cartesian2d);
        // 判断拾取要素为空返回
        if (!defined(feature)) {
          this.remove();
          return;
        }
        if (feature instanceof Cesium3DTileFeature) {
          // 3dtiles
          var propertyNames = feature.getPropertyNames();
          var length = propertyNames.length;
          for (var i = 0; i < length; ++i) {
            var propertyName = propertyNames[i];
            info += '"' + (propertyName + '": "' + feature.getProperty(propertyName)) + '",\n';
          }
        } else if (feature) {
          // Entity
          var properties = feature.properties;
          if (properties) {
            var _propertyNames = properties._propertyNames;
            var _length = _propertyNames.length;
            for (var _i = 0; _i < _length; ++_i) {
              var _propertyName = _propertyNames[_i];
              info += '"' + (_propertyName + '": "' + properties[_propertyName]._value) + '",\n';
            }
          }
        }
      } else {
        options.content && (info = options.content);
      }

      // 2.生成特效
      // 添加之前先移除
      this.remove();
      // if (!info) {
      //   return;
      // }
      options.position = cartesian3d;
      options.element = options.element || _classPrivateFieldGet(this, _element);
      _classStaticPrivateMethodGet(InfoTool, InfoTool, _createInfoTool).call(InfoTool, this.viewer, options, function () {
        _util["default"].setInnerText(options.element, info);
        typeof callback === 'function' && callback();
      });
    }

    /**
     * 移除。
     */
  }, {
    key: "remove",
    value: function remove() {
      var entityId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
      _util["default"].setCss(this.element, 'opacity', '0');
      _util["default"].setCss(this.element, 'z-index', '-1');
      _util["default"].setCss(_classPrivateFieldGet(this, _element), 'opacity', '0');
    }
  }]);
  return InfoTool;
}();
_class = InfoTool;
/**
 * 创建一个动态实体弹窗。
 *
 * @param {viewer} viewer Cesium视窗。
 * @param {Number} options 选项。
 * @param {Cartesian3} options.position 弹出位置。
 * @param {HTMLElement} options.element 弹出窗元素容器。
 * @param {Function} callback 回调函数。
 * @ignore
 */
function _createInfoTool(viewer, options) {
  var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
  var cartographic = Cartographic.fromCartesian(options.position);
  var lon = Math.toDegrees(cartographic.longitude); //.toFixed(5);
  var lat = Math.toDegrees(cartographic.latitude); //.toFixed(5);

  // 回调
  callback();

  // 添加div弹窗
  setTimeout(function () {
    _classStaticPrivateMethodGet(_class, _class, _popup).call(_class, viewer, options.element, lon, lat, cartographic.height,
    // options.geometryType,
    options.offset);
  }, 100);
}
/**
 * 弹出HTML元素弹窗。
 * @param {viewer} viewer Cesium视窗。
 * @param {Element|HTMLElement} element 弹窗元素。
 * @param {Number} lon 经度。
 * @param {Number} lat 纬度。
 * @param {Number} height 高度。
 * @ignore
 */
function _popup(viewer, element, lon, lat, height, offset) {
  setTimeout(function () {
    // 设置元素效果
    _util["default"].setCss(element, 'opacity', '1');
    _util["default"].setCss(element, 'z-index', '0');
  }, 100);
  var divPosition = Cartesian3.fromDegrees(lon, lat, height);
  var x = (offset === null || offset === void 0 ? void 0 : offset.x) || 0;
  var y = (offset === null || offset === void 0 ? void 0 : offset.y) || 0;
  _classStaticPrivateMethodGet(_class, _class, _hookToGlobe).call(_class, viewer, element, divPosition, [-4 + x, -parseInt(_util["default"].getCss(element, 'offsetHeight') + y)], true);
  viewer.scene.requestRender();
}
/**
 * 将HTML弹窗挂接到地球上。
 *
 * @param {viewer} viewer Cesium视窗。
 * @param {Element} element 弹窗元素。
 * @param {Cartesian3} position 地图坐标点。
 * @param {Array} offset 偏移。
 * @param {Boolean} hideOnBehindGlobe 当元素在地球背面会自动隐藏，以减轻判断计算压力。
 * @ignore
 */
function _hookToGlobe(viewer, element, position, offset, hideOnBehindGlobe) {
  var scene = viewer.scene,
    camera = viewer.camera;
  var cartesian2 = new Cartesian2();
  scene.preRender.addEventListener(function () {
    var canvasPosition = scene.cartesianToCanvasCoordinates(position, cartesian2); // 笛卡尔坐标到画布坐标
    if (defined(canvasPosition)) {
      _util["default"].setCss(element, 'left', parseInt(canvasPosition.x + offset[0]) + 'px');
      _util["default"].setCss(element, 'top', parseInt(canvasPosition.y + offset[1]) + 'px');

      // 是否在地球背面隐藏
      if (hideOnBehindGlobe) {
        var cameraPosition = camera.position;
        var height = scene.globe.ellipsoid.cartesianToCartographic(cameraPosition).height;
        height += scene.globe.ellipsoid.maximumRadius;
        if (!(Cartesian3.distance(cameraPosition, position) > height)) {
          // util.setCss(element, 'display', 'flex');
        } else {
          _util["default"].setCss(element, 'display', 'none');
        }
      }
    }
  });
}
var _default = InfoTool;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
