/**
 * 问题四十二: Brainstorm / Option Exploration — 脑暴与方案探索契约。
 *
 * 强制执行:
 * - brainstorm 不能停留在闲聊，必须形成候选方案、取舍、反证、推荐与非目标
 * - 脑暴推荐不得绕过 Decision Sovereignty human gate
 */
/** 脑暴触发条件对应的最低选项数量要求 */
const MIN_OPTIONS_BY_TRIGGER = {
    high_impact_change: 2,
    mechanism_design: 2,
    technology_choice: 3,
    user_project_architecture: 2,
    migration_plan: 2,
    ambiguous_goal: 2,
    rework_risk: 2,
};
/**
 * 创建脑暴会话。
 */
export function createBrainstormSession(overrides) {
    return {
        trigger: "ambiguous_goal",
        options: [],
        recommended_option_id: "",
        rejected_option_ids: [],
        human_gate_required: false,
        evidence_refs: [],
        brainstorm_status: "not_required",
        ...overrides,
    };
}
/**
 * 创建候选方案记录。
 */
export function createOptionRecord(overrides) {
    return {
        description: "",
        pros: [],
        cons: [],
        applicable_when: [],
        not_applicable_when: [],
        failure_conditions: [],
        verification_method: [],
        estimated_risk: "medium",
        reversibility: "reversible",
        required_human_decisions: [],
        ...overrides,
    };
}
/**
 * 验证脑暴会话完整性。
 * - 必须有候选方案
 * - 必须有取舍分析
 * - 必须有推荐方案
 * - 必须有被否决的方案及原因
 */
export function validateBrainstormSession(session) {
    const violations = [];
    const minOptions = MIN_OPTIONS_BY_TRIGGER[session.trigger] ?? 2;
    if (session.options.length < minOptions) {
        violations.push(`脑暴必须包含至少 ${minOptions} 个候选方案，当前 ${session.options.length} 个`);
    }
    if (!session.recommended_option_id) {
        violations.push("脑暴必须形成推荐方案");
    }
    if (session.recommended_option_id && !session.options.some(o => o.option_id === session.recommended_option_id)) {
        violations.push("推荐方案不在候选方案列表中");
    }
    // 检查每个候选方案的完整性
    for (const opt of session.options) {
        if (opt.pros.length === 0 && opt.cons.length === 0) {
            violations.push(`候选方案 ${opt.option_id} 缺少取舍分析（pros/cons）`);
        }
        if (opt.failure_conditions.length === 0) {
            violations.push(`候选方案 ${opt.option_id} 缺少失败条件（反证）`);
        }
    }
    // 检查被否决方案是否有原因
    for (const rejectedId of session.rejected_option_ids) {
        const opt = session.options.find(o => o.option_id === rejectedId);
        if (!opt) {
            violations.push(`被否决方案 ${rejectedId} 不在候选方案列表中`);
        }
    }
    const maxScore = 5;
    const score = Math.max(0, maxScore - violations.length);
    const result = {
        valid: violations.length === 0,
        violations,
        completeness_score: score / maxScore,
    };
    if (!result.valid) {
        result.expected_schema = {
            trigger: "uncertainty | high_impact | detail_intensive",
            options: [{ option_id: "string", title: "string", description: "string", pros: ["string"], cons: ["string"] }],
            recommended_option_id: "string — 必须匹配 options[].option_id",
            rejected_option_ids: ["string — 必须在 options 列表中"],
            failure_conditions: ["string — 每个方案的失败条件"],
        };
    }
    return result;
}
/**
 * 创建取舍矩阵。
 */
export function createTradeoffMatrix(overrides, options) {
    return {
        option_scores: options.map(opt => ({
            option_id: opt.option_id,
            scores: Object.fromEntries(overrides.criteria?.map(c => [c, "unknown"]) ?? []),
            rationale_zh: "",
        })),
        why_selected_zh: "",
        why_rejected_zh: [],
        ...overrides,
    };
}
/**
 * 评估脑暴推荐是否可以绕过 Decision Sovereignty。
 * 返回 false 表示脑暴推荐不能直接执行，必须经过决策主权流程。
 */
export function canBrainstormAutoExecute(_session) {
    // 脑暴推荐永远不能绕过 Decision Sovereignty human gate
    // 即使 human_gate_required = false，仍然需要经过决策流程
    return false;
}
/**
 * 推进脑暴状态。
 */
export function advanceBrainstormStatus(session, targetStatus) {
    const validTransitions = {
        not_required: ["exploring"],
        exploring: ["options_ready", "not_required"],
        options_ready: ["selected", "human_required", "exploring"],
        selected: [],
        human_required: ["selected", "rejected"],
        rejected: [],
    };
    const allowed = validTransitions[session.brainstorm_status]?.includes(targetStatus) ?? false;
    return {
        allowed,
        reason: allowed ? "合法转移" : `${session.brainstorm_status} → ${targetStatus} 不允许`,
    };
}
//# sourceMappingURL=brainstorm_contract.js.map