"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = _interopRequireDefault(require("@credo-ts/react-hooks"));
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _OpenIDCredentialRecordProvider = require("../modules/openid/context/OpenIDCredentialRecordProvider");
var _constants = require("../constants");
var _containerApi = require("../container-api");
var _activity = require("../contexts/activity");
var _store = require("../contexts/store");
var _error = require("../types/error");
var _MainStack = _interopRequireDefault(require("./MainStack"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const RootStack = () => {
  const [store, dispatch] = (0, _store.useStore)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [useAgentSetup, OnboardingStack, loadState] = (0, _containerApi.useServices)([_containerApi.TOKENS.HOOK_USE_AGENT_SETUP, _containerApi.TOKENS.STACK_ONBOARDING, _containerApi.TOKENS.LOAD_STATE]);
  const {
    agent,
    initializeAgent,
    shutdownAndClearAgentIfExists
  } = useAgentSetup();
  const [onboardingComplete, setOnboardingComplete] = (0, _react.useState)(false);
  const shouldRenderMainStack = (0, _react.useMemo)(() => onboardingComplete && store.authentication.didAuthenticate, [onboardingComplete, store.authentication.didAuthenticate]);
  (0, _react.useEffect)(() => {
    // if user gets locked out, erase agent
    if (!store.authentication.didAuthenticate) {
      shutdownAndClearAgentIfExists();
    }
  }, [store.authentication.didAuthenticate, shutdownAndClearAgentIfExists]);
  (0, _react.useEffect)(() => {
    const sub = _reactNative.DeviceEventEmitter.addListener(_constants.EventTypes.DID_COMPLETE_ONBOARDING, () => {
      setOnboardingComplete(true);
    });
    return sub.remove;
  }, []);
  (0, _react.useEffect)(() => {
    // Load state only if it hasn't been loaded yet
    if (store.stateLoaded) return;
    loadState(dispatch).catch(err => {
      const error = new _error.BifoldError(t('Error.Title1044'), t('Error.Message1044'), err.message, 1001);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, error);
    });
  }, [dispatch, loadState, t, store.stateLoaded]);
  if (shouldRenderMainStack && agent) {
    return /*#__PURE__*/_react.default.createElement(_reactHooks.default, {
      agent: agent
    }, /*#__PURE__*/_react.default.createElement(_OpenIDCredentialRecordProvider.OpenIDCredentialRecordProvider, null, /*#__PURE__*/_react.default.createElement(_activity.ActivityProvider, null, /*#__PURE__*/_react.default.createElement(_MainStack.default, null))));
  }
  return /*#__PURE__*/_react.default.createElement(OnboardingStack, {
    agent: agent,
    initializeAgent: initializeAgent
  });
};
var _default = exports.default = RootStack;
//# sourceMappingURL=RootStack.js.map