"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VersionHandler = void 0;
/**
 * @author NovaStepStudios
 * @alias StepnickaSantiago
 * @license Apache-2.0
 * @link https://www.apache.org/licenses/LICENSE-2.0
 */
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class VersionHandler {
    root;
    versionsRoot;
    constructor(minecraftRoot) {
        this.root = minecraftRoot;
        this.versionsRoot = path_1.default.join(this.root, "versions");
    }
    loadVersion(versionId) {
        const versionPath = path_1.default.join(this.versionsRoot, versionId, `${versionId}.json`);
        if (!fs_1.default.existsSync(versionPath)) {
            throw new Error(`[VersionHandler] No existe JSON para la versión: ${versionId}`);
        }
        let versionData = JSON.parse(fs_1.default.readFileSync(versionPath, "utf-8"));
        if (versionData.inheritsFrom) {
            const parent = this.loadVersion(versionData.inheritsFrom);
            versionData = this.mergeVersions(parent, versionData);
        }
        versionData.arguments = this.normalizeArguments(versionData);
        return versionData;
    }
    getLaunchData(versionId, gameDir, assetsDir) {
        const version = this.loadVersion(versionId);
        const rootDir = this.root;
        const gameDirectory = gameDir ?? path_1.default.join(rootDir, versionId);
        const assetsDirectory = assetsDir ?? path_1.default.join(rootDir, "assets");
        const replacedArgs = {
            game: version.arguments?.game.map(arg => {
                if (typeof arg === "string") {
                    return arg
                        .replace(/\$\{game_directory\}/g, gameDirectory)
                        .replace(/\$\{assets_root\}/g, assetsDirectory)
                        .replace(/\$\{assets_index_name\}/g, version.assetIndex?.id ?? "")
                        .replace(/\$\{version_name\}/g, version.id);
                }
                return arg;
            }) ?? [],
            jvm: version.arguments?.jvm ?? []
        };
        return {
            mainClass: version.mainClass ?? "",
            javaVersion: version.javaVersion,
            libraries: version.libraries ?? [],
            arguments: replacedArgs,
            assetIndex: version.assetIndex,
            gameDir: gameDirectory,
            assetsDir: assetsDirectory,
            rootDir
        };
    }
    mergeVersions(base, override) {
        const merged = {
            ...base,
            ...override,
            libraries: [...(base.libraries ?? []), ...(override.libraries ?? [])],
            minecraftArguments: override.minecraftArguments ?? base.minecraftArguments,
        };
        merged.arguments = this.mergeArguments(base.arguments, override.arguments);
        return merged;
    }
    mergeArguments(base, override) {
        return {
            game: [...(base?.game ?? []), ...(override?.game ?? [])],
            jvm: [...(base?.jvm ?? []), ...(override?.jvm ?? [])]
        };
    }
    normalizeArguments(version) {
        if (version.arguments) {
            return {
                game: [...(version.arguments.game ?? [])],
                jvm: [...(version.arguments.jvm ?? [])]
            };
        }
        if (version.minecraftArguments) {
            return { game: version.minecraftArguments.split(" "), jvm: [] };
        }
        return { game: [], jvm: [] };
    }
}
exports.VersionHandler = VersionHandler;
