import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import type { IGeneralError } from "./types";

/**
 * Merges class names with tailwind-merge and clsx
 */
export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

/**
 * Concatenates truthy classes into a space-separated string.
 *
 * @param classes - The classes to concatenate.
 * @returns The concatenated classes.
 */
export const clx = (...classes: (string | boolean | undefined)[]): string => {
  return twMerge(classes.filter(Boolean).join(" "));
};

export const extractErrorMessage = (
  error: Error | unknown,
  fallbackMessage: string = "خطایی رخ داده است"
): string => {
  try {
    const axiosError = error as { response?: { data: IGeneralError } };
    return axiosError?.response?.data?.error?.message?.[0] || fallbackMessage;
  } catch {
    return fallbackMessage;
  }
};
