/**
 * 中文语义优先机制 — 问题十 (Chinese Semantic Priority)。
 *
 * 在用户项目内建立中文语义优先默认策略。
 * - 业务语义、验收标准、审查结论和治理契约默认中文
 * - 代码标识符、命令、日志、协议保持原样
 * - 与问题四十一 LanguagePolicyOverride 共享 LanguagePolicy 类型
 * - 消费 Batch 2 language_policy_contract，不重定义总契约
 */
// ── 中文优先范围 ──
/** 必须中文优先的内容领域 */
export const ZH_FIRST_DOMAINS = [
    "business_semantics",
    "acceptance_criteria",
    "review_findings",
    "governance_contracts",
    "user_feedback",
];
/** 不强制中文化的内容领域 */
export const PRESERVE_DOMAINS = [
    "code_identifiers",
    "logs_and_errors",
    "api_docs",
    "commit_messages",
];
// ── 核心函数 ──
/**
 * 评估内容是否遵守中文语义优先策略。
 * 硬规则：
 * - zh_first 域内容必须中文优先
 * - code_native 域不得翻译
 * - source_preserving 域保持原文
 * - en_allowed 域允许英文
 */
export function evaluateSemanticPriority(config, contents) {
    const checks = [];
    const pollutionFindings = [];
    for (const item of contents) {
        const domainPolicy = getDomainPolicy(config, item.domain);
        const expectedLang = getExpectedLanguage(domainPolicy);
        const actualLang = detectLanguage(item.content);
        const passed = checkLanguageCompliance(expectedLang, actualLang, domainPolicy);
        checks.push({
            domain: item.domain,
            passed,
            policy: domainPolicy,
            expected_language: expectedLang,
            actual_language: actualLang,
            message_zh: passed
                ? `${item.domain} 语义策略合规`
                : `${item.domain} 语义策略违规：期望 ${expectedLang}，实际 ${actualLang}`,
        });
        if (!passed && expectedLang === "zh" && actualLang === "en") {
            pollutionFindings.push({
                id: `pollution-${item.id}`,
                entry_id: item.id,
                status: "polluted",
                detected_language: actualLang,
                expected_language: expectedLang,
                message: `中文优先域 ${item.domain} 被英文内容污染: ${item.id}`,
                remediation: "恢复中文语义或配置 en_allowed 策略",
            });
        }
    }
    const passingCount = checks.filter((c) => c.passed).length;
    return {
        compliant: checks.every((c) => c.passed),
        checks,
        pollution_findings: pollutionFindings,
        total_domains: checks.length,
        passing_domains: passingCount,
    };
}
/**
 * 验证中文项目不被英文模板污染。
 * 硬规则：zh_first 项目的知识条目不得被英文模板默认值覆盖。
 */
export function validateZhProjectProtection(config, templateContent, targetDomain) {
    if (config.default !== "zh_first") {
        return { protected: true };
    }
    const domainPolicy = getDomainPolicy(config, targetDomain);
    if (domainPolicy === "en_allowed" || domainPolicy === "code_native" || domainPolicy === "source_preserving") {
        return { protected: true };
    }
    // zh_first 域检查模板是否英文
    const templateLang = detectLanguage(templateContent);
    if (templateLang === "en") {
        return {
            protected: false,
            reason: `中文项目（zh_first）的 ${targetDomain} 域不得被英文模板污染`,
        };
    }
    return { protected: true };
}
/**
 * 验证英文项目可配置 en_allowed。
 */
export function validateEnProjectOverride(config) {
    if (config.default === "en_first" || config.default === "source_preserving") {
        return { valid: true };
    }
    return { valid: true };
}
// ── 辅助函数 ──
function getDomainPolicy(config, domain) {
    const domainMap = {
        business_semantics: config.business_semantics,
        acceptance_criteria: config.acceptance_criteria,
        review_findings: config.review_findings,
        governance_contracts: config.governance_contracts,
        user_feedback: config.user_feedback,
        code_comments: config.code_comments,
        api_docs: config.api_docs,
        commit_messages: config.commit_messages,
        logs_and_errors: config.logs_and_errors,
        code_identifiers: config.code_identifiers,
    };
    return domainMap[domain] ?? config.business_semantics;
}
function getExpectedLanguage(policy) {
    switch (policy) {
        case "zh_first": return "zh";
        case "en_allowed": return "en";
        case "source_preserving":
        case "code_native": return "preserve";
        default: return "zh";
    }
}
function detectLanguage(content) {
    if (!content || content.length === 0)
        return "en";
    const chineseChars = (content.match(/[一-鿿]/g) ?? []).length;
    const englishWords = (content.match(/[a-zA-Z]+/g) ?? []).length;
    const total = chineseChars + englishWords;
    if (total === 0)
        return "en";
    if (chineseChars / total > 0.6)
        return "zh";
    if (englishWords / total > 0.7)
        return "en";
    return "mixed";
}
function checkLanguageCompliance(expected, actual, _policy) {
    if (expected === "preserve")
        return true;
    if (expected === "zh")
        return actual === "zh" || actual === "mixed";
    if (expected === "en")
        return true;
    return true;
}
//# sourceMappingURL=chinese_semantic_priority.js.map