"use strict";
/**
 * Tool filtering utilities with wildcard and pattern support
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseToolPatterns = parseToolPatterns;
exports.matchesPattern = matchesPattern;
exports.filterTools = filterTools;
exports.createToolFilter = createToolFilter;
/**
 * Parse tool patterns from a string (comma or space separated)
 * Supports wildcards (*) and specific patterns like "Bash(git:*)"
 */
function parseToolPatterns(input) {
    if (!input)
        return [];
    // Split by comma or space, filter empty strings
    const patterns = input
        .split(/[,\s]+/)
        .map(p => p.trim())
        .filter(p => p.length > 0);
    return patterns.map(pattern => {
        // Convert pattern to regex
        let regexPattern = pattern
            // Escape special regex characters except * and ()
            .replace(/[.+?^${}|[\]\\]/g, '\\$&')
            // Convert * to .* for wildcard matching
            .replace(/\*/g, '.*')
            // Handle parentheses patterns like Bash(git:*)
            .replace(/\(/g, '\\(')
            .replace(/\)/g, '\\)');
        // Ensure exact match (not partial)
        regexPattern = `^${regexPattern}$`;
        return {
            pattern,
            regex: new RegExp(regexPattern, 'i')
        };
    });
}
/**
 * Check if a tool name matches any of the patterns
 */
function matchesPattern(toolName, patterns) {
    if (patterns.length === 0)
        return false;
    return patterns.some(p => p.regex.test(toolName));
}
/**
 * Filter tool list based on allowed and disallowed patterns
 * Disallowed patterns take precedence over allowed patterns
 */
function filterTools(tools, allowedPatterns, disallowedPatterns) {
    return tools.filter(tool => {
        // If disallowed, reject immediately
        if (matchesPattern(tool, disallowedPatterns)) {
            return false;
        }
        // If no allowed patterns specified, allow all (except disallowed)
        if (allowedPatterns.length === 0) {
            return true;
        }
        // Check if matches allowed patterns
        return matchesPattern(tool, allowedPatterns);
    });
}
/**
 * Create a tool filter function for use in tool registries
 */
function createToolFilter(allowedInput, disallowedInput) {
    const allowedPatterns = parseToolPatterns(allowedInput);
    const disallowedPatterns = parseToolPatterns(disallowedInput);
    return (toolName) => {
        // Disallowed takes precedence
        if (matchesPattern(toolName, disallowedPatterns)) {
            return false;
        }
        // If no allowed patterns, allow all (except disallowed)
        if (allowedPatterns.length === 0) {
            return true;
        }
        // Must match at least one allowed pattern
        return matchesPattern(toolName, allowedPatterns);
    };
}
/**
 * Examples of supported patterns:
 * - "mcp__*" - All MCP tools
 * - "mcp__filesystem__*" - All filesystem MCP tools
 * - "Bash" - Exact match for Bash tool
 * - "Bash(git:*)" - Bash tool with git commands (if supported by tool)
 * - "Edit,Read" - Multiple specific tools
 * - "mcp__* Edit Read" - Mix of wildcards and specific tools
 */ 
//# sourceMappingURL=tool-filter.js.map