/**
 * Toolkit - Text Formatting
 * 
 * Utilities for text manipulation and formatting.
 */

/**
 * Capitalize first letter of a string
 */
export function capitalize(str: string): string {
  if (!str) return str;
  return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}

/**
 * Convert string to title case
 */
export function toTitleCase(str: string): string {
  return str
    .split(" ")
    .map(word => capitalize(word))
    .join(" ");
}

/**
 * Truncate string with ellipsis
 */
export function truncate(str: string, maxLength: number, suffix = "..."): string {
  if (str.length <= maxLength) return str;
  return str.slice(0, maxLength - suffix.length) + suffix;
}

/**
 * Convert camelCase or PascalCase to human-readable text
 */
export function humanize(str: string): string {
  return str
    .replace(/([A-Z])/g, " $1") // Add space before capital letters
    .replace(/^./, match => match.toUpperCase()) // Capitalize first letter
    .trim();
}

/**
 * Generate initials from a name
 */
export function getInitials(name: string, maxLength = 2): string {
  const parts = name.trim().split(/\s+/);
  const initials = parts.map(part => part.charAt(0).toUpperCase());
  return initials.slice(0, maxLength).join("");
}

/**
 * Mask sensitive data (e.g., email, phone)
 */
export function maskString(str: string, visibleStart = 3, visibleEnd = 3, maskChar = "*"): string {
  if (str.length <= visibleStart + visibleEnd) {
    return str;
  }
  
  const start = str.slice(0, visibleStart);
  const end = str.slice(-visibleEnd);
  const maskedLength = str.length - visibleStart - visibleEnd;
  const masked = maskChar.repeat(maskedLength);
  
  return `${start}${masked}${end}`;
}

