import { useEffect, useContext, useRef } from 'react';
import { EventType } from '../../../events';
import { useConfig } from '../use-config';
import { useSaveProject } from '../use-save-project';
import { EngineContext } from '../../providers';
import { persistentStorage } from '../../../persistent-storage';
// Listens for project changes and notifies main process if there are any unsaved changes or not
export const useUnsavedChanges = () => {
    const context = useContext(EngineContext);
    const projectConfig = useConfig([]);
    const { save } = useSaveProject();
    const didMountRef = useRef(false);
    const unsavedChangesRef = useRef(false);
    useEffect(() => {
        if (projectConfig === undefined) {
            return;
        }
        if (!didMountRef.current) {
            didMountRef.current = true;
            return;
        }
        if (!unsavedChangesRef.current) {
            window.electron.setUnsavedChanges(true);
            unsavedChangesRef.current = true;
        }
    }, [projectConfig]);
    useEffect(() => {
        const handleUnload = () => {
            persistentStorage.saveImmediately();
            window.electron.setUnsavedChanges(false);
        };
        window.addEventListener('beforeunload', handleUnload);
        return () => window.removeEventListener('beforeunload', handleUnload);
    }, []);
    useEffect(() => {
        const handleBlur = () => {
            persistentStorage.saveImmediately();
            save();
        };
        window.addEventListener('blur', handleBlur);
        return () => window.removeEventListener('blur', handleBlur);
    }, []);
    useEffect(() => {
        if (!context) {
            return () => { };
        }
        const { world } = context;
        const handleGameStateUpdate = () => {
            if (unsavedChangesRef.current) {
                window.electron.setUnsavedChanges(false);
                unsavedChangesRef.current = false;
            }
        };
        world.addEventListener(EventType.SaveProject, handleGameStateUpdate);
        return () => world.removeEventListener(EventType.SaveProject, handleGameStateUpdate);
    }, [context]);
};
