"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _reactNative = require("react-native");

var _Context = require("./Context");

var _hooks = require("./../color-mode/hooks");

var _hooks2 = require("../../hooks");

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

const HybridProvider = ({
  children,
  options: {
    initialColorMode = 'light',
    accessibleColors: isTextColorAccessible = false,
    useSystemColorMode
  },
  colorModeManager
}) => {
  // Color-mode content
  const {
    colorMode,
    setColorMode
  } = (0, _hooks.useModeManager)(initialColorMode, useSystemColorMode, colorModeManager);

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


  const [accessibleColors, setAccessibleColors] = _react.default.useState(isTextColorAccessible);

  _react.default.useEffect(() => {
    let escapeKeyListener = null;

    if (_reactNative.Platform.OS === 'web') {
      escapeKeyListener = e => {
        if (e.key === 'Escape') {
          if (_hooks2.keyboardDismissHandlerManager.length() > 0) {
            const lastHandler = _hooks2.keyboardDismissHandlerManager.pop();

            lastHandler();
          }
        }
      };

      document.addEventListener('keydown', escapeKeyListener);
    }

    return () => {
      if (_reactNative.Platform.OS === 'web') {
        document.removeEventListener('keydown', escapeKeyListener);
      }
    };
  }, []);

  return /*#__PURE__*/_react.default.createElement(_Context.HybridContext.Provider, {
    value: {
      colorMode: {
        colorMode,
        toggleColorMode,
        setColorMode,
        accessibleColors,
        setAccessibleColors
      }
    }
  }, children);
};

var _default = HybridProvider;
exports.default = _default;
//# sourceMappingURL=HybridProvider.js.map