"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@credo-ts/core");
var _reactHooks = require("@credo-ts/react-hooks");
var _ariesBifoldVerifier = require("@hyperledger/aries-bifold-verifier");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _constants = require("../constants");
var _containerApi = require("../container-api");
var _store = require("../contexts/store");
var _deepLinks = require("../hooks/deep-links");
var _error = require("../types/error");
var _MainStack = _interopRequireDefault(require("./MainStack"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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; }
const RootStack = () => {
  const [store, dispatch] = (0, _store.useStore)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [OnboardingStack, loadState] = (0, _containerApi.useServices)([_containerApi.TOKENS.STACK_ONBOARDING, _containerApi.TOKENS.LOAD_STATE]);
  // remove connection on mobile verifier proofs if proof is rejected
  // regardless of if it has been opened
  const declinedProofs = (0, _reactHooks.useProofByState)([_core.ProofState.Declined, _core.ProofState.Abandoned]);
  const [onboardingComplete, setOnboardingComplete] = (0, _react.useState)(false);
  (0, _deepLinks.useDeepLinks)();
  const shouldRenderMainStack = (0, _react.useMemo)(() => onboardingComplete && store.authentication.didAuthenticate, [onboardingComplete, store.authentication.didAuthenticate]);
  (0, _react.useEffect)(() => {
    const sub = _reactNative.DeviceEventEmitter.addListener(_constants.EventTypes.DID_COMPLETE_ONBOARDING, () => {
      setOnboardingComplete(true);
    });
    return sub.remove;
  }, []);
  (0, _react.useEffect)(() => {
    declinedProofs.forEach(proof => {
      var _proof$metadata;
      const meta = proof === null || proof === void 0 || (_proof$metadata = proof.metadata) === null || _proof$metadata === void 0 ? void 0 : _proof$metadata.get(_ariesBifoldVerifier.ProofMetadata.customMetadata);
      if (meta !== null && meta !== void 0 && meta.delete_conn_after_seen) {
        agent === null || agent === void 0 || agent.connections.deleteById((proof === null || proof === void 0 ? void 0 : proof.connectionId) ?? '').catch(() => null);
        proof === null || proof === void 0 || proof.metadata.set(_ariesBifoldVerifier.ProofMetadata.customMetadata, {
          ...meta,
          delete_conn_after_seen: false
        });
      }
    });
  }, [declinedProofs, agent, store.preferences.useDataRetention]);
  (0, _react.useEffect)(() => {
    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]);
  if (shouldRenderMainStack) {
    return /*#__PURE__*/_react.default.createElement(_MainStack.default, null);
  }
  return /*#__PURE__*/_react.default.createElement(OnboardingStack, null);
};
var _default = exports.default = RootStack;
//# sourceMappingURL=RootStack.js.map