"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = 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 _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

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

var _Layer2 = _interopRequireDefault(require("./Layer"));

var _LayeredMaterialNodeProcessing = require("../Process/LayeredMaterialNodeProcessing");

var _textureConverter = _interopRequireDefault(require("../Converter/textureConverter"));

/**
 * @property {boolean} isElevationLayer - Used to checkout whether this layer is
 * an ElevationLayer. Default is true. You should not change this, as it is used
 * internally for optimisation.
 * @property {number} scale - Used to apply a scale on the elevation value. It
 * can be used for exageration of the elevation, like in [this
 * example](https://www.itowns-project.org/itowns/examples/tiff.html).
 */
var ElevationLayer =
/*#__PURE__*/
function (_Layer) {
  (0, _inherits2["default"])(ElevationLayer, _Layer);

  /**
   * A simple layer, managing an elevation texture to add some reliefs on the
   * plane or globe view for example.
   *
   * @constructor
   * @extends Layer
   *
   * @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.
   * @param {Source} [config.source] - Description and options of the source.
   *
   * @example
   * // Create an ElevationLayer
   * const elevation = new ElevationLayer('IGN_MNT', {
   *      source: {
   *          url: 'http://server.geo/wmts/SERVICE=WMTS&TILEMATRIX=%TILEMATRIX&TILEROW=%ROW&TILECOL=%COL',
   *          protocol: 'wmts',
   *          format: 'image/x-bil;bits=32',
   *      },
   * });
   *
   * // Add the layer
   * view.addLayer(elevation);
   */
  function ElevationLayer(id) {
    var _this;

    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck2["default"])(this, ElevationLayer);
    _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(ElevationLayer).call(this, id, config));
    _this.isElevationLayer = true; // This is used to add a factor needed to color texture

    var baseScale = 1.0;

    if (_this.useColorTextureElevation) {
      baseScale = _this.colorTextureElevationMaxZ - _this.colorTextureElevationMinZ;
    }

    _this.defineLayerProperty('scale', _this.scale || 1.0, function (self) {
      self.parent.object3d.traverse(function (obj) {
        if (obj.layer == self.parent && obj.material) {
          obj.material.setElevationScale(self.scale * baseScale);
          obj.obb.updateScaleZ(self.scale);
        }
      });
    });

    return _this;
  }

  (0, _createClass2["default"])(ElevationLayer, [{
    key: "update",
    value: function update(context, layer, node, parent) {
      return (0, _LayeredMaterialNodeProcessing.updateLayeredMaterialNodeElevation)(context, this, node, parent);
    }
  }, {
    key: "convert",
    value: function convert(data, extentDestination) {
      return _textureConverter["default"].convert(data, extentDestination, this);
    }
    /**
    * All layer's textures are removed from scene and disposed from video device.
    */

  }, {
    key: "delete",
    value: function _delete() {
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = this.parent.level0Nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var root = _step.value;
          root.traverse((0, _LayeredMaterialNodeProcessing.removeLayeredMaterialNodeLayer)(this.id));
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator["return"] != null) {
            _iterator["return"]();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }
    }
  }]);
  return ElevationLayer;
}(_Layer2["default"]);

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