function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { ProofState } from '@aries-framework/core';
import { useAgent, useProofByState } from '@aries-framework/react-hooks';
import { ProofMetadata } from '@hyperledger/aries-bifold-verifier';
import { useNavigation } from '@react-navigation/core';
import { createStackNavigator } from '@react-navigation/stack';
import { parseUrl } from 'query-string';
import React, { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { AppState, DeviceEventEmitter } from 'react-native';
import HeaderButton, { ButtonLocation } from '../components/buttons/HeaderButton';
import { EventTypes, walletTimeout } from '../constants';
import { useAuth } from '../contexts/auth';
import { useConfiguration } from '../contexts/configuration';
import { DispatchAction } from '../contexts/reducers/store';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { useDeepLinks } from '../hooks/deep-links';
import AttemptLockout from '../screens/AttemptLockout';
import Chat from '../screens/Chat';
import NameWallet from '../screens/NameWallet';
import Onboarding from '../screens/Onboarding';
import { createCarouselStyle } from '../screens/OnboardingPages';
import PINCreate from '../screens/PINCreate';
import PINEnter from '../screens/PINEnter';
import { BifoldError } from '../types/error';
import { Screens, Stacks, TabStacks } from '../types/navigators';
import { connectFromInvitation, getOobDeepLink } from '../utils/helpers';
import { testIdWithKey } from '../utils/testable';
import ConnectStack from './ConnectStack';
import ContactStack from './ContactStack';
import DeliveryStack from './DeliveryStack';
import NotificationStack from './NotificationStack';
import ProofRequestStack from './ProofRequestStack';
import SettingStack from './SettingStack';
import TabStack from './TabStack';
import { createDefaultStackOptions } from './defaultStackOptions';
const RootStack = () => {
  const [state, dispatch] = useStore();
  const {
    removeSavedWalletSecret
  } = useAuth();
  const {
    agent
  } = useAgent();
  const appState = useRef(AppState.currentState);
  const [backgroundTime, setBackgroundTime] = useState(undefined);
  const [prevAppStateVisible, setPrevAppStateVisible] = useState('');
  const [appStateVisible, setAppStateVisible] = useState('');
  const {
    t
  } = useTranslation();
  const navigation = useNavigation();
  const theme = useTheme();
  const defaultStackOptions = createDefaultStackOptions(theme);
  const OnboardingTheme = theme.OnboardingTheme;
  const {
    pages,
    terms,
    splash,
    useBiometry,
    developer,
    preface,
    showPreface,
    enableImplicitInvitations,
    enableReuseConnections,
    disableOnboardingSkip
  } = useConfiguration();
  useDeepLinks();

  // remove connection on mobile verifier proofs if proof is rejected regardless of if it has been opened
  const declinedProofs = useProofByState([ProofState.Declined, ProofState.Abandoned]);
  useEffect(() => {
    declinedProofs.forEach(proof => {
      var _proof$metadata;
      const meta = proof === null || proof === void 0 ? void 0 : (_proof$metadata = proof.metadata) === null || _proof$metadata === void 0 ? void 0 : _proof$metadata.get(ProofMetadata.customMetadata);
      if (meta !== null && meta !== void 0 && meta.delete_conn_after_seen) {
        agent === null || agent === void 0 ? void 0 : agent.connections.deleteById((proof === null || proof === void 0 ? void 0 : proof.connectionId) ?? '').catch(() => {});
        proof === null || proof === void 0 ? void 0 : proof.metadata.set(ProofMetadata.customMetadata, {
          ...meta,
          delete_conn_after_seen: false
        });
      }
    });
  }, [declinedProofs, state.preferences.useDataRetention]);
  const lockoutUser = async () => {
    if (agent && state.authentication.didAuthenticate) {
      // make sure agent is shutdown so wallet isn't still open
      removeSavedWalletSecret();
      await agent.wallet.close();
      await agent.shutdown();
      dispatch({
        type: DispatchAction.DID_AUTHENTICATE,
        payload: [{
          didAuthenticate: false
        }]
      });
      dispatch({
        type: DispatchAction.LOCKOUT_UPDATED,
        payload: [{
          displayNotification: true
        }]
      });
    }
  };

  // handle deeplink events
  useEffect(() => {
    async function handleDeepLink(deepLink) {
      // If it's just the general link with no params, set link inactive and do nothing
      if (deepLink.endsWith('//')) {
        dispatch({
          type: DispatchAction.ACTIVE_DEEP_LINK,
          payload: [undefined]
        });
        return;
      }
      try {
        var _receivedInvitation$c;
        // Try connection based
        const receivedInvitation = await connectFromInvitation(deepLink, agent, enableImplicitInvitations, enableReuseConnections);
        navigation.navigate(Stacks.ConnectionStack, {
          screen: Screens.Connection,
          params: {
            connectionId: receivedInvitation === null || receivedInvitation === void 0 ? void 0 : (_receivedInvitation$c = receivedInvitation.connectionRecord) === null || _receivedInvitation$c === void 0 ? void 0 : _receivedInvitation$c.id
          }
        });
      } catch {
        try {
          // Try connectionless here
          const queryParams = parseUrl(deepLink).query;
          const param = queryParams['d_m'] ?? queryParams['c_i'];
          // if missing both of the required params, don't attempt to open OOB
          if (!param) {
            dispatch({
              type: DispatchAction.ACTIVE_DEEP_LINK,
              payload: [undefined]
            });
            return;
          }
          const message = await getOobDeepLink(deepLink, agent);
          navigation.navigate(Stacks.ConnectionStack, {
            screen: Screens.Connection,
            params: {
              threadId: message['@id']
            }
          });
        } catch (err) {
          const error = new BifoldError(t('Error.Title1039'), t('Error.Message1039'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1039);
          DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
        }
      }

      // set deeplink as inactive
      dispatch({
        type: DispatchAction.ACTIVE_DEEP_LINK,
        payload: [undefined]
      });
    }
    if (agent && state.deepLink.activeDeepLink && state.authentication.didAuthenticate) {
      handleDeepLink(state.deepLink.activeDeepLink);
    }
  }, [agent, state.deepLink.activeDeepLink, state.authentication.didAuthenticate]);
  useEffect(() => {
    AppState.addEventListener('change', nextAppState => {
      if (appState.current.match(/active/) && nextAppState.match(/inactive|background/)) {
        //update time that app gets put in background
        setBackgroundTime(Date.now());
      }
      setPrevAppStateVisible(appState.current);
      appState.current = nextAppState;
      setAppStateVisible(appState.current);
    });
  }, []);
  useEffect(() => {
    if (appStateVisible.match(/active/) && prevAppStateVisible.match(/inactive|background/) && backgroundTime) {
      // prevents the user from being locked out during metro reloading
      setPrevAppStateVisible(appStateVisible);
      //lock user out after 5 minutes
      if (!state.preferences.preventAutoLock && walletTimeout && backgroundTime && Date.now() - backgroundTime > walletTimeout) {
        lockoutUser();
      }
    }
  }, [appStateVisible, prevAppStateVisible, backgroundTime]);
  const onTutorialCompleted = () => {
    dispatch({
      type: DispatchAction.DID_COMPLETE_TUTORIAL
    });
    navigation.navigate(Screens.Terms);
  };
  const onAuthenticated = status => {
    if (!status) {
      return;
    }
    dispatch({
      type: DispatchAction.DID_AUTHENTICATE
    });
  };
  const authStack = () => {
    const Stack = createStackNavigator();
    return /*#__PURE__*/React.createElement(Stack.Navigator, {
      initialRouteName: Screens.Splash,
      screenOptions: {
        ...defaultStackOptions,
        headerShown: false
      }
    }, /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Splash,
      component: splash
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.EnterPIN,
      options: () => ({
        title: t('Screens.EnterPIN'),
        headerShown: true,
        headerLeft: () => false,
        rightLeft: () => false
      })
    }, props => /*#__PURE__*/React.createElement(PINEnter, _extends({}, props, {
      setAuthenticated: onAuthenticated
    }))), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.AttemptLockout,
      component: AttemptLockout,
      options: {
        headerShown: true,
        headerLeft: () => null
      }
    }));
  };
  const mainStack = () => {
    const Stack = createStackNavigator();

    // This function is to make the fade in behavior of both iOS and Android consistent for the settings menu
    const forFade = _ref => {
      let {
        current
      } = _ref;
      return {
        cardStyle: {
          opacity: current.progress
        }
      };
    };
    return /*#__PURE__*/React.createElement(Stack.Navigator, {
      initialRouteName: Screens.Splash,
      screenOptions: {
        ...defaultStackOptions,
        headerShown: false
      }
    }, /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Splash,
      component: splash
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.TabStack,
      component: TabStack
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Chat,
      component: Chat,
      options: _ref2 => {
        let {
          navigation
        } = _ref2;
        return {
          headerShown: true,
          title: t('Screens.CredentialOffer'),
          headerLeft: () => /*#__PURE__*/React.createElement(HeaderButton, {
            buttonLocation: ButtonLocation.Left,
            accessibilityLabel: t('Global.Back'),
            testID: testIdWithKey('BackButton'),
            onPress: () => {
              navigation.navigate(TabStacks.HomeStack, {
                screen: Screens.Home
              });
            },
            icon: "arrow-left"
          })
        };
      }
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.ConnectStack,
      component: ConnectStack
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.SettingStack,
      component: SettingStack,
      options: {
        cardStyleInterpolator: forFade
      }
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.ContactStack,
      component: ContactStack
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.NotificationStack,
      component: NotificationStack
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.ConnectionStack,
      component: DeliveryStack,
      options: {
        gestureEnabled: false
      }
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Stacks.ProofRequestsStack,
      component: ProofRequestStack
    }));
  };
  const onboardingStack = () => {
    const Stack = createStackNavigator();
    const carousel = createCarouselStyle(OnboardingTheme);
    return /*#__PURE__*/React.createElement(Stack.Navigator, {
      initialRouteName: Screens.Splash,
      screenOptions: {
        ...defaultStackOptions,
        headerShown: false
      }
    }, /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Splash,
      component: splash
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Preface,
      component: preface,
      options: {
        title: t('Screens.Preface'),
        headerShown: true
      }
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Onboarding,
      options: () => ({
        title: t('Screens.Onboarding'),
        headerTintColor: OnboardingTheme.headerTintColor,
        headerShown: true,
        gestureEnabled: false,
        headerLeft: () => false
      })
    }, props => /*#__PURE__*/React.createElement(Onboarding, _extends({}, props, {
      nextButtonText: t('Global.Next'),
      previousButtonText: t('Global.Back'),
      pages: pages(onTutorialCompleted, OnboardingTheme),
      disableSkip: disableOnboardingSkip,
      style: carousel
    }))), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Terms,
      options: () => ({
        title: t('Screens.Terms'),
        headerTintColor: OnboardingTheme.headerTintColor,
        headerShown: true,
        headerLeft: () => false,
        rightLeft: () => false
      }),
      component: terms
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.CreatePIN,
      options: () => ({
        title: t('Screens.CreatePIN'),
        headerShown: true,
        headerLeft: () => false,
        rightLeft: () => false
      })
    }, props => /*#__PURE__*/React.createElement(PINCreate, _extends({}, props, {
      setAuthenticated: onAuthenticated
    }))), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.NameWallet,
      options: () => ({
        title: t('Screens.NameWallet'),
        headerTintColor: OnboardingTheme.headerTintColor,
        headerShown: true,
        headerLeft: () => false,
        rightLeft: () => false
      }),
      component: NameWallet
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.UseBiometry,
      options: () => ({
        title: t('Screens.Biometry'),
        headerTintColor: OnboardingTheme.headerTintColor,
        headerShown: true,
        headerLeft: () => false,
        rightLeft: () => false
      }),
      component: useBiometry
    }), /*#__PURE__*/React.createElement(Stack.Screen, {
      name: Screens.Developer,
      component: developer,
      options: {
        ...defaultStackOptions,
        title: t('Screens.Developer'),
        headerBackTestID: testIdWithKey('Back')
      }
    }));
  };
  if ((!showPreface || state.onboarding.didSeePreface) && state.onboarding.didAgreeToTerms && state.onboarding.didCompleteTutorial && state.onboarding.didCreatePIN && (!state.preferences.enableWalletNaming || state.onboarding.didNameWallet) && state.onboarding.didConsiderBiometry) {
    return state.authentication.didAuthenticate ? mainStack() : authStack();
  }
  return onboardingStack();
};
export default RootStack;
//# sourceMappingURL=RootStack.js.map