"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ColorModeProvider = ColorModeProvider;
exports.useColorModeValue = useColorModeValue;
exports.useColorMode = exports.ColorModeContext = void 0;

var _react = _interopRequireDefault(require("react"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const ColorModeContext = /*#__PURE__*/_react.default.createContext({});

exports.ColorModeContext = ColorModeContext;

function ColorModeProvider(props) {
  const {
    value,
    children,
    options: {
      initialColorMode
    },
    colorModeManager
  } = props;

  const [colorMode, rawSetColorMode] = _react.default.useState(colorModeManager ? colorModeManager.get(initialColorMode) : initialColorMode);

  const setColorMode = _react.default.useCallback(val => {
    if (colorModeManager) {
      colorModeManager.set(val);
    }

    rawSetColorMode(val);
  }, [colorModeManager]);

  const toggleColorMode = _react.default.useCallback(() => {
    setColorMode(colorMode === 'light' ? 'dark' : 'light');
  }, [colorMode, setColorMode]);

  const context = {
    colorMode: value !== null && value !== void 0 ? value : colorMode,
    toggleColorMode: value ? () => {} : toggleColorMode,
    setColorMode: value ? () => {} : setColorMode
  };
  return /*#__PURE__*/_react.default.createElement(ColorModeContext.Provider, {
    value: context
  }, children);
}

const useColorMode = () => {
  const context = _react.default.useContext(ColorModeContext);

  if (context === undefined) {
    throw new Error('useColorMode must be used within a ColorModeProvider');
  }

  return context;
};

exports.useColorMode = useColorMode;

function useColorModeValue(light, dark) {
  const {
    colorMode
  } = useColorMode();
  return colorMode === 'light' ? light : dark;
}
//# sourceMappingURL=provider.js.map