"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.projectPath = projectPath;
exports.loadGameConfig = loadGameConfig;
exports.ensureConfiguredAssetDirectories = ensureConfiguredAssetDirectories;
exports.clientGameConfig = clientGameConfig;
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const PROJECT_ROOT = path_1.default.resolve(__dirname, '../..');
function fallbackConfig(engine) {
    return {
        engine,
        locale: 'en_US',
        paths: {
            mainGameFile: engine === 'ink'
                ? 'data/ink/story.ink.json'
                : engine === 'zork'
                    ? 'data/z-code/zork1.bin'
                    : 'data/worlds/example_world.yml',
            music: 'public/music',
            sfx: 'public/sounds',
            images: 'public/images',
        },
        metadata: {
            title: 'AI Interactive Fiction',
            author: 'Generative AI',
            subtitle: 'An open-world text adventure',
            version: '1.0.0',
            copyright: '',
            language: 'en_US',
        },
    };
}
function projectPath(relativeOrAbsolutePath) {
    return path_1.default.isAbsolute(relativeOrAbsolutePath)
        ? relativeOrAbsolutePath
        : path_1.default.resolve(PROJECT_ROOT, relativeOrAbsolutePath);
}
function loadGameConfig(configPath, engine) {
    const absolutePath = projectPath(configPath);
    if (!(0, fs_1.existsSync)(absolutePath)) {
        console.warn(`[config] Missing ${absolutePath}; using ${engine} defaults.`);
        return fallbackConfig(engine);
    }
    const parsed = JSON.parse((0, fs_1.readFileSync)(absolutePath, 'utf8'));
    const fallback = fallbackConfig(engine);
    return {
        engine: parsed.engine ?? fallback.engine,
        locale: parsed.locale ?? fallback.locale,
        paths: {
            ...fallback.paths,
            ...(parsed.paths ?? {}),
        },
        metadata: {
            ...fallback.metadata,
            ...(parsed.metadata ?? {}),
            language: parsed.metadata?.language ?? parsed.locale ?? fallback.metadata.language,
        },
    };
}
function ensureConfiguredAssetDirectories(config) {
    const directories = [
        config.paths.music,
        config.paths.sfx,
        config.paths.images,
        config.paths.inkSource ? path_1.default.dirname(config.paths.inkSource) : undefined,
        config.paths.inkCompiled ? path_1.default.dirname(config.paths.inkCompiled) : undefined,
        config.paths.mainGameFile ? path_1.default.dirname(config.paths.mainGameFile) : undefined,
        config.paths.promptDir,
    ];
    for (const directory of directories) {
        if (!directory)
            continue;
        const absolutePath = projectPath(directory);
        if (!(0, fs_1.existsSync)(absolutePath)) {
            (0, fs_1.mkdirSync)(absolutePath, { recursive: true });
        }
    }
}
function clientGameConfig(config) {
    return {
        engine: config.engine,
        locale: config.locale,
        metadata: config.metadata,
        assets: {
            music: '/music/',
            sfx: '/sounds/',
            sounds: '/sounds/',
            images: '/images/',
        },
    };
}
//# sourceMappingURL=game-config.js.map