import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
	return twMerge(clsx(inputs));
}

export function toTitleCase(str: string) {
	return str.replace(/\b\w/g, (char) => char.toUpperCase());
}

namespace DiffUtils {
	export function getDifferences<T extends object>(
		obj1: T,
		obj2: T,
	): Partial<T> {
		return Object.entries(obj2).reduce((diff, [key, value]) => {
			if (
				JSON.stringify(obj1[key as keyof T]) !==
				JSON.stringify(obj2[key as keyof T])
			) {
				diff[key as keyof T] = value as T[keyof T];
			}
			return diff;
		}, {} as Partial<T>);
	}

	export function isEmpty<T extends object>(obj: T) {
		return Object.keys(obj).length === 0;
	}
}

// Make it globally available
if (typeof window !== "undefined") {
	(window as any).DiffUtils = DiffUtils;
} else {
	(global as any).DiffUtils = DiffUtils;
}

export default DiffUtils;
