import { constants } from "fs/promises";
import { getRuntimeConfig } from "../runtimeConfig.js";
import { verifyDir } from "../utils.js";
export function buildPathProblem(descriptor) {
    const { category, name, path, issue, message, severity } = descriptor;
    return {
        id: `path:${category}:${issue}:${path}`,
        severity,
        summary: `${name}: ${message}`,
        details: `Path: ${path}`,
        metadata: { category, path, issue },
    };
}
export async function diagnoseDirForProblems(path, name, category, options) {
    const mode = (options.read ? constants.R_OK : 0) |
        (options.write ? constants.W_OK : 0);
    const verification = await verifyDir(path, name, mode);
    if (verification.ok) {
        return { descriptor: null, verification };
    }
    let descriptor;
    switch (verification.reason) {
        case "missing":
            descriptor = {
                category,
                name,
                path,
                issue: "missing",
                message: "Directory does not exist.",
                severity: "error",
            };
            break;
        case "not-directory":
            descriptor = {
                category,
                name,
                path,
                issue: "not-directory",
                message: "Path is not a directory.",
                severity: "error",
            };
            break;
        case "unreadable":
            descriptor = {
                category,
                name,
                path,
                issue: "unreadable",
                message: "cross-seed cannot read from this directory.",
                severity: "error",
            };
            break;
        case "unwritable":
            descriptor = {
                category,
                name,
                path,
                issue: "unwritable",
                message: "cross-seed cannot write to this directory.",
                severity: "error",
            };
            break;
        default:
            descriptor = {
                category,
                name,
                path,
                issue: "unreadable",
                message: "cross-seed cannot access this directory.",
                severity: "error",
            };
            break;
    }
    return { descriptor, verification };
}
export async function collectPathProblems() {
    const { torrentDir, outputDir, injectDir } = getRuntimeConfig();
    const descriptors = [];
    if (torrentDir) {
        const { descriptor } = await diagnoseDirForProblems(torrentDir, "torrentDir", "torrentDir", { read: true });
        if (descriptor)
            descriptors.push(descriptor);
    }
    if (outputDir) {
        const { descriptor } = await diagnoseDirForProblems(outputDir, "outputDir", "outputDir", { read: true, write: true });
        if (descriptor)
            descriptors.push(descriptor);
    }
    if (injectDir) {
        const { descriptor } = await diagnoseDirForProblems(injectDir, "injectDir", "injectDir", { read: true, write: true });
        if (descriptor)
            descriptors.push(descriptor);
    }
    return descriptors.map(buildPathProblem);
}
//# sourceMappingURL=path.js.map