"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _proj = _interopRequireDefault(require("proj4"));
var _LASParser = _interopRequireDefault(require("../Parser/LASParser"));
var _PotreeBinParser = _interopRequireDefault(require("../Parser/PotreeBinParser"));
var _Fetcher = _interopRequireDefault(require("../Provider/Fetcher"));
var _Source = _interopRequireDefault(require("./Source"));
/**
 * @classdesc
 * An object defining the source of Entwine Point Tile data. It fetches and
 * parses the main configuration file of Entwine Point Tile format,
 * [`ept.json`](https://entwine.io/entwine-point-tile.html#ept-json).
 *
 * @extends Source
 *
 * @property {boolean} isEntwinePointTileSource - Used to checkout whether this
 * source is a EntwinePointTileSource. Default is true. You should not change
 * this, as it is used internally for optimisation.
 * @property {string} url - The URL of the directory containing the whole
 * Entwine Point Tile structure.
 */
class EntwinePointTileSource extends _Source.default {
  /**
   * @constructor
   *
   * @param {Object} config - The configuration, see {@link Source} for
   * available values.
   * @param {number|string} [config.colorDepth='auto'] - Does the color
   * encoding is known ? Is it `8` or `16` bits ? By default it is to
   * `'auto'`, but it will be more performant if a specific value is set.
   */
  constructor(config) {
    super(config);
    this.isEntwinePointTileSource = true;
    this.colorDepth = config.colorDepth;

    // Necessary because we use the url without the ept.json part as a base
    this.url = this.url.replace('/ept.json', '');

    // https://entwine.io/entwine-point-tile.html#ept-json
    this.whenReady = _Fetcher.default.json(`${this.url}/ept.json`, this.networkOptions).then(metadata => {
      // Set parser and its configuration from schema
      this.parse = metadata.dataType === 'laszip' ? _LASParser.default.parse : _PotreeBinParser.default.parse;
      this.extension = metadata.dataType === 'laszip' ? 'laz' : 'bin';
      if (metadata.srs && metadata.srs.authority && metadata.srs.horizontal) {
        this.crs = `${metadata.srs.authority}:${metadata.srs.horizontal}`;
        if (!_proj.default.defs(this.crs)) {
          _proj.default.defs(this.crs, metadata.srs.wkt);
        }
        if (metadata.srs.vertical && metadata.srs.vertical !== metadata.srs.horizontal) {
          console.warn('EntwinePointTileSource: Vertical coordinates system code is not yet supported.');
        }
      }

      // NOTE: this spacing is kinda arbitrary here, we take the width and
      // length (height can be ignored), and we divide by the specified
      // span in ept.json. This needs improvements.
      this.spacing = (Math.abs(metadata.boundsConforming[3] - metadata.boundsConforming[0]) + Math.abs(metadata.boundsConforming[4] - metadata.boundsConforming[1])) / (2 * metadata.span);
      this.boundsConforming = metadata.boundsConforming;
      this.span = metadata.span;
      return this;
    });
    this.fetcher = _Fetcher.default.arrayBuffer;
  }
}
var _default = EntwinePointTileSource;
exports.default = _default;