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

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

/**
 * Shallow equality check for two plain objects. Returns true if the objects
 * have the same keys and all primitive/Date values are strictly equal.
 */
export function shallowEqual(a?: Record<string, unknown> | null, b?: Record<string, unknown> | null) {
  if (!a || !b) {
    return false
  }
  const aKeys = Object.keys(a)
  const bKeys = Object.keys(b)
  if (aKeys.length !== bKeys.length) {
    return false
  }
  for (const key of aKeys) {
    const av = (a as any)[key]
    const bv = (b as any)[key]
    if (av instanceof Date && bv instanceof Date) {
      if (av.getTime() !== bv.getTime()) {
        return false
      }
    } else if (av !== bv) {
      return false
    }
  }
  return true
}
