import Check from "../Core/Check.js";
import clone from "../Core/clone.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import MetadataEntity from "./MetadataEntity.js";

/**
 * A table for storing free-form JSON metadata, as in the 3D Tiles batch table.
 *
 * @param {Object} options Object with the following properties:
 * @param {Number} options.count The number of entities in the table.
 * @param {Object.<String, Array>} options.properties The JSON representation of the metadata table. All the arrays must have exactly options.count elements. The value of array
 * @param {MetadataClass} [options.class] The class that group metadata conforms to.
 * @alias JsonMetadataTable
 * @constructor
 */
export default function JsonMetadataTable(options) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number.greaterThan("options.count", options.count, 0);
  Check.typeOf.object("options.properties", options.properties);
  //>>includeEnd('debug');

  this._count = options.count;
  this._properties = clone(options.properties, true);
}

/**
 * Returns whether this property exists.
 *
 * @param {String} propertyId The case-sensitive ID of the property.
 * @returns {Boolean} Whether this property exists.
 */
JsonMetadataTable.prototype.hasProperty = function (propertyId) {
  return MetadataEntity.hasProperty(propertyId, this._properties);
};

/**
 * Returns an array of property IDs.
 *
 * @param {String[]} [results] An array into which to store the results.
 * @returns {String[]} The property IDs.
 */
JsonMetadataTable.prototype.getPropertyIds = function (results) {
  return MetadataEntity.getPropertyIds(this._properties, undefined, results);
};

/**
 * Returns a copy of the value of the property with the given ID.
 * <p>
 * If the property is normalized the normalized value is returned.
 * </p>
 *
 * @param {Number} index The index of the entity.
 * @param {String} propertyId The case-sensitive ID of the property.
 * @returns {*} The value of the property or <code>undefined</code> if the property does not exist.
 */
JsonMetadataTable.prototype.getProperty = function (index, propertyId) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("index", index);
  Check.typeOf.string("propertyId", propertyId);

  if (index < 0 || this._count < index) {
    throw new DeveloperError("index must be in [0, " + this._count + ")");
  }
  //>>includeEnd('debug');

  var property = this._properties[propertyId];
  if (defined(property)) {
    return clone(property[index], true);
  }

  return undefined;
};

/**
 * Sets the value of the property with the given ID.
 *
 * @param {Number} index The index of the entity.
 * @param {String} propertyId The case-sensitive ID of the property.
 * @param {*} value The value of the property that will be copied.
 * @exception {DeveloperError} A property with the given ID doesn't exist.
 */
JsonMetadataTable.prototype.setProperty = function (index, propertyId, value) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("index", index);
  Check.typeOf.string("propertyId", propertyId);

  if (!defined(this._properties[propertyId])) {
    throw new DeveloperError("propertyId " + propertyId + " does not exist");
  }

  if (index < 0 || this._count < index) {
    throw new DeveloperError("index must be in [0, " + this._count + ")");
  }
  //>>includeEnd('debug');

  var property = this._properties[propertyId];
  if (defined(property)) {
    property[index] = clone(value, true);
  }
};

@@ -601,28 +601,9 @@
 nts.
- The value of array
 %0A
+
  * @
@@ -727,16 +727,28 @@
 tructor%0A
+ * @private%0A
  */%0Aexpo
@@ -1260,16 +1260,28 @@
 exists.%0A
+ * @private%0A
  */%0AJson
@@ -1571,16 +1571,28 @@
 ty IDs.%0A
+ * @private%0A
  */%0AJson
@@ -1809,90 +1809,8 @@
 ID.%0A
- * %3Cp%3E%0A * If the property is normalized the normalized value is returned.%0A * %3C/p%3E%0A
  *%0A 
@@ -2019,32 +2019,98 @@
 does not exist.%0A
+ * @exception %7BDeveloperError%7D index is out of bounds%0A * @private%0A
  */%0AJsonMetadata
@@ -2842,16 +2842,16 @@
 copied.%0A
-
  * @exce
@@ -2917,16 +2917,82 @@
  exist.%0A
+ * @exception %7BDeveloperError%7D index is out of bounds%0A * @private%0A
  */%0AJson

Update documentation