export const supportsHoverInteractions = (): boolean => {
  if (
    typeof window === "undefined" ||
    typeof window.matchMedia !== "function"
  ) {
    return true;
  }

  return window.matchMedia(
    "(hover: hover) and (pointer: fine), (any-hover: hover) and (any-pointer: fine)",
  ).matches;
};

export const isFocusVisibleTarget = (target: EventTarget | null): boolean => {
  if (!(target instanceof Element) || typeof target.matches !== "function") {
    return true;
  }

  try {
    return target.matches(":focus-visible");
  } catch {
    return true;
  }
};

export const shouldHandleHoverPointer = (
  pointerType?: string | null,
): boolean => {
  if (!supportsHoverInteractions()) {
    return false;
  }

  return pointerType !== "touch";
};
