'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import React, { createContext, use, useCallback, useEffect, useState } from 'react';
import { useConfig } from '../Config/index.js';
const initialContext = {
    autoMode: true,
    setTheme: ()=>null,
    theme: 'light'
};
const Context = /*#__PURE__*/ createContext(initialContext);
function setCookie(cname, cvalue, exdays) {
    const d = new Date();
    d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
    const expires = 'expires=' + d.toUTCString();
    document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
const getTheme = (cookieKey)=>{
    let theme;
    const themeFromCookies = window.document.cookie.split('; ').find((row)=>row.startsWith(`${cookieKey}=`))?.split('=')[1];
    if (themeFromCookies === 'light' || themeFromCookies === 'dark') {
        theme = themeFromCookies;
    } else {
        theme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
    document.documentElement.setAttribute('data-theme', theme);
    return {
        theme,
        themeFromCookies
    };
};
export const defaultTheme = 'light';
export const ThemeProvider = ({ children, theme: initialTheme })=>{
    const { config } = useConfig();
    const preselectedTheme = config.admin.theme;
    const cookieKey = `${config.cookiePrefix || 'payload'}-theme`;
    const [theme, setThemeState] = useState(initialTheme || defaultTheme);
    const [autoMode, setAutoMode] = useState();
    useEffect(()=>{
        if (preselectedTheme !== 'all') {
            return;
        }
        const { theme, themeFromCookies } = getTheme(cookieKey);
        setThemeState(theme);
        setAutoMode(!themeFromCookies);
    }, [
        preselectedTheme,
        cookieKey
    ]);
    const setTheme = useCallback((themeToSet)=>{
        if (themeToSet === 'light' || themeToSet === 'dark') {
            setThemeState(themeToSet);
            setAutoMode(false);
            setCookie(cookieKey, themeToSet, 365);
            document.documentElement.setAttribute('data-theme', themeToSet);
        } else if (themeToSet === 'auto') {
            // to delete the cookie, we set an expired date
            setCookie(cookieKey, themeToSet, -1);
            const themeFromOS = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
            document.documentElement.setAttribute('data-theme', themeFromOS);
            setAutoMode(true);
            setThemeState(themeFromOS);
        }
    }, [
        cookieKey
    ]);
    return /*#__PURE__*/ _jsxDEV(Context, {
        value: {
            autoMode,
            setTheme,
            theme
        },
        children: children
    }, void 0, false, {
        fileName: "src/providers/Theme/index.tsx",
        lineNumber: 103,
        columnNumber: 10
    }, this);
};
export const useTheme = ()=>use(Context);

//# sourceMappingURL=index.js.map