"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeProvider = exports.useTheme = exports.SCRIPT_SRC = void 0;
const react_1 = __importStar(require("react"));
const LOCAL_STORAGE_KEY = 'based-ui-theme';
const DEFAULT_THEME = 'light';
const ATTRIBUTE_NAME = 'data-theme';
// This script is necessary because we want to set the data-theme attribute before the first render
// so that light/dark does not flash
// It should be placed somewhere in the body of the document, since it's sync it will stop the document parsing and so a render won't happen which is exactly what we are looking for
exports.SCRIPT_SRC = `<script>!function(){try{var t=localStorage.getItem("${LOCAL_STORAGE_KEY}");document.documentElement.setAttribute("${ATTRIBUTE_NAME}",null!=t?t:"${DEFAULT_THEME}")}catch(t){}}();</script>`;
const defaultContext = {
    setTheme: () => { },
    theme: DEFAULT_THEME,
};
const ThemeContext = (0, react_1.createContext)(defaultContext);
function useTheme() {
    return (0, react_1.useContext)(ThemeContext);
}
exports.useTheme = useTheme;
function ThemeProvider({ children }) {
    const [theme, setTheme] = (0, react_1.useState)(() => getThemeFromLocalStorage());
    (0, react_1.useEffect)(() => {
        const d = document.documentElement;
        d.setAttribute(ATTRIBUTE_NAME, theme);
        try {
            localStorage.setItem(LOCAL_STORAGE_KEY, theme);
        }
        catch { }
    }, [theme]);
    return (react_1.default.createElement(ThemeContext.Provider, { value: { theme, setTheme } }, children));
}
exports.ThemeProvider = ThemeProvider;
function getThemeFromLocalStorage() {
    let theme;
    try {
        theme = localStorage.getItem(LOCAL_STORAGE_KEY);
    }
    catch { }
    return theme ?? DEFAULT_THEME;
}
//# sourceMappingURL=useTheme.js.map