"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const parser_1 = require("../parser/parser");
const util_1 = require("../util");
const BaseForLines_1 = require("./BaseForLines");
let collisionIdCounter = 0;
class CollisionLayer extends BaseForLines_1.BaseForLines {
    constructor(source, tiles, addValidationMessage) {
        super(source);
        this.id = collisionIdCounter++;
        // Map all the Objects to the layer
        tiles.forEach((tile) => {
            if (tile.hasCollisionLayer()) {
                addValidationMessage(new parser_1.ValidationMessage(tile.__source, parser_1.ValidationLevel.WARNING, 'An Object should not belong to more than one collision layer'));
            }
            tile.setCollisionLayer(this);
            tile._getDescendantTiles().forEach((subTile) => {
                if (subTile.hasCollisionLayer()) {
                    addValidationMessage(new parser_1.ValidationMessage(subTile.__source, parser_1.ValidationLevel.WARNING, 'An Object should not belong to more than one collision layer. This item was referenced indirectly by a LEGEND entry'));
                }
                subTile.setCollisionLayer(this);
            });
        });
        // build an array of Sprites so we can index to them in a BitSet
        this.sprites = [...new Set(util_1._flatten(tiles.map((t) => t.getSprites())))];
        this.sprites.forEach((sprite, index) => sprite.setCollisionLayerAndIndex(this, index));
    }
    // isInvalid(): Optional<string> {
    //     return null
    // }
    getBitSetIndexOf(sprite) {
        const index = this.sprites.indexOf(sprite);
        if (index < 0) {
            throw new Error(`BUG: Sprite is not in this CollisionLayer`);
        }
        return index;
    }
    toKey() {
        return this.sprites.map((s) => s.getName()).join(' ');
    }
}
exports.CollisionLayer = CollisionLayer;
//# sourceMappingURL=collisionLayer.js.map