import "reflect-metadata";
import { Container } from "../../container/Container";
import Logger from "../Logger";
import globVars from "../../globVars";

export default function EntryPoint(T: { main: () => void }) {
    if (window.WHTRANS) {
        console.log('退出, 已运行次数' + window.WHTRANS)
    } else {
        window.WHTRANS = window.WHTRANS === undefined ? 1 : window.WHTRANS++;
        const logger = Container.factory(Logger);

        let started = false;
        const starter = () => {
            console.log('starter init...');
            let started = performance.now();
            try {
                T.main();
            } catch (e) {
                logger.error('[Starter]加载出错信息: ' + e.stack || e.message);
            }
            let runTime: number = (performance.now() - started) | 0;
            globVars.loadTime = runTime;
            logger.info(`芜湖脚本完成加载, 耗时${ runTime }ms`);
            // if (ZhongIcon.ZhongNode && ZhongIcon.ZhongNode.initTimer)
            //     ZhongIcon.ZhongNode.initTimer.innerHTML = `加载时间 ${ runTime }ms`;
        };
        const evHandler = () => {
            // console.log('document.readyState: ' + document.readyState);
            if (!started && (document.readyState === 'complete' || document.readyState === 'interactive')) {
                document.removeEventListener('readystatechange', evHandler);
                started = !started;
                starter();
            } else if (!(document.readyState === 'complete' || document.readyState === 'interactive')) {
                document.addEventListener('readystatechange', evHandler);
            }
        };
        evHandler();
    }
}
