/**
 * 语言策略配置 — 问题四十一 (Language Policy Override) + 问题十 (中文语义优先)。
 *
 * 管理语言策略配置，支持 zh_first / source_preserving / en_allowed / code_native。
 * 消费 Batch 2 language_policy_contract，不重定义总契约。
 * 使用 language_policy_status 规范字段名。
 */
// ── 默认配置（中文语义优先） ──
const DEFAULT_ZH_FIRST_CONFIG = {
    default: "zh_first",
    business_semantics: "zh_first",
    acceptance_criteria: "zh_first",
    review_findings: "zh_first",
    governance_contracts: "zh_first",
    user_feedback: "zh_first",
    code_comments: "source_preserving",
    api_docs: "source_preserving",
    commit_messages: "source_preserving",
    logs_and_errors: "source_preserving",
    code_identifiers: "code_native",
};
const EN_FIRST_CONFIG = {
    default: "en_first",
    business_semantics: "en_allowed",
    acceptance_criteria: "en_allowed",
    review_findings: "en_allowed",
    governance_contracts: "en_allowed",
    user_feedback: "zh_first",
    code_comments: "source_preserving",
    api_docs: "source_preserving",
    commit_messages: "source_preserving",
    logs_and_errors: "source_preserving",
    code_identifiers: "code_native",
    override_source: "project_config",
};
const BILINGUAL_CONFIG = {
    default: "bilingual",
    business_semantics: "zh_first",
    acceptance_criteria: "zh_first",
    review_findings: "zh_first",
    governance_contracts: "zh_first",
    user_feedback: "zh_first",
    code_comments: "source_preserving",
    api_docs: "en_allowed",
    commit_messages: "source_preserving",
    logs_and_errors: "source_preserving",
    code_identifiers: "code_native",
    override_source: "project_config",
};
const SOURCE_PRESERVING_CONFIG = {
    default: "source_preserving",
    business_semantics: "source_preserving",
    acceptance_criteria: "source_preserving",
    review_findings: "source_preserving",
    governance_contracts: "source_preserving",
    user_feedback: "source_preserving",
    code_comments: "source_preserving",
    api_docs: "source_preserving",
    commit_messages: "source_preserving",
    logs_and_errors: "source_preserving",
    code_identifiers: "code_native",
    override_source: "project_config",
};
// ── 核心函数 ──
/**
 * 获取当前语言策略配置。
 * 默认 zh_first。
 */
export function getLanguagePolicyConfig(override) {
    switch (override ?? "zh_first") {
        case "zh_first":
            return { ...DEFAULT_ZH_FIRST_CONFIG };
        case "en_first":
            return { ...EN_FIRST_CONFIG };
        case "bilingual":
            return { ...BILINGUAL_CONFIG };
        case "source_preserving":
            return { ...SOURCE_PRESERVING_CONFIG };
        case "project_override":
            return { ...DEFAULT_ZH_FIRST_CONFIG };
        default:
            return { ...DEFAULT_ZH_FIRST_CONFIG };
    }
}
/**
 * 检测中文项目是否被英文模板污染。
 * 硬规则：中文项目（zh_first）的知识条目不得被英文内容覆盖。
 */
export function detectSemanticPollution(config, entries) {
    const findings = [];
    if (config.default !== "zh_first") {
        return findings;
    }
    for (const entry of entries) {
        const expected = entry.expected_language ?? "zh";
        if (expected === "zh" && isEnglishDominant(entry.content)) {
            findings.push({
                id: `pollution-${entry.id}`,
                entry_id: entry.id,
                status: "polluted",
                detected_language: "en",
                expected_language: "zh",
                message: `中文项目知识条目 ${entry.id} 被英文内容污染`,
                remediation: "恢复中文语义或标记为 source_preserving",
            });
        }
    }
    return findings;
}
/**
 * 判断文本是否以英文为主。
 * 简单启发式：英文单词占比超过 70% 视为英文为主。
 */
function isEnglishDominant(text) {
    if (!text || text.length === 0)
        return false;
    const englishWords = text.match(/[a-zA-Z]+/g) ?? [];
    const chineseChars = text.match(/[一-鿿]/g) ?? [];
    const totalContent = englishWords.length + chineseChars.length;
    if (totalContent === 0)
        return false;
    return englishWords.length / totalContent > 0.7;
}
/**
 * 验证语言策略变更不会破坏已有知识条目的主权。
 * 硬规则：语言策略变更不得改变 sovereignty 字段。
 */
export function validateLanguagePolicyChange(_oldConfig, _newConfig) {
    // 语言策略变更只影响输出语言，不影响知识主权
    return { valid: true };
}
/**
 * 获取语言策略（兼容旧接口）。
 * @deprecated 被 3 个知识模块引用（knowledge_writer, knowledge_asset_schema, knowledge_config_loader），标记待迁移到新 API。使用 getLanguagePolicyConfig 替代。
 */
export function getLanguagePolicy() {
    return { mode: "zh_first", enforce_zh_comments: true, enforce_zh_logs: true };
}
//# sourceMappingURL=language_policy.js.map