"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.themeContext = exports.ThemeProvider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = require("./base/index");
var _reactNativeGestureHandler = require("react-native-gesture-handler");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const themeContext = exports.themeContext = /*#__PURE__*/(0, _react.createContext)({});

/**
 * The main provider component which controls the theme and color mode of the components in the entire application.
 */
const ThemeProvider = ({
  initialColorMode = "light",
  theme = _index.baseTheme,
  haveFontsLoaded = true,
  children
}) => {
  const systemThemeStyle = (0, _reactNative.useColorScheme)();
  const [colorMode, setColorMode] = (0, _react.useState)(initialColorMode === "system" ? systemThemeStyle : initialColorMode);
  const switchColorMode = colorMode => {
    if (colorMode === "dark") {
      setColorMode("dark");
    } else if (colorMode == "light") {
      setColorMode("light");
    } else {
      setColorMode(systemThemeStyle);
    }
  };
  const toggleColorMode = () => {
    if (colorMode === "light") {
      switchColorMode("dark");
    } else {
      switchColorMode("light");
    }
  };
  (0, _react.useEffect)(() => {
    let activeTheme = initialColorMode === "system" ? systemThemeStyle : initialColorMode;
    switchColorMode(activeTheme);
  }, [theme, initialColorMode, systemThemeStyle]);
  if (haveFontsLoaded) {
    return /*#__PURE__*/_react.default.createElement(themeContext.Provider, {
      value: {
        theme,
        colorMode,
        toggleColorMode,
        switchColorMode
      }
    }, /*#__PURE__*/_react.default.createElement(_reactNativeGestureHandler.GestureHandlerRootView, {
      style: {
        flex: 1
      }
    }, children));
  }
  return null;
};
exports.ThemeProvider = ThemeProvider;
//# sourceMappingURL=theme-context.js.map