import { DEFAULT_TOOL_GOVERNANCE_RULES } from './rules.js';
import { governRequestWithNative, governResponseWithNative } from '../../../router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.js';
/**
 * Hybrid governance engine:
 * - request path: native-primary
 * - response path: native-primary
 *
 * Legacy-only snapshot retained at:
 * - src/conversion/hub/tool-governance/archive/engine.legacy.ts
 */
export class ToolGovernanceError extends Error {
    protocol;
    direction;
    field;
    constructor(message, protocol, direction, field) {
        super(message);
        this.protocol = protocol;
        this.direction = direction;
        this.field = field;
        this.name = 'ToolGovernanceError';
    }
}
function normalizeProtocol(protocol) {
    if (!protocol)
        return 'openai-chat';
    const normalized = String(protocol).toLowerCase();
    if (normalized === 'anthropic-messages') {
        return 'anthropic';
    }
    if (normalized === 'gemini-chat') {
        return 'gemini';
    }
    if (normalized === 'responses' || normalized === 'openai-responses') {
        return 'openai-responses';
    }
    if (normalized === 'openai-chat') {
        return 'openai-chat';
    }
    return protocol;
}
export class ToolGovernanceEngine {
    registry;
    constructor(registry = DEFAULT_TOOL_GOVERNANCE_RULES) {
        this.registry = registry;
    }
    governRequest(request, protocol) {
        const rules = this.resolveRules(protocol, 'request');
        if (!rules) {
            return {
                request,
                summary: buildSummary(protocol, 'request', false)
            };
        }
        let governed;
        try {
            governed = governRequestWithNative({
                request: request,
                protocol,
                registry: this.registry
            });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error ?? 'unknown error');
            if (message.includes('Tool name exceeds max length')) {
                throw new ToolGovernanceError(message, protocol, 'request', 'tool.function.name');
            }
            throw error;
        }
        return {
            request: governed.request,
            summary: governed.summary
        };
    }
    governResponse(payload, protocol) {
        const rules = this.resolveRules(protocol, 'response');
        if (!rules) {
            return {
                payload,
                summary: buildSummary(protocol, 'response', false)
            };
        }
        let governed;
        try {
            governed = governResponseWithNative({
                payload: payload,
                protocol,
                registry: this.registry
            });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error ?? 'unknown error');
            if (message.includes('Tool name exceeds max length')) {
                throw new ToolGovernanceError(message, protocol, 'response', 'tool.function.name');
            }
            throw error;
        }
        return {
            payload: governed.payload,
            summary: governed.summary
        };
    }
    resolveRules(protocol, direction) {
        const resolved = normalizeProtocol(protocol);
        const entry = this.registry[resolved] ?? this.registry['openai-chat'];
        return entry?.[direction];
    }
}
function buildSummary(protocol, direction, applied) {
    return {
        protocol,
        direction,
        applied,
        sanitizedNames: 0,
        truncatedNames: 0,
        defaultedNames: 0,
        timestamp: Date.now()
    };
}
