"use strict";
/**
 * @author NovaStepStudios
 * @alias StepnickaSantiago
 * @license Apache-2.0
 * @link https://www.apache.org/licenses/LICENSE-2.0
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mirrors = void 0;
exports.getPathLibraries = getPathLibraries;
exports.getFileHash = getFileHash;
exports.isOldVersion = isOldVersion;
exports.loader = loader;
exports.getFileFromArchive = getFileFromArchive;
exports.skipLibrary = skipLibrary;
exports.fromAnyReadable = fromAnyReadable;
const crypto_1 = __importDefault(require("crypto"));
const fs_1 = __importDefault(require("fs"));
const stream_1 = require("stream");
const Unzipper_js_1 = require("./Unzipper.js");
function getPathLibraries(main, nativeString, forceExt = '.jar') {
    const parts = main.split(':');
    const group = parts[0] ?? '';
    const artifact = parts[1] ?? '';
    const version = parts[2] ?? '';
    const classifier = parts[3] ? `-${parts[3]}` : '';
    const fileName = `${artifact}-${version.replace('@', '.')}${nativeString || ''}${classifier}${forceExt}`;
    const filePath = `${group.replace(/\./g, '/')}/${artifact}/${version.split('@')[0]}`;
    return { path: filePath, name: fileName, version };
}
async function getFileHash(filePath, algorithm = 'sha1') {
    const hash = crypto_1.default.createHash(algorithm);
    const stream = fs_1.default.createReadStream(filePath);
    return new Promise((resolve, reject) => {
        stream.on('data', chunk => hash.update(chunk));
        stream.on('end', () => resolve(hash.digest('hex')));
        stream.on('error', reject);
    });
}
function isOldVersion(json) {
    return json.assets === 'legacy' || json.assets === 'pre-1.6';
}
function loader(type) {
    const data = {
        forge: {
            meta: 'https://files.minecraftforge.net/net/minecraftforge/forge/maven-metadata.json',
            installer: 'https://maven.minecraftforge.net/net/minecraftforge/forge/${version}/forge-${version}-installer.jar',
            universal: 'https://maven.minecraftforge.net/net/minecraftforge/forge/${version}/forge-${version}-universal.jar'
        },
        neoforge: {
            meta: 'https://maven.neoforged.net/api/maven/versions/releases/net/neoforged/neoforge',
            installer: 'https://maven.neoforged.net/releases/net/neoforged/neoforge/${version}/neoforge-${version}-installer.jar'
        },
        fabric: {
            meta: 'https://meta.fabricmc.net/v2/versions',
            data: 'https://meta.legacyfabric.net/v2/versions/loader',
            json: 'https://meta.fabricmc.net/v2/versions/loader/${version}/${build}/profile/json'
        },
        legacyfabric: {
            meta: 'https://meta.legacyfabric.net/v2/versions',
            json: 'https://meta.legacyfabric.net/v2/versions/loader/${version}/${build}/profile/json'
        },
        quilt: {
            meta: 'https://meta.quiltmc.org/v3/versions',
            json: 'https://meta.quiltmc.org/v3/versions/loader/${version}/${build}/profile/json'
        }
    };
    return data[type];
}
exports.mirrors = [
    'https://maven.minecraftforge.net',
    'https://maven.neoforged.net/releases',
    'https://maven.creeperhost.net',
    'https://libraries.minecraft.net',
    'https://repo1.maven.org/maven2'
];
async function getFileFromArchive(jar, file = null, prefix = null) {
    const unzip = new Unzipper_js_1.Unzipper(jar);
    if (file) {
        const entry = unzip.getEntry(file);
        if (!entry)
            return undefined;
        return entry.getData();
    }
    const entries = prefix ? unzip.getEntriesWithPrefix(prefix) : unzip.getEntries();
    return entries.length > 0 ? entries : undefined;
}
function skipLibrary(lib) {
    const map = { win32: 'windows', darwin: 'osx', linux: 'linux' };
    if (!lib.rules)
        return false;
    let skip = true;
    for (const rule of lib.rules) {
        if (rule.action === 'allow' && (!rule.os || rule.os.name === map[process.platform]))
            skip = false;
        if (rule.action === 'disallow' && (!rule.os || rule.os.name === map[process.platform]))
            skip = true;
    }
    return skip;
}
function fromAnyReadable(webStream) {
    const nodeStream = new stream_1.Readable({
        read() { }
    });
    const reader = webStream.getReader();
    const pump = async () => {
        try {
            while (true) {
                const { done, value } = await reader.read();
                if (done) {
                    nodeStream.push(null);
                    break;
                }
                nodeStream.push(value ? Buffer.from(value) : Buffer.alloc(0));
            }
        }
        catch (err) {
            nodeStream.destroy(err);
        }
    };
    pump();
    return nodeStream;
}
