/**
 * 问题五十: Existing System Analysis / Gap Analysis — 现有系统分析与差距分析。
 *
 * 强制执行:
 * - existing_system + new_requirement 必须先做 ExistingSystemAnalysis + RequirementGapAnalysis
 */
/**
 * 检测项目上下文类型。
 */
export function detectProjectContext(hasExistingCode, hasPackageJson, hasSrcDir, userOverride) {
    let contextType;
    let confidence;
    if (userOverride) {
        contextType = userOverride;
        confidence = "high";
    }
    else if (hasExistingCode && hasPackageJson && hasSrcDir) {
        contextType = "existing_system";
        confidence = "high";
    }
    else if (!hasExistingCode && !hasPackageJson) {
        contextType = "empty_project";
        confidence = "high";
    }
    else if (hasExistingCode || hasSrcDir) {
        contextType = "existing_system";
        confidence = "medium";
    }
    else {
        contextType = "unknown";
        confidence = "low";
    }
    return {
        project_context_type: contextType,
        detection_evidence_refs: [],
        confidence,
    };
}
/**
 * 创建现有系统分析契约。
 */
export function createExistingSystemAnalysis(projectType, overrides) {
    return {
        project_type: projectType,
        existing_system_analysis_status: projectType === "existing_system" ? "pending" : "not_required",
        analysis_depth: "L1_project_fingerprint",
        detected_tech_stack: [],
        module_boundaries: [],
        business_flows: [],
        data_models: [],
        api_contracts: [],
        auth_and_permission_model: [],
        existing_tests: [],
        reusable_assets: [],
        unknowns: [],
        evidence_refs: [],
        ...overrides,
    };
}
/**
 * 创建差距分析。
 */
export function createRequirementGapAnalysis(overrides) {
    return {
        gap_analysis_status: "pending",
        requirement_summary: "",
        existing_capabilities: [],
        missing_capabilities: [],
        reusable_modules: [],
        required_changes: [],
        optional_changes: [],
        out_of_scope: [],
        compatibility_risks: [],
        data_risks: [],
        permission_risks: [],
        integration_risks: [],
        frontend_impact: [],
        backend_impact: [],
        test_regression_scope: [],
        user_confirmation_required: [],
        recommended_implementation_strategy: "",
        alternatives: [],
        evidence_refs: [],
        ...overrides,
    };
}
/**
 * 分析前置检查 — 现有系统 + 新需求必须先做分析。
 */
export function checkAnalysisGate(context, hasAnalysis, hasGapAnalysis) {
    const missing = [];
    if (context.project_context_type === "existing_system") {
        if (!hasAnalysis) {
            missing.push("ExistingSystemAnalysis: 现有系统必须先做系统分析");
        }
        if (!hasGapAnalysis) {
            missing.push("RequirementGapAnalysis: 现有系统 + 新需求必须先做差距分析");
        }
    }
    if (context.project_context_type === "unknown") {
        if (!hasAnalysis) {
            missing.push("ExistingSystemAnalysis: 项目类型未知，需要先分析");
        }
    }
    return {
        allowed: missing.length === 0,
        reason: missing.length === 0 ? "分析前置条件已满足" : `缺少必要分析: ${missing.join("; ")}`,
        missing_analysis: missing,
    };
}
/**
 * 创建证据支撑事实。
 */
export function createEvidenceBackedFact(claim, sourceType, confidence) {
    return { claim, source_type: sourceType, confidence, evidence_refs: [] };
}
/**
 * 审查现有系统开发产出。
 */
export function reviewExistingSystemDevelopment(analysis, gapAnalysis) {
    const failedRules = [];
    const missingEvidence = [];
    const missingIntegration = [];
    const manualRequired = [];
    if (analysis.project_type === "existing_system" && analysis.detected_tech_stack.length === 0) {
        failedRules.push("现有系统分析未检测到技术栈");
    }
    if (gapAnalysis.missing_capabilities.length > 0 && gapAnalysis.required_changes.length === 0) {
        failedRules.push("差距分析发现缺失能力但未列出必要变更");
    }
    if (gapAnalysis.compatibility_risks.length > 0 && !gapAnalysis.recommended_implementation_strategy) {
        manualRequired.push("存在兼容性风险但未提供推荐策略");
    }
    if (analysis.unknowns.length > 0) {
        const criticalUnknowns = analysis.unknowns.filter(u => u.resolution_strategy === "ask_user");
        if (criticalUnknowns.length > 0) {
            manualRequired.push(`存在 ${criticalUnknowns.length} 个需要用户确认的未知项`);
        }
    }
    return {
        review_status: failedRules.length > 0 ? "failed"
            : manualRequired.length > 0 ? "manual_required"
                : "passed",
        failed_rules: failedRules,
        missing_evidence_refs: missingEvidence,
        missing_integration_points: missingIntegration,
        manual_required_items: manualRequired,
        repair_required: failedRules.length > 0,
    };
}
//# sourceMappingURL=existing_system_analysis.js.map