/**
 * OOD 与 SOLID 实现契约。
 *
 * 复杂业务编码在执行前必须给出对象职责与依赖边界；审查和交付阶段
 * 使用同一套 finding 口径阻断典型的 SOLID 违规。
 */
const COMPLEX_OOD_KEYWORDS = /领域|业务规则|状态流转|策略模式|计费策略|订单流转|支付编排|结算规则|工作流编排|重构架构|模块设计|服务设计|领域服务|应用服务设计|domain|aggregate|solid|面向对象|职责拆分|接口抽象|依赖倒置/i;
const LOW_RISK_KEYWORDS = /错别字|拼写|文案|注释|样式|css|颜色|README|日志文案|配置值|测试数据|fixture/i;
export function requiresOodSolidContract(intent, route, changedFiles = []) {
    const text = `${intent} ${changedFiles.join(" ")}`;
    if (LOW_RISK_KEYWORDS.test(text) && !COMPLEX_OOD_KEYWORDS.test(text))
        return false;
    const codeRoute = !route || ["code_change", "artifact_generation", "operation", "multi_stage_plan"].includes(route);
    return codeRoute && COMPLEX_OOD_KEYWORDS.test(text);
}
export function createOodDesignSummary(taskId, intent) {
    return {
        task_id: taskId,
        status: "required",
        applicability_reason_zh: `该任务涉及复杂对象职责或依赖边界，编码前需补齐 OOD/SOLID 摘要: ${intent}`,
        objects: [],
        interfaces: [],
        dependency_direction: [],
        change_points: [],
        solid_risks: [],
    };
}
function hasCompleteDesignSummary(summary) {
    return !!summary
        && summary.objects.length > 0
        && summary.objects.every((item) => item.name.trim().length > 0 && item.responsibility.trim().length > 0)
        && summary.interfaces.length > 0
        && summary.dependency_direction.length > 0
        && summary.change_points.length > 0
        && summary.solid_risks.length > 0;
}
export function evaluateOodDesignGate(input) {
    if (!requiresOodSolidContract(input.intent, input.route, input.changed_files)) {
        return {
            applicable: false,
            allowed: true,
            status: "not_applicable",
            reason_zh: "低风险或非对象设计任务，无需引入 OOD/SOLID 工作包",
            findings: [],
        };
    }
    if (!hasCompleteDesignSummary(input.summary)) {
        return {
            applicable: true,
            allowed: false,
            status: "blocked",
            reason_zh: "复杂编码任务缺少完整 OOD/SOLID 设计摘要，不得开始实现",
            required_summary: input.summary ?? createOodDesignSummary(input.task_id, input.intent),
            findings: [],
        };
    }
    return {
        applicable: true,
        allowed: true,
        status: "satisfied",
        reason_zh: "OOD/SOLID 设计摘要已具备，可进入实现并在审查阶段复验",
        required_summary: { ...input.summary, status: "satisfied" },
        findings: [],
    };
}
function addFinding(findings, principle, severity, file, evidence, suggestion_zh) {
    findings.push({ principle, severity, file, evidence, suggestion_zh });
}
export function reviewSolidCode(fileContents) {
    const findings = [];
    for (const [file, content] of Object.entries(fileContents)) {
        const lowered = file.toLowerCase();
        if (/controller/.test(lowered)
            && /(?:repository\.|mapper\.|entitymanager|save\s*\(|@Transactional|calculate|compute)/i.test(content)) {
            addFinding(findings, "SRP", "hard_fail", file, "Controller 同时出现持久化、事务或业务计算职责", "将业务规则与持久化移入应用服务，Controller 仅处理协议适配、校验和响应映射。");
        }
        if (/(?:service|usecase|application)/.test(lowered)
            && /new\s+(?:RestTemplate|WebClient|HttpClient|Stripe|S3|OpenAI|Twilio|Axios|.*Repository)\s*\(/i.test(content)) {
            addFinding(findings, "DIP", "hard_fail", file, "业务服务直接构造外部依赖或仓储实现", "通过端口接口与依赖注入隔离基础设施实现。");
        }
        if (/interface\s+\w+[\s\S]{0,800}(?:upload|export|notify|delete|approve)[\s\S]{0,800}(?:upload|export|notify|delete|approve)/i.test(content)
            && /(?:UnsupportedOperationException|throw new Error\(['"]not implemented)/i.test(content)) {
            addFinding(findings, "ISP", "hard_fail", file, "实现被迫抛出未支持操作，接口职责过宽", "拆分为客户端真正需要的细粒度接口。");
        }
        if (/(?:extends|implements)\s+\w+[\s\S]{0,500}(?:UnsupportedOperationException|throw new Error\(['"]unsupported)/i.test(content)) {
            addFinding(findings, "LSP", "hard_fail", file, "子类型拒绝父契约操作", "重构抽象层级，确保替换后仍满足原契约。");
        }
        const branching = (content.match(/\b(?:if|else if|switch)\s*\([^)]*(?:type|status|kind|strategy)/gi) ?? []).length;
        if (branching >= 3) {
            addFinding(findings, "OCP", "warning", file, "同一变化维度出现多处分支扩展点", "用策略/处理器注册方式隔离新增类型，减少修改既有分支。");
        }
        if (/(?:component|page|\.tsx$|\.vue$)/.test(lowered)
            && /(?:fetch\(|axios\.|apiClient|request\()/i.test(content)
            && /(?:useState|useEffect|computed\(|ref\(|watch\()/i.test(content)
            && /(?:if\s*\(|switch\s*\()/i.test(content)) {
            addFinding(findings, "SRP", "hard_fail", file, "复杂 UI 组件混合远程调用、状态编排与业务判断", "拆分展示组件、状态 hook/composable 与 API client/业务服务。");
        }
    }
    return findings;
}
export function hasBlockingSolidFindings(findings) {
    return findings.some((finding) => finding.severity === "hard_fail");
}
export function evaluateOverdesignRisk(intent, summary) {
    if (!LOW_RISK_KEYWORDS.test(intent) || !summary || summary.objects.length < 4)
        return [];
    return [{
            principle: "OVERDESIGN",
            severity: "warning",
            file: "OOD设计摘要",
            evidence: "低风险修改引入多个对象与抽象边界",
            suggestion_zh: "保持最小实现，不为简单修改提前引入复杂对象结构。",
        }];
}
//# sourceMappingURL=ood_solid_contract.js.map