import fscreen from 'fscreen';
import { isMobile } from '@pixi/core';

/** Requests fullscreen mode on user interaction for mobile devices. */
export default class FullScreenController
{
    private readonly handleClick = (): void =>
    {
        void this.enterFullscreen();
    };

    constructor()
    {
        window.addEventListener('click', this.handleClick);
    }

    /** Tries to enter fullscreen after a user gesture. */
    async enterFullscreen(): Promise<void>
    {
        if (!isMobile.any)
        {
            return;
        }

        if (fscreen.fullscreenElement !== null)
        {
            return;
        }

        try
        {
            const body = document.body;

            if (!body)
            {
                return;
            }

            await fscreen.requestFullscreen(body);
        }
        catch (error)
        {
            console.warn('Fullscreen request failed:', error);
        }
    }

    /** Detaches listeners so the controller can be disposed. */
    destroy(): void
    {
        window.removeEventListener('click', this.handleClick);
    }
}
