"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.classifyDocumentationPath = classifyDocumentationPath;
exports.isDocumentationOnlyPath = isDocumentationOnlyPath;
const DOCUMENTATION_DIRECTORY_RULES = [
    { prefix: ".day-shift/specs/", reason: "day-shift-specification" },
    { prefix: ".day-shift/planning/", reason: "day-shift-planning" },
    { prefix: ".day-shift/prompts/", reason: "day-shift-prompt" },
    { prefix: ".day-shift/templates/", reason: "day-shift-template" },
    { prefix: ".day-shift/workflows/", reason: "day-shift-workflow" },
    { prefix: ".day-shift/references/", reason: "day-shift-reference" },
    { prefix: "docs/", reason: "repository-docs" }
];
const MARKDOWN_EXTENSIONS = new Set([".md", ".markdown", ".mdown", ".mkdn"]);
const TEXT_EXTENSIONS = new Set([".txt", ".text", ".rst", ".adoc", ".asciidoc"]);
const RELEASE_NOTE_BASENAME_PATTERN = /(^|[-_.])(?:release[-_.]?notes?|changelog|changes)([-_.]|$)/i;
function classifyDocumentationPath(path, policy = {}) {
    const normalizedPath = normalizeClassificationPath(path);
    if (policy.declaredTargetRole === "runtime") {
        return runtimeEligible(normalizedPath, "declared-runtime-role");
    }
    if (policy.declaredTargetRole === "documentation") {
        return documentationOnly(normalizedPath, "declared-documentation-role");
    }
    if (matchesPolicyPath(normalizedPath, policy.runtime_eligible_paths)) {
        return runtimeEligible(normalizedPath, "configured-runtime-eligible");
    }
    if (matchesPolicyPath(normalizedPath, policy.documentation_only_paths)) {
        return documentationOnly(normalizedPath, "configured-documentation-only");
    }
    const directoryRule = DOCUMENTATION_DIRECTORY_RULES.find((rule) => normalizedPath === trimTrailingSlash(rule.prefix) || normalizedPath.startsWith(rule.prefix));
    if (directoryRule !== undefined) {
        return documentationOnly(normalizedPath, directoryRule.reason);
    }
    const basename = normalizedPath.split("/").pop() ?? normalizedPath;
    const extension = extensionOf(basename);
    if (RELEASE_NOTE_BASENAME_PATTERN.test(basename)) {
        return documentationOnly(normalizedPath, "release-note-file");
    }
    if (MARKDOWN_EXTENSIONS.has(extension)) {
        return documentationOnly(normalizedPath, "markdown-file");
    }
    if (TEXT_EXTENSIONS.has(extension)) {
        return documentationOnly(normalizedPath, "text-file");
    }
    return runtimeEligible(normalizedPath, "runtime-eligible-default");
}
function isDocumentationOnlyPath(path, policy = {}) {
    return classifyDocumentationPath(path, policy).documentationOnly;
}
function runtimeEligible(normalizedPath, reason) {
    return {
        path: normalizedPath,
        category: "runtime-eligible",
        documentationOnly: false,
        runtimeEligible: true,
        reason
    };
}
function documentationOnly(normalizedPath, reason) {
    return {
        path: normalizedPath,
        category: "documentation-only",
        documentationOnly: true,
        runtimeEligible: false,
        reason
    };
}
function normalizeClassificationPath(path) {
    return path
        .replace(/\\/g, "/")
        .replace(/^\.\//, "")
        .replace(/\/+/g, "/")
        .replace(/\/$/, "");
}
function matchesPolicyPath(normalizedPath, policyPaths) {
    if (policyPaths === undefined) {
        return false;
    }
    return policyPaths.some((policyPath) => {
        const normalizedPolicyPath = normalizeClassificationPath(policyPath);
        const exactPath = trimTrailingSlash(normalizedPolicyPath);
        if (normalizedPolicyPath.endsWith("/")) {
            return normalizedPath.startsWith(`${exactPath}/`);
        }
        return normalizedPath === exactPath || normalizedPath.startsWith(`${exactPath}/`);
    });
}
function trimTrailingSlash(path) {
    return path.endsWith("/") ? path.slice(0, -1) : path;
}
function extensionOf(basename) {
    const extensionStart = basename.lastIndexOf(".");
    if (extensionStart <= 0) {
        return "";
    }
    return basename.slice(extensionStart).toLowerCase();
}
