import { ESiteTheme } from "@/hooks/theme/useTheme";

export function getSystemColorScheme(): ESiteTheme {
  if (typeof window !== "undefined" && window.matchMedia) {
    return window.matchMedia("(prefers-color-scheme: dark)").matches
      ? ESiteTheme.Dark
      : ESiteTheme.Light;
  }

  return ESiteTheme.Light;
}

export function subscribeToSystemThemeChange(
  callback: (theme: ESiteTheme) => void
) {
  if (typeof window === "undefined" || !window.matchMedia) {
    return;
  }

  const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");

  const handleChange = (e: MediaQueryListEvent) => {
    callback(e.matches ? ESiteTheme.Dark : ESiteTheme.Light);
  };

  mediaQuery.addEventListener("change", handleChange);

  return () => {
    mediaQuery.removeEventListener("change", handleChange);
  };
}
