"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 _GeoidGrid = _interopRequireDefault(require("../Core/Geographic/GeoidGrid"));
var _Extent = _interopRequireDefault(require("../Core/Geographic/Extent"));
var _GDFParser = require("./GDFParser");
var _GTXParser = require("./GTXParser");
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; }
/**
 * The `ISGParser` module provides a `[parse]{@link module:ISGParser.parse}` method. This method takes the content of a
 * ISG file in, and returns a `{@link GeoidGrid}`. the `{@link GeoidGrid}` contains all the necessary attributes and
 * methods to access the ISG data in iTowns.
 *
 * @module ISGParser
 */
var _default = {
  /**
   * Parses an ISG file content and returns a corresponding `{@link GeoidGrid}`.
   *
   * @param   {string}    isg                             The content of the ISG file to parse.
   * @param   {Object}    options                         An object gathering the optional parameters to pass to
                                                          * the parser.
   * @param   {Object}    [options.in={}]                 Information on the ISG data.
   * @param   {string}    [options.in.crs='EPSG:4326']    The Coordinates Reference System (CRS) of the ISG data.
                                                          * It must be a geographic CRS, and must be given as an EPSG
                                                          * code.
   *
   * @returns {Promise<GeoidGrid>}    A promise resolving with a `{@link GeoidGrid}`, which contains all the necessary
                                      * attributes and methods to access ISG file data.
   */
  parse(isg) {
    let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
      in: {}
    };
    const rows = isg.split('\n');
    const firstMeasureLine = rows.indexOf(rows.find(row => row.includes('end_of_head'))) + 1;
    const rawHeaderData = rows.slice(0, firstMeasureLine);

    // ---------- GET METADATA FROM THE FILE : ----------

    const metadata = {
      minX: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'lon min'),
      maxX: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'lon max'),
      minY: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'lat min'),
      maxY: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'lat max'),
      stepX: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'delta lon'),
      stepY: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'delta lat'),
      nRows: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'nrows'),
      nColumns: (0, _GDFParser.getHeaderAttribute)(rawHeaderData, 'ncols')
    };

    // ---------- BUILD A DATA VIEWER FROM THE TEXT DATA : ----------

    const data = new DataView(new ArrayBuffer(_GTXParser.BYTES_PER_DOUBLE * metadata.nRows * metadata.nColumns));
    let index = 0;
    for (let row of rows.slice(firstMeasureLine, rows.length)) {
      row = row.split(' ').filter(value => value !== '');
      if (!row.length) {
        continue;
      }
      for (const value of row) {
        data.setFloat64(index * _GTXParser.BYTES_PER_DOUBLE, parseFloat(value));
        index++;
      }
    }

    // ---------- CREATE A GeoidGrid FOR THE GIVEN FILE DATA : ----------

    const dataExtent = new _Extent.default(options.in.crs || 'EPSG:4326', metadata.minX + metadata.stepX / 2, metadata.maxX - metadata.stepX / 2, metadata.minY + metadata.stepY / 2, metadata.maxY - metadata.stepY / 2);
    const dataStep = new THREE.Vector2(metadata.stepX, metadata.stepY);
    return Promise.resolve(new _GeoidGrid.default(dataExtent, dataStep, (verticalIndex, horizontalIndex) => data.getFloat64((metadata.nColumns * verticalIndex + horizontalIndex) * _GTXParser.BYTES_PER_DOUBLE)));
  }
};
exports.default = _default;