/**
 * @deprecated [DC-2] 无运行时消费方。保留供未来演进回归门控实现。
 *
 * 问题五十四: Evolution Regression + Template Evolution Gate — 演进回归与新问题门控。
 *
 * 强制执行:
 * - 新增问题/workflow 必须经过 DuplicateIssueCheck 和 TemplateEvolutionImpact
 * - 没有 NewIssueRegressionGate 的新增问题不得 accepted
 */
/**
 * 创建新问题回归门控。
 */
export function createNewIssueRegressionGate(overrides) {
    return {
        duplicate_check: {
            possible_duplicates: [],
            can_merge_into_existing_issue: false,
        },
        batch_impact_analysis: {
            touched_batches: [],
            touched_contracts: [],
            touched_workflows: [],
            touched_templates: [],
            required_regression_gates: [],
            release_gate_impact: [],
        },
        contract_impact: [],
        template_evolution_impact: {
            template_update_required: false,
            affected_templates: [],
            new_templates_required: [],
            workflow_template_update_required: false,
            adapter_prompt_update_required: false,
            manifest_update_required: false,
            sync_migration_required: false,
            user_project_update_required: false,
        },
        scenario_impact: [],
        migration_impact: [],
        user_project_impact: [],
        decision: "create_new_batch",
        evolution_gate_status: "pending",
        ...overrides,
    };
}
/**
 * 执行重复问题检查。
 */
export function checkDuplicateIssue(candidateTitle, existingIssues) {
    const candidateLower = candidateTitle.toLowerCase();
    const possibleDuplicates = existingIssues.filter(issue => issue.status !== "rejected" && (issue.title.toLowerCase() === candidateLower ||
        issue.title.toLowerCase().includes(candidateLower) ||
        candidateLower.includes(issue.title.toLowerCase())));
    return {
        possible_duplicates: possibleDuplicates.map(d => d.id),
        can_merge_into_existing_issue: possibleDuplicates.length > 0,
        merge_target_issue_id: possibleDuplicates[0]?.id,
        why_new_issue_required: possibleDuplicates.length === 0
            ? "无相似问题，需要新建"
            : undefined,
    };
}
/**
 * 分析现有领域影响。
 */
export function analyzeExistingBatchImpact(touchedContracts, touchedWorkflows) {
    const batchSet = new Set();
    // 根据 contract 前缀推断受影响的领域/模块
    for (const contract of touchedContracts) {
        if (contract.startsWith("foundation_"))
            batchSet.add("foundation");
        else if (contract.startsWith("verification_"))
            batchSet.add("verification");
        else if (contract.startsWith("knowledge_"))
            batchSet.add("knowledge");
        else if (contract.startsWith("extension_"))
            batchSet.add("extension");
    }
    return {
        touched_batches: [...batchSet],
        touched_contracts: touchedContracts,
        touched_workflows: touchedWorkflows,
        touched_templates: [],
        required_regression_gates: [...batchSet].map(b => `${b}_regression_gate`),
        release_gate_impact: [...batchSet].map(b => `${b}_release_gate 需更新`),
    };
}
/**
 * 分析模板演进影响。
 */
export function analyzeTemplateEvolutionImpact(touchedContracts, newWorkflows) {
    const needsUpdate = touchedContracts.length > 0 || newWorkflows.length > 0;
    return {
        template_update_required: needsUpdate,
        affected_templates: needsUpdate ? touchedContracts : [],
        new_templates_required: newWorkflows,
        workflow_template_update_required: newWorkflows.length > 0,
        adapter_prompt_update_required: touchedContracts.length > 0,
        manifest_update_required: needsUpdate,
        sync_migration_required: false,
        user_project_update_required: false,
        not_required_reason: !needsUpdate ? "无模板变更" : undefined,
    };
}
/**
 * 执行演进门控 — 没有 NewIssueRegressionGate 的新增问题不得 accepted。
 */
export function performEvolutionGate(gate) {
    const reasons = [];
    // 重复检查
    if (gate.evolution_gate_status === "pending") {
        reasons.push("演进门控状态仍为 pending，需要先完成检查");
    }
    if (gate.duplicate_check.possible_duplicates.length > 0 && !gate.duplicate_check.why_new_issue_required) {
        reasons.push(`发现可能重复的问题 (${gate.duplicate_check.possible_duplicates.join(", ")})，需说明为何需要新建`);
    }
    // 批次影响
    if (gate.batch_impact_analysis.required_regression_gates.length > 0) {
        // 有影响是正常的，不需要阻止，但需要记录
    }
    // 模板演进影响
    if (gate.template_evolution_impact.template_update_required && !gate.template_evolution_impact.manifest_update_required) {
        reasons.push("模板需要更新但未标记 manifest 更新");
    }
    // 最终决策
    if (gate.decision === "reject") {
        return {
            allowed: false,
            gate_status: "rejected",
            reasons: ["问题被否决"],
        };
    }
    if (reasons.length > 0) {
        return {
            allowed: false,
            gate_status: "manual_required",
            reasons,
        };
    }
    return {
        allowed: true,
        gate_status: "accepted",
        reasons: ["演进门控通过"],
    };
}
/**
 * 推进演进门控状态。
 */
export function advanceEvolutionGateStatus(current) {
    const transitions = {
        pending: ["duplicate_found", "accepted"],
        duplicate_found: ["impact_analyzed", "accepted", "rejected"],
        impact_analyzed: ["template_checked", "accepted", "rejected"],
        template_checked: ["accepted", "rejected", "manual_required"],
        accepted: [],
        rejected: [],
        manual_required: ["accepted", "rejected"],
    };
    return transitions[current] ?? [];
}
//# sourceMappingURL=evolution_regression_gate.js.map