import { MainApp } from './babylonjs/MainApp';
import { AppConfig } from './babylonjs/AppConfig';
import configurator, { ConfiguratorParams } from './components/conf';
import auth from './components/auth';
import { on, off, once, emit } from './event/bus';
import { EventBridge } from './event/bridge';

declare global {
    interface Window {
        faceSDK?: Record<string, unknown>;
        yiyu?: Record<string, unknown>;
    }
}

type InitParams = {
    container?: string;
    modelUrlList?: string[];
    apiConfig?: ConfiguratorParams;
    onSuccess?: () => void;
    onError?: (error?: unknown) => void;
    env?: {
        hdrPath?: string;
        intensity?: number;
        rotationY?: number;
    };
};

let mainApp: MainApp | null = null;

const kernel = {
    // 事件工具，提供给外部订阅/退订
    on,
    off,
    once,
    emit,
    /** 初始化应用 */
    init: async function (params: InitParams): Promise<void> {
        if (!params) { console.error('params is required'); return; }

        mainApp = new MainApp();
        mainApp.loadAConfig({
            container: params.container || document.querySelector(params.container),
            modelUrlList: params.modelUrlList || [],
            env: params.env
        });

        await mainApp.Awake();
        await mainApp.loadModel();
    },
};

if (!window.faceSDK) {
    window.faceSDK = {};
}
window.faceSDK.kernel = kernel;

export { kernel };
