/**
 * 知识治理统一入口。
 *
 * 将知识生命周期、中文语义、知识演进、文档治理和语言策略串成单一检查入口。
 */
import { evaluatePromotion } from "./knowledge_lifecycle.js";
import { validatePromotionEvidence } from "./knowledge_evolution.js";
import { evaluateSemanticPriority } from "./chinese_semantic_priority.js";
import { validateDesignDocEligibility, checkDocContentCompliance } from "./documentation_governance.js";
import { evaluateLanguagePolicy } from "./language_policy_contract.js";
function defaultLanguagePolicy(language = "zh") {
    const zh = language === "zh";
    return {
        default: zh ? "zh_first" : "en_first",
        business_semantics: zh ? "zh_first" : "en_allowed",
        acceptance_criteria: zh ? "zh_first" : "en_allowed",
        review_findings: zh ? "zh_first" : "en_allowed",
        governance_contracts: zh ? "zh_first" : "en_allowed",
        user_feedback: zh ? "zh_first" : "en_allowed",
        code_comments: zh ? "zh_first" : "en_allowed",
        api_docs: "source_preserving",
        commit_messages: "source_preserving",
        logs_and_errors: "source_preserving",
        code_identifiers: "code_native",
    };
}
export function checkKnowledgePromotionHealth(context) {
    const candidate = context.promotion_candidate;
    if (!candidate)
        return [];
    const result = evaluatePromotion({
        entry_id: candidate.id,
        from_status: context.from_status ?? "draft",
        to_status: candidate.proposed_status,
        from_sovereignty: context.from_sovereignty ?? "normal",
        to_sovereignty: candidate.proposed_sovereignty,
        evidence_ids: candidate.evidence_ids,
        requested_by: context.requested_by ?? "ai_inference",
    });
    if (result.allowed)
        return [];
    return [result.reason].map((reason) => ({
        severity: "hard_fail",
        kind: "promotion",
        message_zh: reason,
        evidence_ref: candidate.id,
    }));
}
export function checkEvolutionEvidence(context) {
    const candidate = context.promotion_candidate;
    if (!candidate)
        return [];
    const result = validatePromotionEvidence(candidate, context.promotion_evidences ?? []);
    if (result.sufficient)
        return [];
    return result.missing.map((message) => ({
        severity: "hard_fail",
        kind: "evolution",
        message_zh: message,
        evidence_ref: candidate.id,
    }));
}
export function checkSemanticCompliance(context) {
    const contents = context.contents ?? [];
    if (contents.length === 0)
        return [];
    const result = evaluateSemanticPriority(defaultLanguagePolicy(context.project_language), contents);
    return result.pollution_findings.map((finding) => ({
        severity: "warning",
        kind: "semantic",
        message_zh: finding.message,
        evidence_ref: finding.entry_id,
    }));
}
export function checkDocumentationGovernance(context) {
    const findings = [];
    if (context.design_doc_content) {
        for (const finding of checkDocContentCompliance("design_doc", context.design_doc_content)) {
            findings.push({
                severity: finding.severity === "info" ? "advisory" : finding.severity,
                kind: "documentation",
                message_zh: finding.message_zh,
                evidence_ref: finding.evidence_ref,
            });
        }
    }
    const eligibility = validateDesignDocEligibility({
        decision_id: "knowledge-governance-smoke",
        doc_eligibility_status: "verified",
        evidence_refs: ["knowledge-governance-gate"],
    });
    if (!eligibility.allowed) {
        findings.push({ severity: "hard_fail", kind: "documentation", message_zh: eligibility.reason });
    }
    return findings;
}
export function checkLanguagePolicyCompliance(context) {
    const evaluation = evaluateLanguagePolicy("user_project", [
        { rule_id: "lp-user-feedback-zh", location: "knowledge_governance", passed: context.project_language !== "en", detail: "用户项目默认中文语义优先" },
    ], context.language_override_evidence);
    return evaluation.findings.map((finding) => ({
        severity: finding.severity,
        kind: "language",
        message_zh: finding.message_zh,
        evidence_ref: finding.location,
    }));
}
export function runKnowledgeGovernanceCheck(context = {}) {
    const findings = [
        ...checkKnowledgePromotionHealth(context),
        ...checkEvolutionEvidence(context),
        ...checkSemanticCompliance(context),
        ...checkDocumentationGovernance(context),
        ...checkLanguagePolicyCompliance(context),
    ];
    return {
        checked_at: new Date().toISOString(),
        findings,
        hard_fail_count: findings.filter((f) => f.severity === "hard_fail").length,
        warning_count: findings.filter((f) => f.severity === "warning").length,
    };
}
//# sourceMappingURL=knowledge_governance_gate.js.map