export const buildQueryString = (params: Record<string, unknown>): string => {
  const filteredParams = Object.keys(params).filter(
    (key) =>
      params[key] !== undefined && params[key] !== null && params[key] !== ""
  );

  if (filteredParams.length === 0) {
    return "";
  }

  return (
    "?" +
    filteredParams
      ?.map((key) => `${key}=${encodeURIComponent(String(params[key]))}`)
      .join("&")
  );
};

// export const slugify = (text: string) => {
//   if (!text) return "";
//   return text.toLowerCase().replace(/\s+/g, "-");
// };

export const formatTitle = (key: string): string => {
  if (!key) return "";
  let result = key.replace(/_/g, " ");
  result = result.replace(/([a-z])([A-Z])/g, "$1 $2");
  return result.toUpperCase().trim();
};

export const formatDuration = (
  totalMinutes: number | null | undefined
): string => {
  if (!totalMinutes || totalMinutes <= 0) {
    return "";
  }
  const hours = Math.floor(totalMinutes / 60);
  const minutes = totalMinutes % 60;
  const hoursPart = hours > 0 ? `${hours}h` : "";
  const minutesPart = minutes > 0 ? `${minutes}m` : "";

  return `${hoursPart} ${minutesPart}`.trim();
};

export const truncateWords = (text: string, wordLimit: number) => {
  if (!text) return "";
  const words = text.split(" ");
  return words.length > wordLimit
    ? words.slice(0, wordLimit).join(" ") + "..."
    : text;
};
