/**
 * @license MIT
 * @copyright Copyright (c) 2026, GoldFrite
 */
import fs from 'node:fs/promises';
import { existsSync } from 'node:fs';
import path_ from 'node:path';
import utils from '../../utils/utils.js';
import EventEmitter from '../../utils/events.js';
import { EMLLibError, ErrorType } from '../../../types/errors.js';
export default class FabricLikeLoader extends EventEmitter {
    constructor(config, manifest, loader) {
        super();
        this.config = config;
        this.manifest = manifest;
        this.loader = loader;
        if (this.loader.type === 'QUILT') {
            this.metaConfig = { name: 'Quilt', url: 'https://meta.quiltmc.org', apiVersion: 'v3' };
        }
        else {
            this.metaConfig = { name: 'Fabric', url: 'https://meta.fabricmc.net', apiVersion: 'v2' };
        }
    }
    /**
     * Setup Fabric or Quilt loader.
     * @returns `loaderManifest`: Loader manifest; `installProfile`: null (Fabric n'en a pas);
     * `libraries`: libraries to download; `files`: all files created by this method or that will be
     * created (including `libraries`)
     */
    async setup() {
        const loaderId = this.loader.type.toLowerCase();
        const versionPath = path_.join(this.config.root, 'versions', `${loaderId}-${this.loader.loaderVersion}`);
        const jsonName = `${this.loader.minecraftVersion}-${loaderId}-${this.loader.loaderVersion}.json`;
        const jsonPath = path_.join(versionPath, jsonName);
        if (!existsSync(versionPath)) {
            await fs.mkdir(versionPath, { recursive: true });
        }
        const url = `${this.metaConfig.url}/${this.metaConfig.apiVersion}/versions/loader/${this.manifest.id}/${this.loader.loaderVersion}/profile/json`;
        let loaderManifest;
        try {
            const req = await fetch(url);
            if (!req.ok) {
                const errorText = await req.text();
                throw new EMLLibError(ErrorType.FETCH_ERROR, `Failed to fetch ${this.metaConfig.name} loader manifest: HTTP ${req.status} ${errorText}`);
            }
            loaderManifest = await req.json();
        }
        catch (err) {
            if (existsSync(jsonPath)) {
                const content = await fs.readFile(jsonPath, 'utf-8');
                loaderManifest = JSON.parse(content);
            }
            else {
                if (err instanceof EMLLibError)
                    throw err;
                throw new EMLLibError(ErrorType.FETCH_ERROR, `Failed to fetch ${this.metaConfig.name} loader manifest: ${err instanceof Error ? err.message : err}`);
            }
        }
        const files = [];
        loaderManifest.id = `${this.loader.minecraftVersion}-${loaderId}-${this.loader.loaderVersion}`;
        await fs.writeFile(jsonPath, JSON.stringify(loaderManifest, null, 2));
        files.push({
            name: jsonName,
            path: path_.relative(this.config.root, versionPath),
            url: url,
            type: 'OTHER'
        });
        const libraries = await this.formatLibraries(loaderManifest.libraries);
        files.push(...libraries);
        return {
            loaderManifest: loaderManifest,
            installProfile: null,
            libraries: libraries,
            files: files
        };
    }
    async formatLibraries(libs) {
        const promises = libs.map(async (lib) => {
            const name = utils.getLibraryName(lib.name);
            const path = utils.getLibraryPath(lib.name, 'libraries');
            const defaultBaseUrl = this.loader.type === 'QUILT' ? 'https://maven.quiltmc.org/repository/release/' : 'https://maven.fabricmc.net/';
            const baseUrl = lib.url ?? defaultBaseUrl;
            const url = `${baseUrl}${utils.getLibraryPath(lib.name).replaceAll('\\', '/')}${name}`;
            const size = await utils.getRemoteFileSize(url, `Failed to get size for library ${name}`);
            const sha1 = await utils.getRemoteFileSha1(url, `Failed to get SHA1 for library ${name}`);
            return {
                name: name,
                path: path,
                url: url,
                sha1: sha1,
                size: size,
                type: 'LIBRARY',
                extra: 'LOADER'
            };
        });
        return await Promise.all(promises);
    }
}
//# sourceMappingURL=fabriclike.js.map