"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactI18next = require("react-i18next");
var _reactNativeToastMessage = _interopRequireDefault(require("react-native-toast-message"));
var _network = require("../../contexts/network");
var _BaseToast = require("../../components/toast/BaseToast");
var _containerApi = require("../../container-api");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const NetInfo = () => {
  const {
    silentAssertConnectedNetwork,
    assertInternetReachable,
    assertMediatorReachable
  } = (0, _network.useNetwork)();
  const [{
    disableMediatorCheck
  }] = (0, _containerApi.useServices)([_containerApi.TOKENS.CONFIG]);
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [hasShown, setHasShown] = (0, _react.useState)(false);
  const isConnected = silentAssertConnectedNetwork();
  (0, _react.useEffect)(() => {
    const _showNetworkWarning = () => {
      setHasShown(true);
      _reactNativeToastMessage.default.show({
        type: _BaseToast.ToastType.Error,
        autoHide: true,
        text1: t('NetInfo.NoInternetConnectionTitle')
      });
    };
    // Network is available, do further testing according to CFG.disableMediatorCheck
    if (!disableMediatorCheck) {
      // Network is available
      if (isConnected) {
        // Check mediator socket, also assert internet reachable
        assertMediatorReachable().then(status => {
          if (status) {
            _reactNativeToastMessage.default.hide();
            return;
          } else {
            // Network is available but cannot access nediator, display toast
            _showNetworkWarning();
          }
        });
        return;
      } else if (!hasShown) {
        _showNetworkWarning();
      }
      return;
    } else {
      // Check internetReachable by connecting test beacon urls
      assertInternetReachable().then(status => {
        if (status) {
          _reactNativeToastMessage.default.hide();
          return;
        } else if (null === status) {
          // keep silent when the internet status not yet assert
          return;
          /*
          Toast.show({
            type: ToastType.Info,
            autoHide: false,
            text1: "Checking internet reachable",
          })
          */
        } else if (!hasShown) {
          _showNetworkWarning();
        }
      });
    }
  }, [isConnected, disableMediatorCheck, assertInternetReachable, assertMediatorReachable, t, hasShown]);
  return null;
};
var _default = exports.default = NetInfo;
//# sourceMappingURL=NetInfo.js.map