// if ((module as any).hot) {
//     (module as any).hot.accept();
// }

import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { AssignAllExtensions } from '../../extensions/General/Extensions';
import { DocServer } from '../DocServer';
import { Docs } from '../documents/Documents';
import { CurrentUserUtils } from '../util/CurrentUserUtils';
import { LinkManager } from '../util/LinkManager'; // this must come before importing Docs and CurrentUserUtils
import { ReplayMovements } from '../util/ReplayMovements';
import { TrackMovements } from '../util/TrackMovements';
import { CollectionView } from './collections/CollectionView';
import { MainView } from './MainView';
import * as dotenv from 'dotenv'; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import { PingManager } from '../util/PingManager';
dotenv.config();

AssignAllExtensions();

(async () => {
    MainView.Live = window.location.search.includes('live');
    window.location.search.includes('safe') && CollectionView.SetSafeMode(true);
    const info = await CurrentUserUtils.loadCurrentUser();
    if (info.email === 'guest') DocServer.Control.makeReadOnly();
    await CurrentUserUtils.loadUserDocument(info.id);
    setTimeout(() => {
        document.getElementById('root')!.addEventListener(
            'wheel',
            event => {
                if (event.ctrlKey) {
                    event.preventDefault();
                }
            },
            true
        );
        const startload = (document as any).startLoad;
        const loading = Date.now() - (startload ? Number(startload) : Date.now() - 3000);
        console.log('Loading Time = ' + loading);
        const d = new Date();
        d.setTime(d.getTime() + 100 * 24 * 60 * 60 * 1000);
        const expires = 'expires=' + d.toUTCString();
        document.cookie = `loadtime=${loading};${expires};path=/`;
        new LinkManager();
        new TrackMovements();
        new ReplayMovements();
        new PingManager();
        ReactDOM.createRoot(document.getElementById('root')!).render(<MainView />);
    }, 0);
})();
