"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useNetwork = exports.NetworkProvider = exports.NetworkContext = void 0;
var _netinfo = require("@react-native-community/netinfo");
var _react = require("react");
var _NetInfoModal = _interopRequireDefault(require("../components/modals/NetInfoModal"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const NetworkContext = exports.NetworkContext = /*#__PURE__*/(0, _react.createContext)(null);

// NOTE: @react-native-community/netinfo can be configured to use whichever reachability check desired
// eg. isInternetReachable can be set to check a specific URL (like your mediator). See the docs here for more info:
// https://github.com/react-native-netinfo/react-native-netinfo?tab=readme-ov-file#configure
const NetworkProvider = ({
  children
}) => {
  const {
    isConnected,
    type,
    isInternetReachable
  } = (0, _netinfo.useNetInfo)();
  const [isNetInfoModalDisplayed, setIsNetInfoModalDisplayed] = (0, _react.useState)(false);
  const displayNetInfoModal = (0, _react.useCallback)(() => {
    setIsNetInfoModalDisplayed(true);
  }, []);
  const hideNetInfoModal = (0, _react.useCallback)(() => {
    setIsNetInfoModalDisplayed(false);
  }, []);

  /**
   * Returns null until the network state is known, then returns boolean
   * Useful for cases where we do not want to take action until the network state is known
   *
   * @returns {boolean | null} - `true` if the network is connected, `false` if not connected,
   *                             and `null` if the network status not yet known
   */
  const silentAssertConnectedNetwork = (0, _react.useCallback)(() => {
    return type === _netinfo.NetInfoStateType.unknown ? null : isConnected || [_netinfo.NetInfoStateType.wifi, _netinfo.NetInfoStateType.cellular].includes(type);
  }, [isConnected, type]);

  /**
   * Strictly asserts that the network is connected. Will return false even if
   * the network state is not yet known - in this case it will also display the
   * NetInfoModal
   * Useful for cases where we must be sure of connectivity before proceeding
   *
   * @returns {boolean} - `true` if the network is checked and connected, otherwise `false`
   */
  const assertNetworkConnected = (0, _react.useCallback)(() => {
    const connectionConfirmed = silentAssertConnectedNetwork() === true;
    if (!connectionConfirmed) {
      displayNetInfoModal();
    }
    return connectionConfirmed;
  }, [silentAssertConnectedNetwork, displayNetInfoModal]);
  const assertInternetReachable = (0, _react.useCallback)(() => {
    return isInternetReachable;
  }, [isInternetReachable]);
  return /*#__PURE__*/React.createElement(NetworkContext.Provider, {
    value: {
      silentAssertConnectedNetwork,
      assertNetworkConnected,
      displayNetInfoModal,
      hideNetInfoModal,
      assertInternetReachable
    }
  }, children, /*#__PURE__*/React.createElement(_NetInfoModal.default, {
    visible: isNetInfoModalDisplayed,
    onSubmit: () => hideNetInfoModal()
  }));
};
exports.NetworkProvider = NetworkProvider;
const useNetwork = () => (0, _react.useContext)(NetworkContext);
exports.useNetwork = useNetwork;
//# sourceMappingURL=network.js.map