// src/utils/stringUtils.ts

/**
 * Sanitizes a string to be used as a safe filename.
 * Replaces spaces and multiple hyphens with a single hyphen,
 * removes characters that are problematic in URLs and file systems,
 * and converts to lowercase.
 * @param filename The string to sanitize.
 * @returns A sanitized string suitable for use as a filename.
 */
export function sanitizeFilename(filename: string): string {
  return filename.trim() // Remove leading/trailing whitespace first
    .replace(/\s+/g, '-') // Then, replace internal spaces with a single hyphen
    .replace(/[^a-zA-Z0-9-._]/g, '') // Remove all non-alphanumeric characters except for hyphens, underscores, and periods
    .replace(/-+/g, '-'); // Replace multiple hyphens with a single one
}