import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, 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(() => {
        let readyScene = null;
        const assignRef = (scene) => {
            if (typeof ref === 'function') {
                ref({ game: game.current, scene });
            }
            else if (ref) {
                ref.current = { game: game.current, scene };
            }
        };
        const setCurrentScene = (scene_instance) => {
            readyScene = scene_instance;
            if (currentActiveScene && typeof currentActiveScene === 'function') {
                currentActiveScene(scene_instance);
            }
            assignRef(scene_instance);
        };
        EventBus.on('current-scene-ready', setCurrentScene);
        if (game.current === null) {
            game.current = StartGame(`skiing-game-tutorial-container`);
            assignRef(readyScene);
        }
        return () => {
            EventBus.removeListener('current-scene-ready', setCurrentScene);
            if (game.current) {
                game.current.destroy(true);
                if (game.current !== null) {
                    game.current = null;
                }
            }
        };
    }, [currentActiveScene, ref]);
    return (_jsx("div", { id: `skiing-game-tutorial-container` }));
});
