/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
export class PartsManager {
    constructor(output) {
        this._parts = new Set();
        this._registeredParts = new Map();
        // If it is managed, the loading from an export will be done by the editor
        this.managed = false;
        this.output = output;
    }
    getRegisteredParts() {
        return this._registeredParts;
    }
    getParts() {
        return this._parts;
    }
    onInject() {
        this.output.dom.root.style.position = 'relative';
    }
    registerPart(partClass) {
        this._registeredParts.set(partClass.type, partClass);
    }
    addPart(partClass, data) {
        const part = new partClass();
        if (data) {
            part.load(data);
        }
        part.onInstall({
            visuals: this.output.visuals,
            audio: this.output.audio,
            dom: this.output.dom,
            resources: this.output.resources,
        });
        this._parts.add(part);
        return part;
    }
    removePart(part) {
        part.dispose();
        this._parts.delete(part);
    }
    save() {
        const state = [];
        this._parts.forEach((part) => {
            state.push(part.serialize());
        });
        return state;
    }
    load(partsData) {
        // The loading is handled by the editor when managed
        if (this.managed) {
            return;
        }
        this.reset();
        partsData.forEach((data) => {
            const partClass = this._registeredParts.get(data.type);
            if (!partClass) {
                console.warn(`Could not load part '${data.type}': This part was not registered`);
                return;
            }
            this.addPart(partClass, data);
        });
    }
    reset() {
        this._parts.forEach(p => p.dispose());
        this._parts.clear();
    }
}
//# sourceMappingURL=manager.js.map