"use strict";

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

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

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

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

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

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

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

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

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

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

var _LayerUpdateStrategy = require("./LayerUpdateStrategy");

var _InfoLayer = _interopRequireDefault(require("./InfoLayer"));

var _Source = _interopRequireDefault(require("../Source/Source"));

var _DataSourceProvider = require("../Provider/DataSourceProvider");

function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function () {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

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(n); 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 _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

/**
 * @property {boolean} isLayer - Used to checkout whether this layer is a Layer.
 * Default is true. You should not change this, as it is used internally for
 * optimisation.
 * @property {boolean} ready - This property is false when the layer isn't added.
 * It's true when the layer is added and all initializations are done.
 * @property {Source} source - This source determines the datas to be displayed with the layer.
 * The layer determines how this data are displayed.
 * By example:
 * * For ColorLayer/ElevationLayer, the source datas are rasterised (if it's necessary).
 * * For GeometryLayer, the source datas are converted to meshes (not possible for the raster data sources).
 * @property {Promise} whenReady - this promise is resolved when the layer is added and all initializations are done.
 * This promise is resolved with this layer.
 * This promise is returned by [View#addLayer]{@link View}.
 */
var Layer = /*#__PURE__*/function (_THREE$EventDispatche) {
  (0, _inherits2["default"])(Layer, _THREE$EventDispatche);

  var _super = _createSuper(Layer);

  /**
   * Don't use directly constructor to instance a new Layer. Instead, use
   * another available type of Layer, implement a new one inheriting from this
   * one or use [View#addLayer]{@link View}.
   *
   * @constructor
   * @protected
   *
   * @param {string} id - The id of the layer, that should be unique. It is
   * not mandatory, but an error will be emitted if this layer is added a
   * {@link View} that already has a layer going by that id.
   * @param {Object} [config] - Optional configuration, all elements in it
   * will be merged as is in the layer. For example, if the configuration
   * contains three elements `name, protocol, extent`, these elements will be
   * available using `layer.name` or something else depending on the property
   * name.
   *
   * @example
   * // Add and create a new Layer
   * const newLayer = new Layer('id', options);
   * view.addLayer(newLayer);
   *
   * // Change layer's visibility
   * const layerToChange = view.getLayerById('idLayerToChange');
   * layerToChange.visible = false;
   * view.notifyChange(); // update viewer
   *
   * // Change layer's opacity
   * const layerToChange = view.getLayerById('idLayerToChange');
   * layerToChange.opacity = 0.5;
   * view.notifyChange(); // update viewer
   *
   * // Listen properties
   * const layerToListen = view.getLayerById('idLayerToListen');
   * layerToListen.addEventListener('visible-property-changed', (event) => console.log(event));
   * layerToListen.addEventListener('opacity-property-changed', (event) => console.log(event));
   */
  function Layer(id) {
    var _this;

    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck2["default"])(this, Layer);
    _this = _super.call(this);
    _this.isLayer = true;
    Object.assign((0, _assertThisInitialized2["default"])(_this), config);
    Object.defineProperty((0, _assertThisInitialized2["default"])(_this), 'id', {
      value: id,
      writable: false
    }); // Default properties

    _this.options = config.options || {};

    if (!_this.updateStrategy) {
      _this.updateStrategy = {
        type: _LayerUpdateStrategy.STRATEGY_MIN_NETWORK_TRAFFIC,
        options: {}
      };
    }

    _this.defineLayerProperty('frozen', false);

    _this.info = new _InfoLayer["default"]((0, _assertThisInitialized2["default"])(_this));
    _this.source = _this.source || new _Source["default"]({
      url: 'none'
    });
    _this.ready = false;
    _this._promises = [];
    _this.whenReady = new Promise(function (re, rj) {
      _this._resolve = re;
      _this._reject = rj;
    }).then(function () {
      if (_this.source.fetchedData && !_this.source.parsedData) {
        return (0, _DataSourceProvider.parseSourceData)(_this.source.fetchedData, _this.source.extent, (0, _assertThisInitialized2["default"])(_this));
      }
    }).then(function () {
      _this.ready = true;
      return (0, _assertThisInitialized2["default"])(_this);
    });

    _this._promises.push(_this.source.whenReady);

    return _this;
  }

  (0, _createClass2["default"])(Layer, [{
    key: "addInitializationStep",
    value: function addInitializationStep() {
      // Possibility to add rejection handler, if it's necessary.
      var resolve;

      this._promises.push(new Promise(function (re) {
        resolve = re;
      }));

      return resolve;
    }
    /**
     * Defines a property for this layer, with a default value and a callback
     * executed when the property changes.
     * <br><br>
     * When changing the property, it also emits an event, named following this
     * convention: `${propertyName}-property-changed`, with `${propertyName}`
     * being replaced by the name of the property.  For example, if the added
     * property name is `frozen`, it will emit a `frozen-property-changed`.
     * <br><br>
     * @example <caption>The emitted event has some properties assigned to it</caption>
     * event = {
     *     new: {
     *         ${propertyName}: * // the new value of the property
     *     },
     *     previous: {
     *         ${propertyName}: * // the previous value of the property
     *     },
     *     target: Layer // the layer it has been dispatched from
     *     type: string // the name of the emitted event
     * }
     *
     * @param {string} propertyName - The name of the property, also used in
     * the emitted event name.
     * @param {*} defaultValue - The default set value.
     * @param {function} [onChange] - The function executed when the property is
     * changed. Parameters are the layer the property is defined on, and the
     * name of the property.
     */

  }, {
    key: "defineLayerProperty",
    value: function defineLayerProperty(propertyName, defaultValue, onChange) {
      var _this2 = this;

      var existing = Object.getOwnPropertyDescriptor(this, propertyName);

      if (!existing || !existing.set) {
        var property = this[propertyName] == undefined ? defaultValue : this[propertyName];
        Object.defineProperty(this, propertyName, {
          get: function get() {
            return property;
          },
          set: function set(newValue) {
            if (property !== newValue) {
              var event = {
                type: "".concat(propertyName, "-property-changed"),
                previous: {},
                "new": {}
              };
              event.previous[propertyName] = property;
              event["new"][propertyName] = newValue;
              property = newValue;

              if (onChange) {
                onChange(_this2, propertyName);
              }

              _this2.dispatchEvent(event);
            }
          }
        });
      }
    } // Placeholder
    // eslint-disable-next-line

  }, {
    key: "convert",
    value: function convert(data) {
      return data;
    }
    /**
     * Remove and dispose all objects from layer.
     */
    // eslint-disable-next-line

  }, {
    key: "delete",
    value: function _delete() {
      console.warn('Function delete doesn\'t exist for this layer');
    }
  }]);
  return Layer;
}(THREE.EventDispatcher);

