/**
 * KRA - String Utility Functions
 * String manipulation utilities
 */

/**
 * Capitalize first letter of string
 * @param str - Input string
 * @returns String with first letter capitalized
 */
export const toUpperCase = (str: string): string => {
  if (str && str[0]) {
    return str.replace(str[0], str[0].toUpperCase());
  }
  return '';
};

/**
 * Lowercase first letter of string
 * @param str - Input string
 * @returns String with first letter lowercased
 */
export const toLowerCase = (str: string): string => {
  if (str && str[0]) {
    return str.replace(str[0], str[0].toLowerCase());
  }
  return '';
};

/**
 * Convert camelCase to snake_case
 * @param str - CamelCase string
 * @returns snake_case string
 */
export const toSQLLine = (str: string): string => {
  if (str === 'ID') return 'ID';
  return str.replace(/([A-Z])/g, '_$1').toLowerCase();
};

/**
 * Convert snake_case to camelCase
 * @param name - snake_case string
 * @returns camelCase string
 */
export const toHump = (name: string): string => {
  return name.replace(/_(\w)/g, (_all, letter) => {
    return letter.toUpperCase();
  });
};

/**
 * Trim whitespace from string
 * @param str - Input string
 * @returns Trimmed string
 */
export const trim = (str: string): string => {
  return str?.trim() || '';
};

/**
 * Check if string is empty or whitespace only
 * @param str - Input string
 * @returns True if empty or whitespace
 */
export const isEmpty = (str: string | null | undefined): boolean => {
  return !str || str.trim().length === 0;
};

/**
 * Truncate string with ellipsis
 * @param str - Input string
 * @param maxLength - Maximum length
 * @returns Truncated string
 */
export const truncate = (str: string, maxLength: number): string => {
  if (!str || str.length <= maxLength) return str || '';
  return str.slice(0, maxLength) + '...';
};

export default {
  toUpperCase,
  toLowerCase,
  toSQLLine,
  toHump,
  trim,
  isEmpty,
  truncate,
};
