/**
 * 问题四十九: Architecture Design Contract — 架构设计工作流契约。
 *
 * 强制执行:
 * - architecture design 必须按 Architecture Design Contract 产出
 * - 输出目录名必须用户确认，未确认不得 delivered
 * - 后端默认优先 Java，前端默认优先最新稳定 Vue 生态
 * - 多端前端默认不同项目仓库
 * - Mermaid sequenceDiagram 必须可渲染
 */
/** 默认前端技术栈 */
export const DEFAULT_FRONTEND_STACK = {
    framework: "latest_stable_vue",
    language: "typescript",
    build_tool: "vite",
    router: "vue_router",
    state_management: "pinia",
    unit_test: "vitest",
    e2e_test: "playwright",
    lint_format: "eslint_prettier",
    api_client: "typed_fetch_client",
    ui_library_policy: "recommend_by_project_type",
};
/** 默认技术策略 */
export const DEFAULT_TECHNOLOGY_POLICY = {
    backend_preferred: "java",
    frontend_preferred: "latest_stable_vue_ecosystem",
    frontend_default_stack: DEFAULT_FRONTEND_STACK,
    allow_override: true,
    override_evidence_refs: [],
};
/**
 * 创建架构设计契约。
 */
export function createArchitectureDesignContract(overrides) {
    return {
        workflow_id: "architecture_design",
        architecture_design_status: "draft",
        input_set: {
            task_ids: [],
            required_source_artifacts: ["prototype_description"],
            consumed_files: [],
            missing_required_files: [],
            all_clients_covered: true,
        },
        output_directory_decision: {
            recommended_name: "",
            recommendation_reason_zh: "",
            architecture_output_directory_status: "recommended",
        },
        template_id: "architecture_design",
        technology_policy: DEFAULT_TECHNOLOGY_POLICY,
        frontend_repository_policy: {
            default_strategy: "separate_repo_per_client",
            clients: [],
            repository_plan: [],
            merge_requires_user_confirmation: true,
        },
        uncertainty_decisions: [],
        mermaid_diagram_requirements: [],
        review: {
            review_status: "manual_required",
            missing_sections: [],
            missing_inputs: [],
            failed_rules: [],
            manual_required_items: [],
            repair_required: false,
        },
        evidence_refs: [],
        ...overrides,
    };
}
/**
 * 验证架构输入集完整性。
 */
export function validateArchitectureInputs(inputSet) {
    const missing = [];
    if (inputSet.task_ids.length === 0) {
        missing.push("缺少 task_ids");
    }
    if (inputSet.missing_required_files.length > 0) {
        missing.push(`缺少必要文件: ${inputSet.missing_required_files.join(", ")}`);
    }
    if (!inputSet.all_clients_covered) {
        missing.push("未覆盖所有客户端");
    }
    return { valid: missing.length === 0, missing };
}
/**
 * 验证输出目录决策 — 未确认不得 delivered。
 */
export function validateOutputDirectoryDecision(decision, contractStatus) {
    if (contractStatus === "delivered" && decision.architecture_output_directory_status !== "confirmed") {
        return { allowed: false, reason: "输出目录名未经用户确认，不得标记为 delivered" };
    }
    if (!decision.recommended_name) {
        return { allowed: false, reason: "必须提供推荐目录名" };
    }
    return { allowed: true, reason: "输出目录决策合规" };
}
/**
 * 验证 Mermaid 图表 — 必须可渲染。
 */
export function validateMermaidDiagrams(diagrams) {
    const failures = [];
    for (const diagram of diagrams) {
        if (diagram.render_check_status === "failed") {
            failures.push(`图表 ${diagram.diagram_id} 渲染失败`);
        }
        if (!diagram.mermaid_source || diagram.mermaid_source.trim().length === 0) {
            failures.push(`图表 ${diagram.diagram_id} 缺少 Mermaid 源码`);
        }
        // 基本语法检查
        if (diagram.diagram_type === "sequenceDiagram" && !diagram.mermaid_source.includes("->")) {
            failures.push(`图表 ${diagram.diagram_id} 的 sequenceDiagram 缺少消息箭头`);
        }
    }
    return { allRenderable: failures.length === 0, failures };
}
/**
 * 执行架构设计审查。
 */
export function reviewArchitectureDesign(contract) {
    const failedRules = [];
    const missingSections = [];
    const missingInputs = [];
    const manualRequired = [];
    // 输入检查
    const inputCheck = validateArchitectureInputs(contract.input_set);
    if (!inputCheck.valid) {
        missingInputs.push(...inputCheck.missing);
    }
    // 输出目录检查
    const dirCheck = validateOutputDirectoryDecision(contract.output_directory_decision, contract.architecture_design_status);
    if (!dirCheck.allowed) {
        failedRules.push(dirCheck.reason);
    }
    // Mermaid 检查
    if (contract.mermaid_diagram_requirements.length > 0) {
        const mermaidCheck = validateMermaidDiagrams(contract.mermaid_diagram_requirements);
        if (!mermaidCheck.allRenderable) {
            failedRules.push(...mermaidCheck.failures);
        }
    }
    // 不确定性决策检查
    for (const ud of contract.uncertainty_decisions) {
        if (ud.user_confirmation_required && !ud.user_confirmation_ref) {
            manualRequired.push(`不确定性决策 ${ud.decision_id} 需要用户确认`);
        }
    }
    // 前端仓库策略检查
    if (contract.frontend_repository_policy.merge_requires_user_confirmation &&
        !contract.frontend_repository_policy.merge_confirmation_ref &&
        contract.frontend_repository_policy.clients.length > 1) {
        manualRequired.push("多端前端仓库合并需要用户确认");
    }
    const review = {
        review_status: failedRules.length > 0 ? "failed"
            : manualRequired.length > 0 ? "manual_required"
                : "passed",
        missing_sections: missingSections,
        missing_inputs: missingInputs,
        failed_rules: failedRules,
        manual_required_items: manualRequired,
        repair_required: failedRules.length > 0,
    };
    return { passed: review.review_status === "passed", review };
}
//# sourceMappingURL=architecture_design_contract.js.map