"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 _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

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

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

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

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

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

var _Style = _interopRequireDefault(require("../Core/Style"));

var _Cache = require("../Core/Scheduler/Cache");

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; 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 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(o); 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) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { 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; } }

/**
 * Fires when the visiblity of the layer has changed.
 * @event ColorLayer#visible-property-changed
 */

/**
 * Fires when the opacity of the layer has changed.
 * @event ColorLayer#opacity-property-changed
 */

/**
 * Fires when the sequence of the layer has changed, meaning that the order of
 * the layer changed in the view it is attached to.
 * @event ColorLayer#sequence-property-changed
 */

/**
 * @property {boolean} isColorLayer - Used to checkout whether this layer is a
 * ColorLayer. Default is true. You should not change this, as it is used
 * internally for optimisation.
 */
var ColorLayer = /*#__PURE__*/function (_Layer) {
  (0, _inherits2["default"])(ColorLayer, _Layer);

  var _super = _createSuper(ColorLayer);

  /**
   * A simple layer, usually managing a texture to display on a view. For example,
   * it can be an aerial view of the ground or a simple transparent layer with the
   * roads displayed.
   *
   * @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 a ColorLayer
   * const color = new ColorLayer('roads', {
   *     source: {
   *          protocol: 'wmts',
   *          url: 'http://server.geo/wmts/SERVICE=WMTS&TILEMATRIX=%TILEMATRIX&TILEROW=%ROW&TILECOL=%COL',
   *          format: 'image/png',
   *     }
   *     transparent: true
   * });
   *
   * // Add the layer
   * view.addLayer(color);
   */
  function ColorLayer(id) {
    var _this;

    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck2["default"])(this, ColorLayer);
    config.cacheLifeTime = config.cacheLifeTime == undefined ? _Cache.CACHE_POLICIES.TEXTURE : config.cacheLifeTime;
    _this = _super.call(this, id, config);
    _this.isColorLayer = true;
    _this.style = new _Style["default"](config.style);

    _this.defineLayerProperty('visible', true);

    _this.defineLayerProperty('opacity', 1.0);

    _this.defineLayerProperty('sequence', 0);

    _this.transparent = config.transparent || _this.opacity < 1.0;
    _this.noTextureParentOutsideLimit = config.source ? config.source.isFileSource : false; // Feature options

    _this.buildExtent = true;
    _this.withNormal = false;
    _this.withAltitude = false;
    return _this;
  }

  (0, _createClass2["default"])(ColorLayer, [{
    key: "update",
    value: function update(context, layer, node, parent) {
      return (0, _LayeredMaterialNodeProcessing.updateLayeredMaterialNodeImagery)(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 _iterator = _createForOfIteratorHelper(this.parent.level0Nodes),
          _step;

      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var root = _step.value;
          root.traverse((0, _LayeredMaterialNodeProcessing.removeLayeredMaterialNodeLayer)(this.id));
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
  }]);
  return ColorLayer;
}(_Layer2["default"]);

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