import fs from 'fs/promises';
import path from 'path';
/**
 * 加载完整的场景文件（包含 entities 字段）
 * @param projectDir 项目目录
 * @param sceneFileId 场景文件 ID（用于文件名）
 * @returns 完整的场景数据
 */
export async function loadFullSceneData(projectDir, sceneFileId) {
    const scenePath = path.join(projectDir, `${sceneFileId}.json`);
    try {
        const content = await fs.readFile(scenePath, 'utf-8');
        return JSON.parse(content);
    }
    catch (error) {
        console.warn(`[PlayCanvasLoader] 无法加载场景文件 ${scenePath}:`, error);
        return null;
    }
}
/**
 * 加载 PlayCanvas 源代码项目
 */
export async function loadPlayCanvasProject(projectDir) {
    const [projectJson, scenesJson, assetsJson] = await Promise.all([
        fs.readFile(path.join(projectDir, 'project.json'), 'utf-8'),
        fs.readFile(path.join(projectDir, 'scenes.json'), 'utf-8'),
        fs.readFile(path.join(projectDir, 'assets.json'), 'utf-8'),
    ]);
    const project = JSON.parse(projectJson);
    const scenes = JSON.parse(scenesJson);
    const assets = JSON.parse(assetsJson);
    // 检查是否有 Import Map 配置
    let importMap;
    const importMapId = project.settings?.importMap;
    if (importMapId) {
        try {
            const importMapAsset = assets[String(importMapId)];
            if (importMapAsset && importMapAsset.file) {
                // 尝试获取文件URL，如果不存在则构建可能的路径
                let importMapPath = null;
                if (importMapAsset.file.url) {
                    importMapPath = path.join(projectDir, importMapAsset.file.url);
                }
                else if (importMapAsset.file.filename) {
                    // 尝试多个可能的路径
                    const filename = importMapAsset.file.filename;
                    const revision = importMapAsset.revision ?? 1;
                    const candidates = [
                        path.join(projectDir, 'files', 'assets', String(importMapId), String(revision), filename),
                        path.join(projectDir, 'files', 'assets', String(importMapId), '1', filename),
                        path.join(projectDir, 'files', 'assets', String(importMapId), filename),
                    ];
                    for (const candidate of candidates) {
                        try {
                            await fs.access(candidate);
                            importMapPath = candidate;
                            break;
                        }
                        catch {
                            // 继续尝试下一个
                        }
                    }
                }
                if (importMapPath) {
                    const importMapContent = await fs.readFile(importMapPath, 'utf-8');
                    importMap = {
                        id: String(importMapId),
                        content: JSON.parse(importMapContent),
                    };
                    console.log('[PlayCanvasLoader] Import Map 已加载:', importMap.id);
                }
            }
        }
        catch (error) {
            console.warn('[PlayCanvasLoader] 无法加载 Import Map:', error);
        }
    }
    return {
        project,
        scenes,
        assets,
        format: 'playcanvas',
        importMap,
    };
}
