"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useTheme = exports.ThemeProvider = void 0;
var _react = require("react");
var _theme = require("../theme");
var _store = require("./store");
var _store2 = require("./reducers/store");
const ThemeContext = /*#__PURE__*/(0, _react.createContext)({
  ..._theme.bifoldTheme,
  setTheme: () => {}
});
const ThemeProvider = ({
  themes,
  defaultThemeName,
  children
}) => {
  const [store, dispatch] = (0, _store.useStore)();
  const activeTheme = (0, _react.useMemo)(() => {
    return store.preferences.theme && themes.find(t => t.themeName === store.preferences.theme) || themes.find(t => t.themeName === defaultThemeName) || themes[0];
  }, [store.preferences.theme, themes, defaultThemeName]);
  const setTheme = (0, _react.useCallback)(themeName => {
    const newTheme = themes.find(t => t.themeName === themeName) || themes[0];
    dispatch({
      type: _store2.DispatchAction.SET_THEME,
      payload: [newTheme.themeName]
    });
  }, [themes, dispatch]);

  // prevent re-rendering of the context value
  const value = (0, _react.useMemo)(() => {
    return {
      ...activeTheme,
      setTheme
    };
  }, [activeTheme, setTheme]);
  return /*#__PURE__*/React.createElement(ThemeContext.Provider, {
    value: value
  }, children);
};
exports.ThemeProvider = ThemeProvider;
const useTheme = () => (0, _react.useContext)(ThemeContext);
exports.useTheme = useTheme;
//# sourceMappingURL=theme.js.map