"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TARGETS = exports.PACKAGE_NAME = void 0;
exports.supportedTargetLabels = supportedTargetLabels;
exports.detectLinuxLibc = detectLinuxLibc;
exports.executableName = executableName;
exports.vendorRelativePath = vendorRelativePath;
exports.unsupportedPlatformError = unsupportedPlatformError;
exports.resolveTarget = resolveTarget;
const node_path_1 = __importDefault(require("node:path"));
exports.PACKAGE_NAME = "@xelandernt/skilly";
exports.TARGETS = [
    {
        platform: "darwin",
        arch: "arm64",
        triple: "aarch64-apple-darwin",
        label: "macOS arm64"
    },
    {
        platform: "darwin",
        arch: "x64",
        triple: "x86_64-apple-darwin",
        label: "macOS x64"
    },
    {
        platform: "linux",
        arch: "x64",
        libc: "glibc",
        triple: "x86_64-unknown-linux-gnu",
        label: "Linux x64 (glibc)"
    },
    {
        platform: "win32",
        arch: "x64",
        triple: "x86_64-pc-windows-msvc",
        label: "Windows x64"
    }
];
function supportedTargetLabels() {
    return exports.TARGETS.map((target) => target.label);
}
function detectLinuxLibc(processLike = process) {
    if (processLike.platform !== "linux") {
        return null;
    }
    const report = processLike.report;
    if (!report || typeof report.getReport !== "function") {
        return "unknown";
    }
    const glibcVersionRuntime = report.getReport()?.header?.glibcVersionRuntime;
    return glibcVersionRuntime ? "glibc" : "musl";
}
function executableName(target) {
    return target.platform === "win32" ? "skilly.exe" : "skilly";
}
function vendorRelativePath(target) {
    return node_path_1.default.posix.join("vendor", target.triple, executableName(target));
}
function unsupportedPlatformError(processLike = process) {
    const libc = detectLinuxLibc(processLike);
    const details = libc
        ? `${processLike.platform} ${processLike.arch} (${libc})`
        : `${processLike.platform} ${processLike.arch}`;
    const supported = supportedTargetLabels().join(", ");
    const hint = processLike.platform === "linux" && libc !== "glibc"
        ? "Linux builds currently require glibc."
        : "No packaged binary matches this platform.";
    const error = new Error(`Unsupported platform for ${exports.PACKAGE_NAME}: ${details}. Supported targets: ${supported}. ${hint}`);
    error.code = "ERR_UNSUPPORTED_PLATFORM";
    return error;
}
function resolveTarget(processLike = process) {
    const libc = detectLinuxLibc(processLike);
    const target = exports.TARGETS.find((candidate) => {
        if (candidate.platform !== processLike.platform || candidate.arch !== processLike.arch) {
            return false;
        }
        if (candidate.platform !== "linux") {
            return true;
        }
        return candidate.libc === libc;
    });
    if (!target) {
        throw unsupportedPlatformError(processLike);
    }
    return target;
}
