import { asWriteable } from '../Types';
import { Permissions } from './Permission';
/**
 * A Layer definition. Describes a layer and its permissions. To create a layer
 * the corresponding layer definition must exist.
 */
export class LayerDefinition extends Permissions {
    id;
    /** Used to represent a layer definition that does not exist. */
    static notFound = Object.freeze(new LayerDefinition('not-found'));
    constructor(id, ...permissions) {
        super(Permissions.join(permissions));
        this.id = id;
    }
    /** `true` if the layer definition exists, `false` otherwise. */
    get exists() {
        return this !== LayerDefinition.notFound;
    }
}
Object.freeze(LayerDefinition); // Ensures nobody can modify LayerDefinition.notFound;
/** Represents a collection of {@link LayerDefinition} instances. */
export class LayerDefinitions {
    _definitions = new Map();
    /**
     * Constructor.
     * @param definitions Definitions the collection shall consist of.
     */
    constructor(definitions = []) {
        for (const d of definitions) {
            this._definitions.set(d.id, d);
        }
    }
    /**
     * Creates a {@link LayerDefinitions} instance for a `Record<string, { permission_str: string }>` lookup where
     * the properties are the layer ids. This is how a {@link LayerDefinitions} instance is represented in JSON.
     * @param src Source to copy from.
     * @returns change with converted property.
     */
    static fromDTO(src) {
        return new LayerDefinitions(Object.entries(src ?? {}).map(([id, def]) => new LayerDefinition(id, BigInt(def.permissionsString))));
    }
    /**
     * Copies the layer definitions defined by `layerDefinitions` to the `dst`.availableLayerDefinitions property.
     * @param layerDefinitions Layer definitions to copy to `dst`.availableLayerDefinitions.
     * @param dst Object to assign property on.
     * @returns Reference to `dst`.
     */
    static copyToRef(layerDefinitions, dst) {
        const defs = layerDefinitions instanceof LayerDefinitions
            ? new LayerDefinitions([...layerDefinitions])
            : LayerDefinitions.fromDTO(layerDefinitions.json);
        asWriteable(dst).availableLayerDefinitions = defs;
        return dst;
    }
    /**
     * Gets the {@link LayerDefinition} instance with the same id. If no such instance is found then `{@link LayerDefinition.notFound} is returned.
     * @param id Id of layer definition to get.
     * @returns {@link Layer} instance.
     */
    get(id) {
        return this._definitions.get(id) ?? LayerDefinition.notFound;
    }
    /**
     * Iterates all contained {@link LayerDefinition}. Use this to
     * get hold of all {@link LayerDefinition} instances the collection consists of.
     */
    *[Symbol.iterator]() {
        for (const def of this._definitions.values()) {
            yield def;
        }
    }
}
//# sourceMappingURL=LayerDefinitions.js.map