"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 _C3DTilesLayer = require("../Layer/C3DTilesLayer");

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

/** @classdesc
 * Class for storing and accessing information relative to the
 *  [3DTILES_batch_table_hierarchy extension]{@link https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/extensions/3DTILES_batch_table_hierarchy}
 *  of 3D Tiles */
var BatchTableHierarchyExtension = /*#__PURE__*/function (_$3dTilesAbstractExte) {
  (0, _inherits2["default"])(BatchTableHierarchyExtension, _$3dTilesAbstractExte);

  var _super = _createSuper(BatchTableHierarchyExtension);

  /**
   * @param {Object} json - json holding the extension
   */
  function BatchTableHierarchyExtension(json) {
    var _this;

    (0, _classCallCheck2["default"])(this, BatchTableHierarchyExtension);
    _this = _super.call(this, _C3DTilesLayer.$3dTilesAbstractExtension);
    _this.classes = json.classes; // inverseHierarchy contains for each instance (i.e. georgraphic
    // feature e.g. building, roof, etc.) an array of the indexes of its
    // parents. For example, the parents of the instance 0 can be found
    // using inverseHierarchy[0].

    _this.inverseHierarchy = {}; // instancesIdxs contains for each instance of the extension, a
    // javascript object with classId and instanceIdx. classId is the id of
    // the class (from this.classes) of the instance. instanceIdx is the
    // index of the instance in this class. Goal: Ease the retrieval
    // of the properties of an instance

    _this.instancesIdxs = []; // Counts the number of instances of a class

    var classCounter = {};
    var parentIdsCounter = 0; // if omitted, parentCounts is an array of length instancesLength,
    // where all values are 1 (cf. spec)

    var parentCounts = json.parentCounts;

    if (parentCounts === undefined) {
      parentCounts = new Array(json.instancesLength);
      parentCounts.fill(1);
    } // for each instance


    for (var i = 0; i < json.instancesLength; i++) {
      // for each parent of the current instance
      for (var j = 0; j < parentCounts[i]; j++) {
        // When an instance's parentId points to itself, then it has no
        // parent" (cf. spec)
        if (i !== json.parentIds[parentIdsCounter]) {
          if (_this.inverseHierarchy[i] === undefined) {
            _this.inverseHierarchy[i] = [];
          }

          _this.inverseHierarchy[i].push(json.parentIds[parentIdsCounter]);

          parentIdsCounter++;
        }
      }

      var classId = json.classIds[i];

      if (classCounter[classId] === undefined) {
        classCounter[classId] = 0;
      }

      _this.instancesIdxs[i] = {
        classId: classId,
        instanceIdx: classCounter[classId]
      };
      classCounter[classId]++;
    }

    return _this;
  }
  /**
   * Creates and returns a javascript object holding the displayable
   * information relative to this extension and for a given feature and
   * its parents
   * @param {integer} featureId - id of the feature
   * @returns {Object} - displayable information relative to this extension
   * and for the feature with id=featureId and for its parents
   */


  (0, _createClass2["default"])(BatchTableHierarchyExtension, [{
    key: "getPickingInfo",
    value: function getPickingInfo(featureId) {
      var _this2 = this;

      var instanceProperties = {}; // get feature class name

      var instanceClassId = this.instancesIdxs[featureId].classId;
      var featureClass = this.classes[instanceClassId].name; // get feature properties and values

      var instanceIdx = this.instancesIdxs[featureId].instanceIdx;
      Object.keys(this.classes[instanceClassId].instances).forEach(function (property) {
        instanceProperties[property] = _this2.classes[instanceClassId].instances[property][instanceIdx];
      }); // create return object: className: {featureProperties and values}

      var pickingInfo = {};
      pickingInfo[featureClass] = instanceProperties; // If this feature has parent(s), recurse on them

      if (this.inverseHierarchy && this.inverseHierarchy[featureId]) {
        this.inverseHierarchy[featureId].forEach(function (parentId) {
          return Object.assign(pickingInfo, _this2.getPickingInfo(parentId));
        });
      }

      return pickingInfo;
    }
  }]);
  return BatchTableHierarchyExtension;
}(_C3DTilesLayer.$3dTilesAbstractExtension);
/**
 * @module BatchTableHierarchyExtensionParser
 */


var _default = {
  /**
   * Parses a
   * [3DTILES_batch_table_hierarchy extension]{@link https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/extensions/3DTILES_batch_table_hierarchy}
   * and returns a Promise that resolves with a BatchTableHierarchyExtension
   * object.
   * @param {Object} json - json holding the extension
   * @return {Promise} - a promise that resolves with a
   *     BatchTableHierarchyExtension object.
   */
  parse: function parse(json) {
    return Promise.resolve(new BatchTableHierarchyExtension(json));
  }
};
exports["default"] = _default;