"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSystem = exports.SystemProvider = exports.SystemContext = exports.MainContainer = void 0;
var _ariesBifoldVerifier = require("@hyperledger/aries-bifold-verifier");
var _legacy = require("@hyperledger/aries-oca/build/legacy");
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
var _react = require("react");
var bundle = _interopRequireWildcard(require("./assets/oca-bundles.json"));
var _Button = _interopRequireDefault(require("./components/buttons/Button"));
var _indy = _interopRequireDefault(require("./configs/ledgers/indy"));
var _constants = require("./constants");
var _containerApi = require("./container-api");
var _store = require("./contexts/reducers/store");
var _store2 = require("./contexts/store");
var _OnboardingStack = _interopRequireDefault(require("./navigators/OnboardingStack"));
var _defaultStackOptions = require("./navigators/defaultStackOptions");
var _Developer = _interopRequireDefault(require("./screens/Developer"));
var _Onboarding = _interopRequireDefault(require("./screens/Onboarding"));
var _Preface = _interopRequireDefault(require("./screens/Preface"));
var _Terms = _interopRequireWildcard(require("./screens/Terms"));
var _keychain = require("./services/keychain");
var _logger = require("./services/logger");
var _navigators = require("./types/navigators");
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class MainContainer {
  static TOKENS = _containerApi.TOKENS;
  constructor(container, log) {
    this._container = container;
    this.log = log;
  }
  get container() {
    return this._container;
  }
  init() {
    var _this$log;
    (_this$log = this.log) === null || _this$log === void 0 ? void 0 : _this$log.info(`Initializing Bifold container`);
    this._container.registerInstance(_containerApi.TOKENS.SCREEN_PREFACE, _Preface.default);
    this._container.registerInstance(_containerApi.TOKENS.SCREEN_DEVELOPER, _Developer.default);
    this._container.registerInstance(_containerApi.TOKENS.SCREEN_TERMS, {
      screen: _Terms.default,
      version: _Terms.TermsVersion
    });
    this._container.registerInstance(_containerApi.TOKENS.SCREEN_ONBOARDING, _Onboarding.default);
    this._container.registerInstance(_containerApi.TOKENS.STACK_ONBOARDING, _OnboardingStack.default);
    this._container.registerInstance(_containerApi.TOKENS.COMP_BUTTON, _Button.default);
    this._container.registerInstance(_containerApi.TOKENS.GROUP_BY_REFERENT, false);
    this._container.registerInstance(_containerApi.TOKENS.CRED_HELP_ACTION_OVERRIDES, []);
    this._container.registerInstance(_containerApi.TOKENS.OBJECT_ONBOARDING_CONFIG, _defaultStackOptions.DefaultScreenOptionsDictionary);
    this._container.registerInstance(_containerApi.TOKENS.UTIL_LOGGER, new _logger.ConsoleLogger());
    this._container.registerInstance(_containerApi.TOKENS.UTIL_OCA_RESOLVER, new _legacy.DefaultOCABundleResolver(bundle));
    this._container.registerInstance(_containerApi.TOKENS.UTIL_LEDGERS, _indy.default);
    this._container.registerInstance(_containerApi.TOKENS.UTIL_PROOF_TEMPLATE, _ariesBifoldVerifier.useProofRequestTemplates);
    this._container.registerInstance(_containerApi.TOKENS.FN_ONBOARDING_DONE, (dispatch, navigation) => {
      return () => {
        dispatch({
          type: _store.DispatchAction.DID_COMPLETE_TUTORIAL
        });
        navigation.navigate(_navigators.Screens.Terms);
      };
    });
    this._container.registerInstance(_containerApi.TOKENS.LOAD_STATE, async dispatch => {
      const loadState = async (key, updateVal) => {
        const data = await _asyncStorage.default.getItem(key);
        if (data) {
          const dataAsJSON = JSON.parse(data);
          updateVal(dataAsJSON);
        }
      };
      let loginAttempt = _store2.defaultState.loginAttempt;
      let preferences = _store2.defaultState.preferences;
      let migration = _store2.defaultState.migration;
      let tours = _store2.defaultState.tours;
      let onboarding = _store2.defaultState.onboarding;
      await Promise.all([(0, _keychain.loadLoginAttempt)().then(data => {
        if (data) {
          loginAttempt = data;
        }
      }), loadState(_constants.LocalStorageKeys.Preferences, val => preferences = val), loadState(_constants.LocalStorageKeys.Migration, val => migration = val), loadState(_constants.LocalStorageKeys.Tours, val => tours = val), loadState(_constants.LocalStorageKeys.Onboarding, val => onboarding = val)]);
      const state = {
        ..._store2.defaultState,
        loginAttempt: {
          ..._store2.defaultState.loginAttempt,
          ...loginAttempt
        },
        preferences: {
          ..._store2.defaultState.preferences,
          ...preferences
        },
        migration: {
          ..._store2.defaultState.migration,
          ...migration
        },
        tours: {
          ..._store2.defaultState.tours,
          ...tours
        },
        onboarding: {
          ..._store2.defaultState.onboarding,
          ...onboarding
        }
      };
      dispatch({
        type: _store.DispatchAction.STATE_DISPATCH,
        payload: [state]
      });
    });
    return this;
  }
  resolve(token) {
    return this._container.resolve(token);
  }
}
exports.MainContainer = MainContainer;
const SystemContext = /*#__PURE__*/(0, _react.createContext)(undefined);
exports.SystemContext = SystemContext;
const SystemProvider = SystemContext.Provider;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
exports.SystemProvider = SystemProvider;
const useSystem = () => (0, _react.useContext)(SystemContext);
exports.useSystem = useSystem;
//# sourceMappingURL=container-impl.js.map