/**
 * 提取预加载模块（WASM 等）
 */
function extractPreloadModules(projectConfig) {
    const modules = [];
    const assets = projectConfig.format === 'playcanvas'
        ? projectConfig.assets
        : projectConfig.assets;
    if (!assets) {
        return modules;
    }
    for (const asset of Object.values(assets)) {
        const assetData = asset;
        if (assetData.type !== 'wasm') {
            continue;
        }
        const wasmUrl = assetData.file?.url;
        const moduleName = assetData.data?.moduleName;
        const glueId = assetData.data?.glueScriptId;
        const fallbackId = assetData.data?.fallbackScriptId;
        if (!wasmUrl || !moduleName || !glueId) {
            continue;
        }
        const glueAsset = assets[String(glueId)];
        const fallbackAsset = fallbackId ? assets[String(fallbackId)] : null;
        const glueUrl = glueAsset?.file?.url;
        const fallbackUrl = fallbackAsset?.file?.url;
        if (!glueUrl) {
            continue;
        }
        modules.push({
            moduleName,
            glueUrl,
            wasmUrl,
            fallbackUrl,
            preload: true,
        });
    }
    return modules;
}
/**
 * 生成 __settings__.js 文件内容
 */
export function generateSettings(projectConfig, scenePath) {
    let settings;
    let scripts = [];
    if (projectConfig.format === 'playcanvas') {
        const pcProject = projectConfig;
        settings = pcProject.project.settings || {};
        scripts = settings.scripts || [];
    }
    else {
        const pcProject = projectConfig;
        settings = pcProject.manifest.settings || {};
        scripts = settings.scripts || [];
    }
    // 确定场景路径
    let finalScenePath = scenePath || '';
    if (!finalScenePath && projectConfig.format === 'playcanvas') {
        const pcProject = projectConfig;
        if (pcProject.scenes) {
            const scenes = Array.isArray(pcProject.scenes)
                ? pcProject.scenes
                : Object.values(pcProject.scenes);
            if (scenes.length > 0) {
                const firstScene = scenes[0];
                finalScenePath = firstScene.url || firstScene.file || `${firstScene.id || 'scene'}.json`;
            }
        }
    }
    else if (!finalScenePath) {
        const pcProject = projectConfig;
        if (pcProject.scenes && pcProject.scenes.length > 0) {
            const firstScene = pcProject.scenes[0];
            finalScenePath = firstScene.url || `${firstScene.id || 'scene'}.json`;
        }
    }
    const preloadModules = extractPreloadModules(projectConfig);
    return `window.ASSET_PREFIX = "";
window.SCRIPT_PREFIX = "";
window.SCENE_PATH = "${finalScenePath}";
window.CONTEXT_OPTIONS = {
  antialias: ${settings.antiAlias ?? true},
  alpha: false,
  preserveDrawingBuffer: ${settings.preserveDrawingBuffer ?? false},
  deviceTypes: ['webgl2', 'webgl1'],
  powerPreference: "${settings.powerPreference || 'default'}"
};
window.SCRIPTS = ${JSON.stringify(scripts)};
window.CONFIG_FILENAME = "config.json";
window.INPUT_SETTINGS = {
  useKeyboard: ${settings.useKeyboard ?? true},
  useMouse: ${settings.useMouse ?? true},
  useGamepads: ${settings.useGamepads ?? false},
  useTouch: ${settings.useTouch ?? true}
};
pc.script.legacy = ${settings.useLegacyScripts ?? false};
window.PRELOAD_MODULES = ${JSON.stringify(preloadModules)};
`;
}
