import { signOut } from "next-auth/react";

/**
 * Humanizes a string by replacing underscores/hyphens with spaces 
 * and converting to Title Case.
 * Example: 'HR_ADMIN' -> 'Hr Admin'
 */
export function humanize(str: string | undefined | null): string {
  if (!str) return "";

  return str
    .toLowerCase()
    .replace(/[-_]/g, " ")
    .replace(/\b\w/g, (char) => char.toUpperCase());
}

/**
 * Formats a date string into a human-readable format.
 * Example: '2023-01-01' -> 'Jan 1, 2023'
 */
export function formatDate(dateStr: string | Date | undefined | null): string {
  if (!dateStr) return "-";

  const date = new Date(dateStr);

  // Check if the date is valid
  if (isNaN(date.getTime())) {
    return "-";
  }

  return new Intl.DateTimeFormat("en-US", {
    month: "short",
    day: "numeric",
    year: "numeric",
  }).format(date);
}

/**
 * Global LOGOUT Functionality
 */
export const handleLogout = () => signOut({ callbackUrl: "/login" })
