import { SceneSystem, ActorQuery } from 'dacha';
import { AddActor, RemoveActor, } from 'dacha/events';
import { Technical } from '../../components';
import { persistentStorage } from '../../../persistent-storage';
import { DebugVisualizerAPI } from './api';
import { modules } from './modules';
export class DebugVisualizer extends SceneSystem {
    constructor(options) {
        super();
        this.handleActorAdd = ({ actor }) => {
            this.debugActorMap.forEach((moduleActorMap, module) => {
                if (module.validate(actor)) {
                    moduleActorMap.set(actor, null);
                }
            });
        };
        this.handleActorRemove = ({ actor }) => {
            this.debugActorMap.forEach((moduleActorMap) => {
                var _a;
                (_a = moduleActorMap.get(actor)) === null || _a === void 0 ? void 0 : _a.remove();
                moduleActorMap.delete(actor);
            });
        };
        this.options = options;
        this.mainActor = options.world.data.mainActor;
        this.actorQuery = new ActorQuery({
            scene: options.scene,
            filter: (actor) => !actor.getComponent(Technical) &&
                modules.some((module) => module.validate(actor)),
        });
        this.debugActorMap = new Map();
        modules.forEach((module) => {
            const isModuleEnabled = persistentStorage.get(`canvas.debugVisualizer.layers.${module.id}`, false);
            if (isModuleEnabled) {
                this.debugActorMap.set(module, new Map());
            }
        });
        window.electron.updateMenuState('debugLayers', modules.map((module) => ({
            id: module.id,
            title: module.title,
            enabled: persistentStorage.get(`canvas.debugVisualizer.layers.${module.id}`, false),
        })));
        window.electron.onToggleDebugLayer((id, enabled) => {
            persistentStorage.set(`canvas.debugVisualizer.layers.${id}`, enabled);
            const module = modules.find((entry) => entry.id === id);
            if (!module) {
                return;
            }
            if (enabled) {
                const moduleActorMap = new Map();
                this.actorQuery.getActors().forEach((actor) => {
                    if (module.validate(actor)) {
                        moduleActorMap.set(actor, null);
                    }
                });
                this.debugActorMap.set(module, moduleActorMap);
            }
            else {
                const moduleActorMap = this.debugActorMap.get(module);
                moduleActorMap === null || moduleActorMap === void 0 ? void 0 : moduleActorMap.forEach((debugActor) => {
                    debugActor === null || debugActor === void 0 ? void 0 : debugActor.remove();
                });
                this.debugActorMap.delete(module);
            }
        });
        const api = new DebugVisualizerAPI({
            debugActorMap: this.debugActorMap,
        });
        options.world.systemApi.register(api);
    }
    onSceneEnter() {
        this.debugActorMap.forEach((moduleActorMap, module) => {
            this.actorQuery.getActors().forEach((actor) => {
                if (module.validate(actor)) {
                    moduleActorMap.set(actor, null);
                }
            });
        });
        this.actorQuery.addEventListener(AddActor, this.handleActorAdd);
        this.actorQuery.addEventListener(RemoveActor, this.handleActorRemove);
    }
    onSceneDestroy() {
        this.actorQuery.removeEventListener(AddActor, this.handleActorAdd);
        this.actorQuery.removeEventListener(RemoveActor, this.handleActorRemove);
        this.actorQuery.destroy();
    }
    update() {
        this.debugActorMap.forEach((moduleActorMap, module) => {
            moduleActorMap.forEach((debugActor, actor) => {
                if (debugActor === null) {
                    debugActor = module.build(actor, this.options);
                    this.mainActor.appendChild(debugActor);
                    moduleActorMap.set(actor, debugActor);
                }
                module.update(actor, debugActor, this.options);
            });
        });
    }
}
DebugVisualizer.systemName = 'DebugVisualizer';
