import { TileLayer } from "./tilelayer";
import { TileSet } from "./tileset";

/**
 * A tilemap object with name, sizes and layers.
 */
export class TileMap {
	/**
     * TileMap constructor.
     *
     * @param {object} options - Options for the tilemap. Can also be a tilemap you loaded from JSON or similar.
	 * @param {string} [options.name="Tilemap"] - {@link TileMap#name}
     * @param {number} options.width - {@link TileMap#width}
     * @param {number} options.height - {@link TileMap#height}
     * @param {number} options.tileWidth - {@link TileMap#tileWidth}
     * @param {number} options.tileHeight - {@link TileMap#tileHeight}
     * @param {layerOption[]} [options.layers=[]] - Same structure as options in {@link TileLayer#constructor}
     * @param {tilesetOption[]} [options.tilesets=[]] - Same structure as options in {@link TileSet#constructor}
	 * @param {Map<string, *>} [options.properties=Map] - {@link TileMap#properties}
     */
	constructor(options) {
		/** The name of the tilemap.
		 * @type {string} */
		this.name = options.name || "Tilemap";

		/** The width of the tilemap.
		 * @type {number} */
		this.width = options.width;
		/** The height of the tilemap.
		 * @type {number} */
		this.height = options.height;

		/** The width of a tile for the tilemap.
		 * @type {number} */
		this.tileWidth = options.tileWidth;
		/** The height of a tile for the tilemap.
		 * @type {number} */
		this.tileHeight = options.tileHeight;

		/** All layers of the tilemap
		 *  @type {TileLayer[]} */
		this.layers = [];
		options.layers = options.layers || [];
		for (let layerData of options.layers) {
			this.createLayer(layerData);
		}

		/** The tilesets the tilemap consists of.
		 * @type {TileSet[]} */
		this.tilesets = [];
		options.tilesets = options.tilesets || [];
		for (let tileset of options.tilesets) {
			this.addTileset(new TileSet(tileset));
		}

		/** Custom properties of the tilemap.
		 * @type {Map<string, *>} */
		this.properties = options.properties || new Map();
	}

	/**
	 * Creates a new layer and adds it to the tilemap.
	 *
	 * @param {object} options - Options for the layer (see {@link TileLayer}).
	 *
	 * @returns {TileLayer}
	 */
	createLayer(options) {
		const layer = new TileLayer(this, options);
		this.layers.push(layer);
		return layer;
	}

	/**
	 * Adds a tileset to the tilemap.
	 *
	 * @param {TileSet} tileset - the tileset to add to the map.
	 *
	 * @returns {TileSet}
	 */
	addTileset(tileset) {
		this.tilesets.push(tileset);
		return tileset;
	}

	/**
	 * Converts this tilemap to a JSON string.
	 *
	 * @returns {string} - The JSON serialization of this tilemap.
	 */
	getJSON() {
		return JSON.stringify(this, (key, value) => {
			if (key.indexOf("_") === 0) {
				return undefined;
			}
			return value;
		});
	}
}

@@ -1075,70 +1075,16 @@
 %22;%0A%0A
-%09%09/** The width of the tilemap.%0A%09%09 * @type %7Bnumber%7D */%0A
 %09%09this.
+_
 widt
@@ -1106,71 +1106,16 @@
 th;%0A
-%09%09/** The height of the tilemap.%0A%09%09 * @type %7Bnumber%7D */%0A
 %09%09this.
+_
 heig
@@ -1899,24 +1899,214 @@
  Map();%0A%09%7D%0A%0A
+%09/** The width of the tilemap.%0A%09 * @type %7Bnumber%7D */%0A%09get width() %7B%0A%09%09return this._width;%0A%09%7D%0A%09/** The height of the tilemap.%0A%09 * @type %7Bnumber%7D */%0A%09get height() %7B%0A%09%09return this._height;%0A%09%7D%0A%0A
 %09/**%0A%09 * Cre

Make map dimmensions read-only