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

/**
 * Merges Tailwind class names with conflict resolution. The canonical
 * helper used across every component in this library — passing
 * conditional classes through `cn()` ensures later-applied utilities
 * override earlier ones predictably.
 */
export function cn(...inputs: ClassValue[]): string {
  return twMerge(clsx(inputs))
}

/**
 * Compile-time + runtime assertion that a value is never reached.
 * Useful in exhaustive `switch` statements over union discriminants
 * (e.g. element / card type tags) so adding a new variant becomes a
 * TypeScript error until every consumer handles it.
 */
export function assertNever(value: never, message?: string): never {
  throw new Error(message ?? `Unexpected value: ${String(value)}`)
}

/**
 * Type predicate for objects that have a `[key]` property — handy when
 * narrowing untrusted JSON before reading from it.
 */
export function hasKey<K extends PropertyKey>(
  obj: unknown,
  key: K,
): obj is Record<K, unknown> {
  return typeof obj === "object" && obj !== null && key in obj
}
