const DURABLE_DECISION_MARKERS = /\b(decid(?:e|ed|imos)|decision|adr|architectural|use .+ instead of|prefer .+ over|standardize|convention)\b/i;
const EPHEMERAL_MARKERS = /\b(temporary|for now only|quick fix|debug|log|stack trace|today)\b/i;
function compact(value, max = 500) {
    const normalized = value.replace(/\s+/g, " ").trim();
    return normalized.length <= max ? normalized : `${normalized.slice(0, max - 3)}...`;
}
function titleFromDecision(decision) {
    return compact(decision, 80).replace(/[.:;]+$/g, "");
}
export function detectAdrCandidates(text, source = "user") {
    if (!DURABLE_DECISION_MARKERS.test(text) || EPHEMERAL_MARKERS.test(text))
        return [];
    const candidates = [];
    const explicit = text.match(/(?:decid(?:e|ed|imos)|decision)[:\s]+(.+?)(?:\n|$)/i)?.[1];
    const tradeoff = text.match(/(?:use|usar|prefer|preferir)\s+(.+?)\s+(?:instead of|over|en vez de|sobre)\s+(.+?)(?:\.|\n|$)/i);
    if (explicit) {
        const decision = compact(explicit);
        candidates.push({
            title: titleFromDecision(decision),
            context: "Detected explicit durable decision language in the conversation.",
            decision,
            consequences: "Review required before accepting as an ADR.",
            reason: "explicit decision marker",
            confidence: 0.8,
            source,
        });
    }
    if (tradeoff) {
        const decision = `Prefer ${compact(tradeoff[1], 120)} over ${compact(tradeoff[2], 120)}.`;
        candidates.push({
            title: titleFromDecision(decision),
            context: "Detected a durable preference/tradeoff statement.",
            decision,
            consequences: "Review alternatives and implications before accepting.",
            reason: "tradeoff preference marker",
            confidence: 0.75,
            source,
        });
    }
    return dedupeAdrCandidates(candidates);
}
export function dedupeAdrCandidates(candidates) {
    const seen = new Set();
    const deduped = [];
    for (const candidate of candidates) {
        const key = candidate.title.toLowerCase();
        if (seen.has(key))
            continue;
        seen.add(key);
        deduped.push(candidate);
    }
    return deduped.sort((left, right) => right.confidence - left.confidence);
}
export function candidateToAdrRecord(candidate) {
    return {
        title: candidate.title,
        context: candidate.context,
        decision: candidate.decision,
        consequences: candidate.consequences,
        links: candidate.links,
    };
}
//# sourceMappingURL=adr-candidates.js.map