// src/hooks/useTheme.ts
import { useEffect, useMemo } from "react";

// src/store/themeStore.ts
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
var DARK_THEME_MAP = {
  "base-light": "base-dark",
  "enem-parana-light": "enem-parana-dark",
  "enem-paraiba-light": "enem-paraiba-dark",
  "analytica-light": "analytica-dark"
};
var resolveDarkTheme = (originalTheme) => {
  if (!originalTheme) return "base-dark";
  return DARK_THEME_MAP[originalTheme] ?? "base-dark";
};
var applyThemeToDOM = (mode) => {
  const htmlElement = document.documentElement;
  const originalTheme = htmlElement.dataset.originalTheme;
  if (mode === "dark") {
    htmlElement.dataset.theme = resolveDarkTheme(originalTheme);
    return true;
  } else if (mode === "light") {
    if (originalTheme) {
      htmlElement.dataset.theme = originalTheme;
    }
    return false;
  } else if (mode === "system") {
    const isSystemDark = window.matchMedia(
      "(prefers-color-scheme: dark)"
    ).matches;
    if (isSystemDark) {
      htmlElement.dataset.theme = resolveDarkTheme(originalTheme);
      return true;
    } else if (originalTheme) {
      htmlElement.dataset.theme = originalTheme;
      return false;
    }
  }
  return false;
};
var saveOriginalTheme = () => {
  const htmlElement = document.documentElement;
  const currentTheme = htmlElement.dataset.theme || document.querySelector('meta[name="theme"]')?.getAttribute("content");
  if (currentTheme && !htmlElement.dataset.originalTheme) {
    htmlElement.dataset.originalTheme = currentTheme;
  }
};
var useThemeStore = create()(
  devtools(
    persist(
      (set, get) => ({
        // Initial state
        themeMode: "system",
        isDark: false,
        // Actions
        applyTheme: (mode) => {
          const isDark = applyThemeToDOM(mode);
          set({ isDark });
        },
        toggleTheme: () => {
          const { themeMode, applyTheme } = get();
          let newMode;
          if (themeMode === "light") {
            newMode = "dark";
          } else if (themeMode === "dark") {
            newMode = "light";
          } else {
            newMode = "dark";
          }
          set({ themeMode: newMode });
          applyTheme(newMode);
        },
        setTheme: (mode) => {
          const { applyTheme } = get();
          set({ themeMode: mode });
          applyTheme(mode);
        },
        setWhiteLabelTheme: (theme) => {
          const htmlElement = document.documentElement;
          if (theme) {
            htmlElement.dataset.originalTheme = theme;
            const { themeMode, applyTheme } = get();
            if (themeMode === "light" || themeMode === "system") {
              htmlElement.dataset.theme = theme;
            }
            applyTheme(themeMode);
          }
        },
        clearWhiteLabelTheme: () => {
          const htmlElement = document.documentElement;
          delete htmlElement.dataset.originalTheme;
          delete htmlElement.dataset.theme;
          saveOriginalTheme();
          const { themeMode, applyTheme } = get();
          applyTheme(themeMode);
        },
        initializeTheme: () => {
          const { themeMode, applyTheme } = get();
          saveOriginalTheme();
          applyTheme(themeMode);
        },
        handleSystemThemeChange: () => {
          const { themeMode, applyTheme } = get();
          if (themeMode === "system") {
            applyTheme("system");
          }
        }
      }),
      {
        name: "theme-store",
        // Nome da chave no localStorage
        partialize: (state) => ({
          themeMode: state.themeMode
        })
        // Só persiste o themeMode, não o isDark
      }
    ),
    {
      name: "theme-store"
    }
  )
);

// src/hooks/useTheme.ts
var useTheme = () => {
  const {
    themeMode,
    isDark,
    toggleTheme,
    setTheme,
    initializeTheme,
    handleSystemThemeChange
  } = useThemeStore();
  const branding = useMemo(() => {
    if (typeof document === "undefined") {
      return {
        theme: null,
        favicon: null,
        icon: null,
        mainLogo: null,
        internalLogo: null,
        loginImage: null
      };
    }
    return {
      theme: document.querySelector('meta[name="theme"]')?.getAttribute("content") ?? null,
      favicon: document.querySelector('link[rel="icon"]')?.getAttribute("href") ?? null,
      icon: document.querySelector('link[rel="apple-touch-icon"]')?.getAttribute("href") ?? null,
      mainLogo: document.querySelector('meta[name="main-logo"]')?.getAttribute("content") ?? null,
      internalLogo: document.querySelector('meta[name="internal-logo"]')?.getAttribute("content") ?? null,
      loginImage: document.querySelector('meta[name="login-image"]')?.getAttribute("content") ?? null
    };
  }, []);
  useEffect(() => {
    initializeTheme();
    const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
    mediaQuery.addEventListener("change", handleSystemThemeChange);
    return () => {
      mediaQuery.removeEventListener("change", handleSystemThemeChange);
    };
  }, [initializeTheme, handleSystemThemeChange]);
  return {
    themeMode,
    isDark,
    toggleTheme,
    setTheme,
    branding
  };
};
export {
  useTheme
};
//# sourceMappingURL=index.mjs.map