'use client';
import { devWarn } from './chunk-VZLV27H2.js';
import * as React2 from 'react';

// ../theme/dist/index.js
function resolveTheme(mode, systemPrefersDark) {
  if (mode === "system") {
    return systemPrefersDark ? "dark" : "light";
  }
  return mode;
}
function createTheme(config = {}, storage, mediaQuery) {
  const {
    defaultMode = "system",
    storageKey = "rfr-theme"
  } = config;
  const listeners = /* @__PURE__ */ new Set();
  let cleanupMediaQuery = null;
  const persisted = storage?.get(storageKey);
  let mode = persisted && ["light", "dark", "system"].includes(persisted) ? persisted : defaultMode;
  let systemPrefersDark = mediaQuery?.matches("(prefers-color-scheme: dark)") ?? false;
  let state = {
    mode,
    resolved: resolveTheme(mode, systemPrefersDark)
  };
  function notify() {
    for (const fn of listeners) {
      fn(state);
    }
  }
  function updateState(newMode) {
    mode = newMode;
    state = { mode, resolved: resolveTheme(mode, systemPrefersDark) };
    storage?.set(storageKey, mode);
    notify();
  }
  if (mediaQuery) {
    cleanupMediaQuery = mediaQuery.subscribe(
      "(prefers-color-scheme: dark)",
      (matches) => {
        systemPrefersDark = matches;
        if (mode === "system") {
          state = { mode, resolved: resolveTheme(mode, systemPrefersDark) };
          notify();
        }
      }
    );
  }
  return {
    getState() {
      return state;
    },
    setMode(newMode) {
      if (newMode !== mode) {
        updateState(newMode);
      }
    },
    subscribe(fn) {
      listeners.add(fn);
      return () => {
        listeners.delete(fn);
      };
    },
    destroy() {
      listeners.clear();
      cleanupMediaQuery?.();
    }
  };
}
function getThemeScript(storageKey = "rfr-theme", attribute = "class") {
  return `(function(){try{var m=localStorage.getItem('${storageKey}');var s=window.matchMedia('(prefers-color-scheme:dark)').matches;var t=m==='dark'||(m!=='light'&&s)?'dark':'light';var d=document.documentElement;${attribute === "class" ? "d.classList.remove('light','dark');d.classList.add(t);" : `d.setAttribute('${attribute}',t);`}d.style.colorScheme=t;}catch(e){}})()`;
}
function createLocalStorageAdapter() {
  return {
    get(key) {
      try {
        return localStorage.getItem(key);
      } catch {
        return null;
      }
    },
    set(key, value) {
      try {
        localStorage.setItem(key, value);
      } catch {
      }
    }
  };
}
function createMediaQueryAdapter() {
  return {
    matches(query) {
      if (typeof window === "undefined") return false;
      return window.matchMedia(query).matches;
    },
    subscribe(query, callback) {
      if (typeof window === "undefined") return () => {
      };
      const mql = window.matchMedia(query);
      const handler = (e) => callback(e.matches);
      mql.addEventListener("change", handler);
      return () => mql.removeEventListener("change", handler);
    }
  };
}
function applyThemeToDOM(resolved, attribute = "class") {
  if (typeof document === "undefined") return;
  const root = document.documentElement;
  if (attribute === "class") {
    root.classList.remove("light", "dark");
    root.classList.add(resolved);
  } else {
    root.setAttribute(attribute, resolved);
  }
  root.style.colorScheme = resolved;
}

