(function () {
    const screens = {};
    let activeScreen = null;

    window.ScreenManager = {
        register(name, screen) {
            screens[name] = screen;
        },

        has(name) {
            return !!screens[name];
        },

        async activate(name, html) {
            const root = document.getElementById("screen-root");
            if (!root) {
                console.error("screen-root not found");
                return;
            }

            // cleanup previous screen
            if (activeScreen && typeof activeScreen.dispose === "function") {
                activeScreen.dispose();
            }

            // inject HTML
            root.innerHTML = html;

            // init new screen
            const screen = screens[name];
            if (screen && typeof screen.init === "function") {
                screen.init();
                activeScreen = screen;
            } else {
                console.warn(`No screen registered for ${name}`);
                activeScreen = null;
            }
        }
    };
})();
