"use client";
import { useState, useEffect } from "react";
let systemListenerInitialized = false;
const COOKIE_NAME = "color-mode";
const COOKIE_OPTIONS = {
  path: "/",
  maxAge: 60 * 60 * 24 * 365,
  // 1 year
  sameSite: "lax"
};
function getColorModeFromCookie() {
  if (typeof document === "undefined") return null;
  let match = document.cookie.match(
    new RegExp(`(?:^|; )${COOKIE_NAME}=([^;]*)`)
  );
  if (!match) {
    match = document.cookie.match(/(?:^|; )theme=([^;]*)/);
  }
  if (!match) return null;
  try {
    const decoded = decodeURIComponent(match[1] || "");
    return JSON.parse(decoded);
  } catch {
    return null;
  }
}
function setColorModeCookie(state) {
  if (typeof document === "undefined") return;
  const value = JSON.stringify(state);
  let cookieString = `${COOKIE_NAME}=${encodeURIComponent(value)}`;
  cookieString += `; path=${COOKIE_OPTIONS.path}`;
  cookieString += `; max-age=${COOKIE_OPTIONS.maxAge}`;
  cookieString += `; samesite=${COOKIE_OPTIONS.sameSite}`;
  document.cookie = cookieString;
}
function getSystemColorMode() {
  if (typeof window === "undefined") return "light";
  return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
function applyColorMode(mode) {
  if (typeof document === "undefined") return;
  if (mode === "dark") {
    document.documentElement.classList.add("dark");
  } else {
    document.documentElement.classList.remove("dark");
  }
  document.documentElement.style.colorScheme = mode;
}
function setupSystemListener() {
  if (systemListenerInitialized || typeof window === "undefined") return;
  systemListenerInitialized = true;
  const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
  mediaQuery.addEventListener("change", () => {
    const stored = getColorModeFromCookie();
    if (stored?.preference !== "system") return;
    const mode = mediaQuery.matches ? "dark" : "light";
    const newState = { preference: "system", mode };
    setColorModeCookie(newState);
    applyColorMode(mode);
    window.dispatchEvent(
      new CustomEvent("color-mode-change", { detail: newState })
    );
  });
}
function useColorMode() {
  const [state, setState] = useState(() => {
    const saved = getColorModeFromCookie();
    if (saved) return saved;
    const mode = getSystemColorMode();
    return { preference: "system", mode };
  });
  function setPreference(newPreference) {
    const mode = newPreference === "system" ? getSystemColorMode() : newPreference;
    const newState = { preference: newPreference, mode };
    setColorModeCookie(newState);
    applyColorMode(mode);
    window.dispatchEvent(
      new CustomEvent("color-mode-change", {
        detail: newState
      })
    );
  }
  useEffect(() => {
    setupSystemListener();
    const handleColorModeChange = (event) => {
      setState(event.detail);
    };
    window.addEventListener(
      "color-mode-change",
      handleColorModeChange
    );
    return () => window.removeEventListener(
      "color-mode-change",
      handleColorModeChange
    );
  }, []);
  return {
    preference: state.preference,
    mode: state.mode,
    setPreference
  };
}
export {
  useColorMode
};
//# sourceMappingURL=use-color-mode.js.map
