"use client";
'use strict';

var React = require('react');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

var THEME_STORAGE_KEY = "sm2-theme";
var DARK_CLASS_NAME = "dark";
var AppThemeContext = React__namespace.createContext(null);
function readStoredTheme() {
  if (typeof window === "undefined") {
    return void 0;
  }
  try {
    const raw = window.localStorage.getItem(THEME_STORAGE_KEY);
    return raw === "light" || raw === "dark" ? raw : void 0;
  } catch (e) {
    return void 0;
  }
}
function applyThemeToDocument(theme) {
  const root = document.documentElement;
  root.classList.toggle(DARK_CLASS_NAME, theme === "dark");
  root.style.colorScheme = theme;
}
function AppThemeProvider({
  children
}) {
  const [theme, setThemeState] = React__namespace.useState(
    () => {
      var _a;
      return (_a = readStoredTheme()) != null ? _a : "light";
    }
  );
  React__namespace.useLayoutEffect(() => {
    applyThemeToDocument(theme);
  }, [theme]);
  React__namespace.useEffect(() => {
    try {
      window.localStorage.setItem(THEME_STORAGE_KEY, theme);
    } catch (e) {
    }
  }, [theme]);
  React__namespace.useEffect(() => {
    const handleStorage = (event) => {
      if (event.key !== THEME_STORAGE_KEY) {
        return;
      }
      const nextTheme = event.newValue === "light" || event.newValue === "dark" ? event.newValue : "light";
      setThemeState(nextTheme);
      applyThemeToDocument(nextTheme);
    };
    window.addEventListener("storage", handleStorage);
    return () => window.removeEventListener("storage", handleStorage);
  }, []);
  const setTheme = React__namespace.useCallback((nextTheme) => {
    setThemeState(nextTheme);
  }, []);
  const value = React__namespace.useMemo(
    () => ({
      theme,
      resolvedTheme: theme,
      setTheme
    }),
    [theme, setTheme]
  );
  return /* @__PURE__ */ jsxRuntime.jsx(AppThemeContext.Provider, { value, children });
}
function useTheme() {
  const context = React__namespace.useContext(AppThemeContext);
  if (context == null) {
    throw new Error("useTheme must be used within AppThemeProvider");
  }
  return context;
}

exports.AppThemeProvider = AppThemeProvider;
exports.THEME_STORAGE_KEY = THEME_STORAGE_KEY;
exports.useTheme = useTheme;
//# sourceMappingURL=app-theme-provider.cjs.map
//# sourceMappingURL=app-theme-provider.cjs.map