"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 = _interopRequireWildcard(require("react"));
var _NetInfoModal = _interopRequireDefault(require("../components/modals/NetInfoModal"));
var _ledger = require("../utils/ledger");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const NetworkContext = /*#__PURE__*/(0, React.createContext)(null);
exports.NetworkContext = NetworkContext;
const NetworkProvider = _ref => {
  let {
    children
  } = _ref;
  const netInfo = (0, _netinfo.useNetInfo)();
  const [isNetInfoModalDisplayed, setIsNetInfoModalDisplayed] = (0, React.useState)(false);
  const displayNetInfoModal = () => {
    setIsNetInfoModalDisplayed(true);
  };
  const hideNetInfoModal = () => {
    setIsNetInfoModalDisplayed(false);
  };
  const silentAssertConnectedNetwork = () => {
    return netInfo.isConnected || netInfo.type !== _netinfo.NetInfoStateType.none;
  };
  const assertConnectedNetwork = () => {
    const isConnected = silentAssertConnectedNetwork();
    if (!isConnected) {
      displayNetInfoModal();
    }
    return isConnected;
  };
  const assertLedgerConnectivity = async () => {
    const nodes = (0, _ledger.fetchLedgerNodes)();
    if (typeof nodes === 'undefined' || nodes.length === 0) {
      return false;
    }
    const connections = await Promise.all(nodes.map(n => (0, _ledger.canConnectToLedgerNode)(n)));
    return connections.includes(true);
  };
  return /*#__PURE__*/React.createElement(NetworkContext.Provider, {
    value: {
      silentAssertConnectedNetwork,
      assertConnectedNetwork,
      displayNetInfoModal,
      hideNetInfoModal,
      assertLedgerConnectivity
    }
  }, 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