var _default = Layer;
exports["default"] = _default;
var ImageryLayers = {
  // move this to new index
  // After the modification :
  //      * the minimum sequence will always be 0
  //      * the maximum sequence will always be layers.lenght - 1
  // the ordering of all layers (Except that specified) doesn't change
  moveLayerToIndex: function (layer, newIndex, imageryLayers) {
    newIndex = Math.min(newIndex, imageryLayers.length - 1);
    newIndex = Math.max(newIndex, 0);
    var oldIndex = layer.sequence;

    var _iterator = _createForOfIteratorHelper(imageryLayers),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var imagery = _step.value;

        if (imagery.id === layer.id) {
          // change index of specified layer
          imagery.sequence = newIndex;
        } else if (imagery.sequence > oldIndex && imagery.sequence <= newIndex) {
          // down all layers between the old index and new index (to compensate the deletion of the old index)
          imagery.sequence--;
        } else if (imagery.sequence >= newIndex && imagery.sequence < oldIndex) {
          // up all layers between the new index and old index (to compensate the insertion of the new index)
          imagery.sequence++;
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  },
  moveLayerDown: function (layer, imageryLayers) {
    if (layer.sequence > 0) {
      this.moveLayerToIndex(layer, layer.sequence - 1, imageryLayers);
    }
  },
  moveLayerUp: function (layer, imageryLayers) {
    var m = imageryLayers.length - 1;

    if (layer.sequence < m) {
      this.moveLayerToIndex(layer, layer.sequence + 1, imageryLayers);
    }
  },
  getColorLayersIdOrderedBySequence: function (imageryLayers) {
    var copy = Array.from(imageryLayers);
    copy.sort(function (a, b) {
      return a.sequence - b.sequence;
    });
    return copy.map(function (l) {
      return l.id;
    });
  }
};
exports.ImageryLayers = ImageryLayers;