"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readPlanningScaffoldTargetPathPolicy = readPlanningScaffoldTargetPathPolicy;
exports.isPlanningScaffoldTargetPath = isPlanningScaffoldTargetPath;
exports.extractPlanningScaffoldRepositoryPaths = extractPlanningScaffoldRepositoryPaths;
const config_layer_files_1 = require("../config/config-layer-files");
const documentation_path_classifier_1 = require("../validation/documentation-path-classifier");
const paths_1 = require("../paths");
const runtime_contract_1 = require("../runtime/runtime-contract");
const PLANNING_TASK_PAIR_PATH = /^\.day-shift\/planning\/(?:basic\/tasks|governed\/.+\/tasks)\/.+\/(?:task-definition|implementation-summary)\.md$/u;
function readPlanningScaffoldTargetPathPolicy(repositoryRoot) {
    const repositoryContext = (0, runtime_contract_1.createRepositoryContext)({
        root: repositoryRoot,
        detectionSource: "explicit"
    }, false);
    const { repositoryConfig } = (0, config_layer_files_1.readPersistedConfigFiles)(repositoryContext);
    const configuredPolicy = repositoryConfig.runtime_evidence?.path_policy;
    return {
        documentation_only_paths: [...(configuredPolicy?.documentation_only_paths ?? [])],
        runtime_eligible_paths: [...(configuredPolicy?.runtime_eligible_paths ?? [])]
    };
}
/**
 * Scaffold targets are repository paths, not framework conventions. Known
 * documentation paths stay out of executable task evidence unless repository
 * policy explicitly marks their prefix as runtime-eligible.
 */
function isPlanningScaffoldTargetPath(value, policy = {}) {
    const candidate = value.trim().replace(/^`|`$/gu, "");
    if (candidate === ""
        || /\s/u.test(candidate)
        || candidate.includes("*")
        || candidate.includes("?")) {
        return false;
    }
    let normalizedPath;
    try {
        normalizedPath = (0, paths_1.parseRepositoryRelativePath)(candidate).normalizedPath;
    }
    catch {
        return false;
    }
    if (PLANNING_TASK_PAIR_PATH.test(normalizedPath)) {
        return true;
    }
    return (0, documentation_path_classifier_1.classifyDocumentationPath)(normalizedPath, policy).runtimeEligible;
}
/**
 * Extracts canonical repository paths without assuming any source-tree name.
 * Bare root filenames are accepted for explicit semantic values, while prose
 * fallback extraction requires path-like punctuation such as a slash or dot.
 */
function extractPlanningScaffoldRepositoryPaths(value, options = {}) {
    const direct = normalizeExtractedPathCandidate(value, options.allowBareBasename === true);
    if (direct !== null) {
        return [direct];
    }
    const paths = [];
    const candidates = [
        ...[...value.matchAll(/`([^`]+)`/gu)].map((match) => match[1]),
        ...value.split(/\s+/u)
    ];
    for (const candidate of candidates) {
        const normalized = normalizeExtractedPathCandidate(candidate, false);
        if (normalized !== null && !paths.includes(normalized)) {
            paths.push(normalized);
        }
    }
    return paths;
}
function normalizeExtractedPathCandidate(value, allowBareBasename) {
    const candidate = value.trim()
        .replace(/^[`'"([{]+/u, "")
        .replace(/[`'")}\],;:.]+$/u, "");
    if (candidate === ""
        || /\s/u.test(candidate)
        || candidate.includes("*")
        || candidate.includes("?")
        || candidate.includes("://")
        || (!allowBareBasename && !candidate.includes("/") && !candidate.includes("."))) {
        return null;
    }
    try {
        return (0, paths_1.parseRepositoryRelativePath)(candidate).normalizedPath;
    }
    catch {
        return null;
    }
}
