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

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 _Picking = _interopRequireDefault(require("../Core/Picking"));

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

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; } }

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(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 disposeMesh(obj) {
  if (obj.dispose) {
    obj.dispose();
  } else {
    if (obj.geometry) {
      obj.geometry.dispose();
    }

    if (obj.material) {
      if (Array.isArray(obj.material)) {
        var _iterator = _createForOfIteratorHelper(obj.material),
            _step;

        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var material = _step.value;
            material.dispose();
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      } else {
        obj.material.dispose();
      }
    }
  }
}

function traverse(obj, callback) {
  var _iterator2 = _createForOfIteratorHelper(obj.children),
      _step2;

  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var child = _step2.value;
      traverse(child, callback);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }

  callback(obj);
}
/**
 * Fires when the opacity of the layer has changed.
 * @event GeometryLayer#opacity-property-changed
 */

/**
 * @property {boolean} isGeometryLayer - Used to checkout whether this layer is
 * a GeometryLayer. Default is true. You should not change this, as it is used
 * internally for optimisation.
 */


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

  var _super = _createSuper(GeometryLayer);

  /**
   * A layer usually managing a geometry to display on a view. For example, it
   * can be a layer of buildings extruded from a a WFS stream.
   *
   * @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 {THREE.Object3d} object3d - The object3d used to contain the
   * geometry of the GeometryLayer. It is usually a `THREE.Group`, but it can
   * be anything inheriting from a `THREE.Object3d`.
   * @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.
   *
   * @throws {Error} `object3d` must be a valid `THREE.Object3d`.
   *
   * @example
   * // Create a GeometryLayer
   * const geometry = new GeometryLayer('buildings', {
   *      source: {
   *          url: 'http://server.geo/wfs?',
   *          protocol: 'wfs',
   *          format: 'application/json'
   *      },
   * });
   *
   * // Add the layer
   * view.addLayer(geometry);
   */
  function GeometryLayer(id, object3d) {
    var _this;

    var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    (0, _classCallCheck2["default"])(this, GeometryLayer);
    config.cacheLifeTime = config.cacheLifeTime == undefined ? _Cache.CACHE_POLICIES.GEOMETRY : config.cacheLifeTime;
    _this = _super.call(this, id, config);
    _this.isGeometryLayer = true;

    if (!object3d || !object3d.isObject3D) {
      throw new Error("Missing/Invalid object3d parameter (must be a\n                three.js Object3D instance)");
    }

    if (object3d.type === 'Group' && object3d.name === '') {
      object3d.name = id;
    }

    Object.defineProperty((0, _assertThisInitialized2["default"])(_this), 'object3d', {
      value: object3d,
      writable: false,
      configurable: true
    });

    _this.defineLayerProperty('opacity', 1.0, function () {
      var root = _this.parent ? _this.parent.object3d : _this.object3d;
      root.traverse(function (object) {
        if (object.layer == (0, _assertThisInitialized2["default"])(_this)) {
          _this.changeOpacity(object);
        } else if (object.content && object.content.layer == (0, _assertThisInitialized2["default"])(_this)) {
          object.content.traverse(_this.changeOpacity);
        }
      });
    });

    _this.defineLayerProperty('wireframe', false, function () {
      var root = _this.parent ? _this.parent.object3d : _this.object3d;
      root.traverse(function (object) {
        if (object.layer == (0, _assertThisInitialized2["default"])(_this) && object.material) {
          object.material.wireframe = _this.wireframe;
        } else if (object.content && object.content.layer == (0, _assertThisInitialized2["default"])(_this)) {
          object.content.traverse(function (o) {
            if (o.material && o.layer == (0, _assertThisInitialized2["default"])(_this)) {
              o.material.wireframe = _this.wireframe;
            }
          });
        }
      });
    });

    _this.attachedLayers = [];
    _this.visible = config.visible == undefined ? true : config.visible;
    return _this;
  } // Attached layers expect to receive the visual representation of a
  // layer (= THREE object with a material).  So if a layer's update
  // function don't process this kind of object, the layer must provide a
  // getObjectToUpdateForAttachedLayers function that returns the correct
  // object to update for attached layer.
  // See 3dtilesLayer or PotreeLayer for examples.
  // eslint-disable-next-line arrow-body-style


  (0, _createClass2["default"])(GeometryLayer, [{
    key: "getObjectToUpdateForAttachedLayers",
    value: function getObjectToUpdateForAttachedLayers(obj) {
      if (obj.parent && obj.material) {
        return {
          element: obj,
          parent: obj.parent
        };
      }
    } // Placeholder
    // eslint-disable-next-line

  }, {
    key: "postUpdate",
    value: function postUpdate() {} // Placeholder
    // eslint-disable-next-line

  }, {
    key: "culling",
    value: function culling() {
      return true;
    }
    /**
     * Attach another layer to this one. Layers attached to a GeometryLayer will
     * be available in `geometryLayer.attachedLayers`.
     *
     * @param {Layer} layer - The layer to attach, that must have an `update`
     * method.
     */

  }, {
    key: "attach",
    value: function attach(layer) {
      if (!layer.update) {
        throw new Error("Missing 'update' function -> can't attach layer\n                ".concat(layer.id));
      }

      this.attachedLayers.push(layer); // To traverse GeometryLayer object3d attached

      layer.parent = this;
    }
    /**
     * Detach a layer attached to this one. See {@link attach} to learn how to
     * attach a layer.
     *
     * @param {Layer} layer - The layer to detach.
     *
     * @return {boolean} Confirmation of the detachment of the layer.
     */

  }, {
    key: "detach",
    value: function detach(layer) {
      var count = this.attachedLayers.length;
      this.attachedLayers = this.attachedLayers.filter(function (attached) {
        return attached.id != layer.id;
      });
      layer.parent = undefined;
      return this.attachedLayers.length < count;
    }
    /**
     * All layer's meshs are removed from scene and disposed from video device.
     */

  }, {
    key: "delete",
    value: function _delete() {
      var _this2 = this;

      // if Layer is attached
      if (this.parent) {
        traverse(this.parent.object3d, function (obj) {
          if (obj.layer && obj.layer.id == _this2.id) {
            obj.parent.remove(obj);
            disposeMesh(obj);
          }
        });
      }

      if (this.object3d.parent) {
        this.object3d.parent.remove(this.object3d);
      }

      this.object3d.traverse(disposeMesh);
    }
    /**
     * Picking method for this layer. It uses the {@link Picking#pickObjectsAt}
     * method.
     *
     * @param {View} view - The view instance.
     * @param {Object} coordinates - The coordinates to pick in the view. It
     * should have at least `x` and `y` properties.
     * @param {number} radius - Radius of the picking circle.
     * @param {Array} target - target to push result.
     *
     * @return {Array} An array containing all targets picked under the
     * specified coordinates.
     */

  }, {
    key: "pickObjectsAt",
    value: function pickObjectsAt(view, coordinates) {
      var radius = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.options.defaultPickingRadius;
      var target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
      var object3d = this.parent ? this.parent.object3d : this.object3d;
      return _Picking["default"].pickObjectsAt(view, coordinates, radius, object3d, target, this.threejsLayer);
    }
    /**
     * Change the opacity of an object, according to the value of the `opacity`
     * property of this layer.
     *
     * @param {Object} object - The object to change the opacity from. It is
     * usually a `THREE.Object3d` or an implementation of it.
     */

  }, {
    key: "changeOpacity",
    value: function changeOpacity(object) {
      if (object.material) {
        // != undefined: we want the test to pass if opacity is 0
        if (object.material.opacity != undefined) {
          object.material.transparent = this.opacity < 1.0;
          object.material.opacity = this.opacity;
        }

        if (object.material.uniforms && object.material.uniforms.opacity != undefined) {
          object.material.transparent = this.opacity < 1.0;
          object.material.uniforms.opacity.value = this.opacity;
        }
      }
    }
  }]);
  return GeometryLayer;
}(_Layer2["default"]);

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