"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.GLOBE_VIEW_EVENTS = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var THREE = _interopRequireWildcard(require("three"));

var _View2 = _interopRequireWildcard(require("../View"));

var _GlobeControls = _interopRequireDefault(require("../../Controls/GlobeControls"));

var _GlobeLayer = _interopRequireDefault(require("./Globe/GlobeLayer"));

var _Atmosphere = _interopRequireDefault(require("./Globe/Atmosphere"));

var _Coordinates = _interopRequireDefault(require("../Geographic/Coordinates"));

var _Crs = _interopRequireDefault(require("../Geographic/Crs"));

var _Ellipsoid = require("../Math/Ellipsoid");

/**
 * Fires when the view is completely loaded. Controls and view's functions can be called then.
 * @event GlobeView#initialized
 * @property target {view} dispatched on view
 * @property type {string} initialized
 */

/**
 * Fires when a layer is added
 * @event GlobeView#layer-added
 * @property layerId {string} the id of the layer
 * @property target {view} dispatched on view
 * @property type {string} layers-added
 */

/**
 * Fires when a layer is removed
 * @event GlobeView#layer-removed
 * @property layerId {string} the id of the layer
 * @property target {view} dispatched on view
 * @property type {string} layers-added
 */

/**
 * Fires when the layers oder has changed
 * @event GlobeView#layers-order-changed
 * @property new {object}
 * @property new.sequence {array}
 * @property new.sequence.0 {number} the new layer at position 0
 * @property new.sequence.1 {number} the new layer at position 1
 * @property new.sequence.2 {number} the new layer at position 2
 * @property previous {object}
 * @property previous.sequence {array}
 * @property previous.sequence.0 {number} the previous layer at position 0
 * @property previous.sequence.1 {number} the previous layer at position 1
 * @property previous.sequence.2 {number} the previous layer at position 2
 * @property target {view} dispatched on view
 * @property type {string} layers-order-changed
 */

/**
 * Globe's EVENT
 * @property GLOBE_INITIALIZED {string} Deprecated: emit one time when globe is initialized (use VIEW_EVENTS.INITIALIZED instead).
 * @property LAYER_ADDED {string} Deprecated: emit when layer id added in viewer (use VIEW_EVENTS.LAYER_ADDED instead).
 * @property LAYER_REMOVED {string} Deprecated: emit when layer id removed in viewer (use VIEW_EVENTS.LAYER_REMOVED instead).
 * @property COLOR_LAYERS_ORDER_CHANGED {string} Deprecated: emit when  color layers order change (use VIEW_EVENTS.COLOR_LAYERS_ORDER_CHANGED instead).
 */
var GLOBE_VIEW_EVENTS = {
  GLOBE_INITIALIZED: _View2.VIEW_EVENTS.INITIALIZED,
  LAYER_ADDED: _View2.VIEW_EVENTS.LAYER_ADDED,
  LAYER_REMOVED: _View2.VIEW_EVENTS.LAYER_REMOVED,
  COLOR_LAYERS_ORDER_CHANGED: _View2.VIEW_EVENTS.COLOR_LAYERS_ORDER_CHANGED
};
exports.GLOBE_VIEW_EVENTS = GLOBE_VIEW_EVENTS;

var GlobeView =
/*#__PURE__*/
function (_View) {
  (0, _inherits2["default"])(GlobeView, _View);

  /**
   * Creates a view of a globe.
   *
   * @constructor
   *
   * @example
   * var viewerDiv = document.getElementById('viewerDiv');
   * var position = new itowns.Coordinates('WGS84', 2.35, 48.8, 25e6);
   * var view = new itowns.GlobeView(viewerDiv, position);
   *
   * @example
   * var viewerDiv = document.getElementById('viewerDiv');
   * var position = { longitude: 2.35, latitude: 48.8, altitude: 25e6 };
   * var view = new itowns.GlobeView(viewerDiv, position);
   *
   * @param {HTMLDivElement} viewerDiv - Where to attach the view and display it
   * in the DOM.
   * @param {object|Coordinates} coordCarto - An object containing three
   * properties: longitude, latitude and altitude. It will help placing the camera
   * on the globe at the creation.
   * @param {object=} options - See options of {@link View}.
   */
  function GlobeView(viewerDiv, coordCarto) {
    var _this;

    var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    (0, _classCallCheck2["default"])(this, GlobeView);
    THREE.Object3D.DefaultUp.set(0, 0, 1); // Setup View

    _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(GlobeView).call(this, 'EPSG:4978', viewerDiv, options)); // Configure camera

    var positionCamera;

    if (coordCarto.isCoordinates) {
      positionCamera = coordCarto.as('EPSG:4326');
    } else {
      positionCamera = new _Coordinates["default"]('EPSG:4326', coordCarto.longitude, coordCarto.latitude, coordCarto.altitude);
    }

    _this.camera.camera3D.near = Math.max(15.0, 0.000002352 * _Ellipsoid.ellipsoidSizes.x);
    _this.camera.camera3D.far = _Ellipsoid.ellipsoidSizes.x * 10;
    var tileLayer = new _GlobeLayer["default"]('globe', options.object3d, options);
    var sun = new THREE.DirectionalLight();
    sun.position.set(-0.5, 0, 1);
    sun.updateMatrixWorld(true);
    tileLayer.object3d.add(sun);

    _this.addLayer(tileLayer); // Configure controls


    var positionTargetCamera = positionCamera.clone();
    positionTargetCamera.altitude = 0;

    if (options.noControls) {
      _this.camera.setPosition(positionCamera);

      _this.camera.camera3D.lookAt(positionTargetCamera.as('EPSG:4978').toVector3());
    } else {
      _this.controls = new _GlobeControls["default"]((0, _assertThisInitialized2["default"])(_this), positionTargetCamera, positionCamera.altitude, _Ellipsoid.ellipsoidSizes.x);
      _this.controls.handleCollision = typeof options.handleCollision !== 'undefined' ? options.handleCollision : true;
    }

    _this.tileLayer = tileLayer;

    _this.addLayer(new _Atmosphere["default"]()); // GlobeView needs this.camera.resize to set perpsective matrix camera


    _this.camera.resize(viewerDiv.clientWidth, viewerDiv.clientHeight);

    return _this;
  }

  (0, _createClass2["default"])(GlobeView, [{
    key: "addLayer",
    value: function addLayer(layer) {
      if (!layer) {
        return new Promise(function (resolve, reject) {
          return reject(new Error('layer is undefined'));
        });
      }

      if (layer.isColorLayer) {
        var colorLayerCount = this.getLayers(function (l) {
          return l.isColorLayer;
        }).length;
        layer.sequence = colorLayerCount;

        if (!this.tileLayer.tileMatrixSets.includes(_Crs["default"].formatToTms(layer.source.projection))) {
          throw new Error("Only ".concat(this.tileLayer.tileMatrixSets, " tileMatrixSet are currently supported for color layers"));
        }
      } else if (layer.isElevationLayer) {
        if (_Crs["default"].formatToTms(layer.source.projection) !== this.tileLayer.tileMatrixSets[0]) {
          throw new Error("Only ".concat(this.tileLayer.tileMatrixSets[0], " tileMatrixSet is currently supported for elevation layers"));
        }
      }

      return (0, _get2["default"])((0, _getPrototypeOf2["default"])(GlobeView.prototype), "addLayer", this).call(this, layer, this.tileLayer);
    }
  }]);
  return GlobeView;
}(_View2["default"]);

var _default = GlobeView;
exports["default"] = _default;