import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, useEffect, useLayoutEffect, useRef } from 'react';
import StartGame from './main';
import { EventBus } from './EventBus';
export const PhaserGame = forwardRef(function PhaserGame({ currentActiveScene }, ref) {
    const game = useRef(null);
    useLayoutEffect(() => {
        if (game.current === null) {
            game.current = StartGame("cog-shooter-game-container");
            if (typeof ref === 'function') {
                ref({ game: game.current, scene: null });
            }
            else if (ref) {
                ref.current = { game: game.current, scene: null };
            }
        }
        return () => {
            if (game.current) {
                game.current.destroy(true);
                if (game.current !== null) {
                    game.current = null;
                }
            }
        };
    }, [ref]);
    useEffect(() => {
        EventBus.on('scene-ready', (scene_instance) => {
            if (currentActiveScene && typeof currentActiveScene === 'function') {
                currentActiveScene(scene_instance);
            }
            if (typeof ref === 'function') {
                ref({ game: game.current, scene: scene_instance });
            }
            else if (ref) {
                ref.current = { game: game.current, scene: scene_instance };
            }
        });
        return () => {
            EventBus.removeListener('scene-ready');
        };
    }, [currentActiveScene, ref]);
    return (_jsx("div", { id: "cog-shooter-game-container" }));
});
