"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// modules/react/themes/index.ts
var themes_exports = {};
__export(themes_exports, {
  ThemeProvider: () => ThemeProvider,
  useTheme: () => useTheme
});
module.exports = __toCommonJS(themes_exports);

// modules/react/themes/theme-provider/index.tsx
var import_react2 = require("react");

// modules/react/hooks/useBroadcastChannel/index.tsx
var import_react = require("react");
var useBroadcastChannel = (channelName) => {
  const [message, setMessage] = (0, import_react.useState)(null);
  const channelRef = (0, import_react.useRef)(null);
  (0, import_react.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 = (0, import_react.useCallback)((msg) => {
    if (channelRef.current) {
      channelRef.current.postMessage(msg);
    }
  }, []);
  return { message, sendMessage };
};

// modules/react/themes/theme-provider/index.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var ThemeContext = (0, import_react2.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 = (0, import_react2.useRef)(null);
  const [theme, setTheme] = (0, import_react2.useState)(() => {
    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";
  });
  (0, import_react2.useEffect)(() => {
    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]);
  (0, import_react2.useEffect)(() => {
    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);
      };
    }
  }, []);
  (0, import_react2.useEffect)(() => {
    if (initialTheme && initialTheme !== theme) {
      setTheme(initialTheme);
    }
  }, [initialTheme]);
  (0, import_react2.useEffect)(() => {
    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__ */ (0, import_jsx_runtime.jsx)(ThemeContext.Provider, { value: { theme, handleThemeChange }, children });
};
var useTheme = () => {
  return (0, import_react2.useContext)(ThemeContext);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ThemeProvider,
  useTheme
});
//# sourceMappingURL=themes.cjs.map