"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var THREE = _interopRequireWildcard(require("three"));
var _LASLoader = _interopRequireDefault(require("./LASLoader"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const lasLoader = new _LASLoader.default();

/** The LASParser module provides a [parse]{@link
 * module:LASParser.parse} method that takes a LAS or LAZ (LASZip) file in, and
 * gives a `THREE.BufferGeometry` containing all the necessary attributes to be
 * displayed in iTowns. It uses the
 * [copc.js](https://github.com/connormanning/copc.js/) library.
 *
 * @module LASParser
 */
var _default = {
  /*
   * Set the laz-perf decoder path.
   * @param {string} path - path to `laz-perf.wasm` folder.
   */
  enableLazPerf(path) {
    if (!path) {
      throw new Error('Path to laz-perf is mandatory');
    }
    lasLoader.lazPerf = path;
  },
  /**
   * Parses a LAS file or a LAZ (LASZip) file and return the corresponding
   * `THREE.BufferGeometry`.
   *
   * @param {ArrayBuffer} data - The file content to parse.
   * @param {Object} [options]
   * @param {Object} [options.in] - Options to give to the parser.
   * @param { 8 | 16 } [options.in.colorDepth] - Color depth (in bits).
   * Defaults to 8 bits for LAS 1.2 and 16 bits for later versions
   * (as mandatory by the specification)
   *
   * @return {Promise} A promise resolving with a `THREE.BufferGeometry`. The
   * header of the file is contained in `userData`.
   */
  parse(data) {
    var _options$out, _options$in;
    let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    if ((_options$out = options.out) !== null && _options$out !== void 0 && _options$out.skip) {
      console.warn("Warning: options 'skip' not supported anymore");
    }
    return lasLoader.parseFile(data, {
      colorDepth: (_options$in = options.in) === null || _options$in === void 0 ? void 0 : _options$in.colorDepth
    }).then(parsedData => {
      const geometry = new THREE.BufferGeometry();
      const attributes = parsedData.attributes;
      geometry.userData = parsedData.header;
      const positionBuffer = new THREE.BufferAttribute(attributes.position, 3);
      geometry.setAttribute('position', positionBuffer);
      const intensityBuffer = new THREE.BufferAttribute(attributes.intensity, 1, true);
      geometry.setAttribute('intensity', intensityBuffer);
      const returnNumber = new THREE.BufferAttribute(attributes.returnNumber, 1);
      geometry.setAttribute('returnNumber', returnNumber);
      const numberOfReturns = new THREE.BufferAttribute(attributes.numberOfReturns, 1);
      geometry.setAttribute('numberOfReturns', numberOfReturns);
      const classBuffer = new THREE.BufferAttribute(attributes.classification, 1, true);
      geometry.setAttribute('classification', classBuffer);
      const pointSourceID = new THREE.BufferAttribute(attributes.pointSourceID, 1);
      geometry.setAttribute('pointSourceID', pointSourceID);
      if (attributes.color) {
        const colorBuffer = new THREE.BufferAttribute(attributes.color, 4, true);
        geometry.setAttribute('color', colorBuffer);
      }
      geometry.computeBoundingBox();
      return geometry;
    });
  }
};
exports.default = _default;