// modules/react/themes/theme-provider/index.tsx
import { createContext, useContext, useState as useState2, useEffect as useEffect2, useRef as useRef2 } from "react";

// modules/react/hooks/useBroadcastChannel/index.tsx
import { useEffect, useState, useRef, useCallback } from "react";
var useBroadcastChannel = (channelName) => {
  const [message, setMessage] = useState(null);
  const channelRef = useRef(null);
  useEffect(() => {
    if (!channelRef.current && typeof window !== "undefined") {
      channelRef.current = new BroadcastChannel(channelName);
    }
    const channel = channelRef.current;
    if (!channel) return;
    const handleMessage = (event) => {
      setMessage(event.data);
    };
    channel.onmessage = handleMessage;
    return () => {
      if (channelRef.current) {
        channelRef.current.close();
        channelRef.current = null;
      }
    };
  }, [channelName]);
  const sendMessage = useCallback((msg) => {
    if (channelRef.current) {
      channelRef.current.postMessage(msg);
    }
  }, []);
  return { message, sendMessage };
};

// modules/react/themes/theme-provider/index.tsx
import { jsx } from "react/jsx-runtime";
var ThemeContext = createContext({ theme: "light", handleThemeChange: () => {
} });
function getCookie(name) {
  if (typeof document === "undefined") return null;
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) {
    return parts.pop()?.split(";").shift() || null;
  }
  return null;
}
var ThemeProvider = ({
  children,
  initialTheme
}) => {
  const { message: themeMessage, sendMessage } = useBroadcastChannel("theme_channel");
  const lastSentRef = useRef2(null);
  const [theme, setTheme] = useState2(() => {
    if (initialTheme) return initialTheme;
    if (typeof window !== "undefined") {
      const windowData = window.__FW_DATA__;
      if (windowData?.theme) return windowData.theme;
    }
    if (typeof window !== "undefined") {
      const cookieTheme = getCookie("theme");
      if (cookieTheme) return cookieTheme;
    }
    return "light";
  });
  useEffect2(() => {
    if (!themeMessage) return;
    if (themeMessage === lastSentRef.current) {
      lastSentRef.current = null;
      return;
    }
    setTheme((currentTheme) => {
      if (themeMessage !== currentTheme) {
        if (typeof document !== "undefined") {
          document.cookie = `theme=${themeMessage}; path=/; max-age=31536000`;
        }
        if (typeof window !== "undefined") {
          if (!window.__FW_DATA__) {
            window.__FW_DATA__ = {};
          }
          window.__FW_DATA__ = {
            ...window.__FW_DATA__,
            theme: themeMessage
          };
        }
        return themeMessage;
      }
      return currentTheme;
    });
  }, [themeMessage]);
  useEffect2(() => {
    const handleDataRefresh = () => {
      if (typeof window !== "undefined") {
        const windowData = window.__FW_DATA__;
        if (windowData?.theme) {
          setTheme((currentTheme) => {
            if (windowData.theme !== currentTheme) {
              return windowData.theme;
            }
            return currentTheme;
          });
        }
      }
    };
    if (typeof window !== "undefined") {
      window.addEventListener("fw-data-refresh", handleDataRefresh);
      return () => {
        window.removeEventListener("fw-data-refresh", handleDataRefresh);
      };
    }
  }, []);
  useEffect2(() => {
    if (initialTheme && initialTheme !== theme) {
      setTheme(initialTheme);
    }
  }, [initialTheme]);
  useEffect2(() => {
    if (typeof document === "undefined") return;
    const body = document.body;
    const currentClasses = body.className.split(" ").filter(Boolean);
    const themeClasses = ["light", "dark"];
    const filteredClasses = currentClasses.filter(
      (cls) => !themeClasses.includes(cls)
    );
    const newClassName = [...filteredClasses, theme].filter(Boolean).join(" ");
    if (body.className !== newClassName) {
      body.className = newClassName;
    }
  }, [theme]);
  const handleThemeChange = (newTheme) => {
    setTheme(newTheme);
    if (typeof document !== "undefined") {
      document.cookie = `theme=${newTheme}; path=/; max-age=31536000`;
    }
    if (typeof window !== "undefined") {
      if (!window.__FW_DATA__) {
        window.__FW_DATA__ = {};
      }
      window.__FW_DATA__ = {
        ...window.__FW_DATA__,
        theme: newTheme
      };
    }
    lastSentRef.current = newTheme;
    sendMessage(newTheme);
    setTimeout(() => {
      if (lastSentRef.current === newTheme) {
        lastSentRef.current = null;
      }
    }, 500);
  };
  return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: { theme, handleThemeChange }, children });
};
var useTheme = () => {
  return useContext(ThemeContext);
};
export {
  ThemeProvider,
  useTheme
};
//# sourceMappingURL=themes.js.map