/**
 * Cache for compiled glob patterns to avoid recompiling on every match
 */
const globPatternCache = new Map();
/**
 * Convert a glob pattern to a RegExp
 */
function compileGlobPattern(pattern) {
    const cached = globPatternCache.get(pattern);
    if (cached) {
        return (cached);
    }
    // Convert glob pattern to regex
    // Replace * with regex pattern for "anything except /"
    const regexPattern = pattern
        .replace(/[.+?^${}()|[\]\\]/g, '\\$&') // Escape special regex chars except *
        .replace(/\*/g, '[^/]+'); // Replace * with "one or more non-slash chars"
    const regex = new RegExp(`^${regexPattern}$`);
    globPatternCache.set(pattern, regex);
    return (regex);
}
/**
 * Checks if a path matches a validator's pattern
 */
export function matchesPattern(path, pattern) {
    if (pattern instanceof RegExp) {
        // Reset lastIndex to handle RegExps with global/sticky flags
        pattern.lastIndex = 0;
        return (pattern.test(path));
    }
    const regex = compileGlobPattern(pattern);
    return (regex.test(path));
}
/**
 * Finds all validators that match a given path
 */
export function findMatchingValidators(path, validators) {
    return (validators.filter(function (validator) {
        return (matchesPattern(path, validator.pathPattern));
    }));
}
/**
 * Checks if a path requires validation (matches any validator pattern)
 */
export function requiresValidation(path, validators) {
    return (validators.some(function (validator) {
        return (matchesPattern(path, validator.pathPattern));
    }));
}
/**
 * Abstract base class for content validators.
 * Provides common validation logic for mime type and size checking.
 * Subclasses should override validateContent for custom validation.
 */
export class ContentValidator {
    async validate(path, content, mimeType) {
        // Check mime type
        if (!this.allowedMimeTypes.includes(mimeType.toLowerCase())) {
            return ({
                valid: false,
                error: `Invalid mime type: ${mimeType}. Allowed: ${this.allowedMimeTypes.join(', ')}`
            });
        }
        // Check size
        if (content.length > this.maxSize) {
            return ({
                valid: false,
                error: `Content too large: ${content.length} bytes. Maximum: ${this.maxSize} bytes`
            });
        }
        // Call subclass-specific validation
        return (await this.validateContent(path, content, mimeType));
    }
    /**
     * Override to add custom content validation beyond mime type and size checks.
     * Default implementation accepts all content that passes basic checks.
     */
    validateContent(_ignorePath, _ignoreContent, _ignoreMimeType) {
        return (Promise.resolve({ valid: true }));
    }
}
//# sourceMappingURL=validators.js.map