/**
 * Validation utilities for form components
 */
/**
 * Validate a single value against a rule
 */
export function validateField(value, rule, fieldName) {
    // Required check
    if (rule.required &&
        (value === null || value === undefined || value === "")) {
        return `${fieldName} is required`;
    }
    // Skip other validations if value is empty and not required
    if (!value && !rule.required) {
        return null;
    }
    // Type-specific validations
    if (typeof value === "string") {
        if (rule.minLength && value.length < rule.minLength) {
            return `${fieldName} must be at least ${rule.minLength} characters`;
        }
        if (rule.maxLength && value.length > rule.maxLength) {
            return `${fieldName} must be no more than ${rule.maxLength} characters`;
        }
        if (rule.pattern && !rule.pattern.test(value)) {
            return `${fieldName} format is invalid`;
        }
        if (rule.email) {
            const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailPattern.test(value)) {
                return `${fieldName} must be a valid email address`;
            }
        }
        if (rule.url) {
            try {
                new URL(value);
            }
            catch {
                return `${fieldName} must be a valid URL`;
            }
        }
    }
    if (typeof value === "number") {
        if (rule.min !== undefined && value < rule.min) {
            return `${fieldName} must be at least ${rule.min}`;
        }
        if (rule.max !== undefined && value > rule.max) {
            return `${fieldName} must be no more than ${rule.max}`;
        }
    }
    // Custom validation
    if (rule.custom) {
        const customError = rule.custom(value);
        if (customError) {
            return customError;
        }
    }
    return null;
}
/**
 * Validate form data against a schema
 */
export function validateForm(data, schema) {
    const errors = {};
    Object.keys(schema).forEach((fieldName) => {
        const rule = schema[fieldName];
        const value = data[fieldName];
        const error = validateField(value, rule, fieldName);
        if (error) {
            errors[fieldName] = error;
        }
    });
    return {
        isValid: Object.keys(errors).length === 0,
        errors,
    };
}
/**
 * Common validation patterns
 */
export const validationPatterns = {
    email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
    url: /^https?:\/\/.+/,
    phone: /^\+?[\d\s-()]+$/,
    slug: /^[a-z0-9]+(?:-[a-z0-9]+)*$/,
    alphanumeric: /^[a-zA-Z0-9]+$/,
};