// ../react-theme/dist/index.js
var ThemeContext = React2.createContext(null);
function ThemeProvider({
  children,
  defaultMode = "system",
  storageKey = "rfr-theme",
  attribute = "class"
}) {
  const themeRef = React2.useRef(null);
  if (!themeRef.current) {
    const isBrowser = typeof window !== "undefined";
    themeRef.current = createTheme(
      { defaultMode, storageKey},
      isBrowser ? createLocalStorageAdapter() : void 0,
      isBrowser ? createMediaQueryAdapter() : void 0
    );
  }
  const [state, setState] = React2.useState(() => themeRef.current.getState());
  React2.useEffect(() => {
    const theme = themeRef.current;
    applyThemeToDOM(theme.getState().resolved, attribute);
    const unsub = theme.subscribe((newState) => {
      setState(newState);
      applyThemeToDOM(newState.resolved, attribute);
    });
    return () => {
      unsub();
      theme.destroy();
    };
  }, [attribute]);
  const contextValue = React2.useMemo(
    () => ({
      mode: state.mode,
      resolved: state.resolved,
      setMode: (mode) => themeRef.current?.setMode(mode)
    }),
    [state.mode, state.resolved]
  );
  return React2.createElement(ThemeContext.Provider, { value: contextValue }, children);
}
function useTheme() {
  const context = React2.useContext(ThemeContext);
  if (!context) {
    devWarn(
      "react-theme/use-theme-outside-provider",
      "useTheme() was called outside a <ThemeProvider>. Wrap your app (or the consuming subtree) in <ThemeProvider> so the theme context is available."
    );
    throw new Error("useTheme must be used within a <ThemeProvider>");
  }
  return context;
}
var modes = [
  { value: "light", label: "Light", icon: "sun" },
  { value: "dark", label: "Dark", icon: "moon" },
  { value: "system", label: "System", icon: "monitor" }
];
var icons = {
  sun: React2.createElement(
    "svg",
    {
      xmlns: "http://www.w3.org/2000/svg",
      width: 16,
      height: 16,
      viewBox: "0 0 24 24",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: 2,
      strokeLinecap: "round",
      strokeLinejoin: "round"
    },
    React2.createElement("circle", { cx: 12, cy: 12, r: 5 }),
    React2.createElement("path", { d: "M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" })
  ),
  moon: React2.createElement(
    "svg",
    {
      xmlns: "http://www.w3.org/2000/svg",
      width: 16,
      height: 16,
      viewBox: "0 0 24 24",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: 2,
      strokeLinecap: "round",
      strokeLinejoin: "round"
    },
    React2.createElement("path", { d: "M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" })
  ),
  monitor: React2.createElement(
    "svg",
    {
      xmlns: "http://www.w3.org/2000/svg",
      width: 16,
      height: 16,
      viewBox: "0 0 24 24",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: 2,
      strokeLinecap: "round",
      strokeLinejoin: "round"
    },
    React2.createElement("rect", { x: 2, y: 3, width: 20, height: 14, rx: 2, ry: 2 }),
    React2.createElement("line", { x1: 8, y1: 21, x2: 16, y2: 21 }),
    React2.createElement("line", { x1: 12, y1: 17, x2: 12, y2: 21 })
  )
};
function ThemeToggle({ className, variant = "segmented" }) {
  const { mode, setMode } = useTheme();
  if (variant === "segmented") {
    return React2.createElement(
      "div",
      {
        className: `inline-flex items-center gap-1 rounded-lg border p-1 ${className ?? ""}`,
        role: "radiogroup",
        "aria-label": "Theme"
      },
      modes.map(
        ({ value, label, icon }) => React2.createElement("button", {
          key: value,
          type: "button",
          role: "radio",
          "aria-checked": mode === value,
          "aria-label": label,
          className: `inline-flex items-center justify-center rounded-md p-1.5 text-sm transition-colors ${mode === value ? "bg-accent text-accent-foreground" : "text-muted-foreground hover:bg-muted"}`,
          onClick: () => setMode(value)
        }, icons[icon])
      )
    );
  }
  const [open, setOpen] = React2.useState(false);
  const ref = React2.useRef(null);
  React2.useEffect(() => {
    if (!open) return;
    const handler = (e) => {
      if (ref.current && !ref.current.contains(e.target)) setOpen(false);
    };
    document.addEventListener("mousedown", handler);
    return () => document.removeEventListener("mousedown", handler);
  }, [open]);
  const currentIcon = modes.find((m) => m.value === mode)?.icon ?? "monitor";
  return React2.createElement(
    "div",
    { ref, className: `relative ${className ?? ""}` },
    React2.createElement("button", {
      type: "button",
      "aria-label": "Toggle theme",
      "aria-expanded": open,
      className: "inline-flex items-center justify-center rounded-md p-2 text-sm transition-colors hover:bg-muted",
      onClick: () => setOpen(!open)
    }, icons[currentIcon]),
    open && React2.createElement(
      "div",
      {
        className: "absolute right-0 top-full mt-1 z-50 min-w-[8rem] rounded-md border bg-popover p-1 shadow-md",
        role: "menu"
      },
      modes.map(
        ({ value, label, icon }) => React2.createElement("button", {
          key: value,
          type: "button",
          role: "menuitem",
          className: `flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm transition-colors hover:bg-accent ${mode === value ? "bg-accent" : ""}`,
          onClick: () => {
            setMode(value);
            setOpen(false);
          }
        }, icons[icon], label)
      )
    )
  );
}
function ThemeScript({
  storageKey = "rfr-theme",
  attribute = "class"
}) {
  return React2.createElement("script", {
    dangerouslySetInnerHTML: {
      __html: getThemeScript(storageKey, attribute)
    }
  });
}

export { ThemeProvider, ThemeScript, ThemeToggle, useTheme };
//# sourceMappingURL=theme.js.map
//# sourceMappingURL=theme.js.map