'use strict';
import { c } from 'react/compiler-runtime';
import { jsx } from 'react/jsx-runtime';
import React, { createContext, useState, useEffect } from 'react';
import { themes } from '../../menu/themes.js';
import { saveTheme, onThemeSwitch } from './theme.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../../meta/constants/meta-error-messages-constants.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const ThemeContext = createContext(void 0);
function useTheme() {
  const context = React.useContext(ThemeContext);
  if (context === void 0) {
    throw createError(metaErrorMessages.themeProviderError);
  }
  return context;
}
__name(useTheme, "useTheme");
function ThemeProvider(t0) {
  const $ = c(4);
  const {
    children
  } = t0;
  const [theme, setTheme] = useState(themes.light);
  const [appliedTheme, setAppliedTheme] = useState(themes.light);
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = newTheme => saveTheme(newTheme, setTheme);
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const contextValue = {
    theme,
    switchTheme: __name(t1, "switchTheme"),
    appliedTheme
  };
  let t2;
  let t3;
  if ($[1] !== theme) {
    t2 = () => onThemeSwitch(theme, setAppliedTheme);
    t3 = [theme];
    $[1] = theme;
    $[2] = t2;
    $[3] = t3;
  } else {
    t2 = $[2];
    t3 = $[3];
  }
  useEffect(t2, t3);
  return jsx(ThemeContext.Provider, {
    value: contextValue,
    children
  });
}
__name(ThemeProvider, "ThemeProvider");

export { ThemeProvider, useTheme };
