import { DidExchangeState } from '@aries-framework/core';
import { useConnectionById } from '@aries-framework/react-hooks';
import { CommonActions, useFocusEffect } from '@react-navigation/native';
import React, { useCallback, useEffect, useMemo, useReducer, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { AccessibilityInfo, BackHandler, Modal, ScrollView, StyleSheet, Text, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Button, { ButtonType } from '../components/buttons/Button';
import { useAnimatedComponents } from '../contexts/animated-components';
import { useConfiguration } from '../contexts/configuration';
import { useTheme } from '../contexts/theme';
import { useOutOfBandByConnectionId } from '../hooks/connections';
import { useNotifications } from '../hooks/notifications';
import { Screens, TabStacks, Stacks } from '../types/navigators';
import { testIdWithKey } from '../utils/testable';
const Connection = _ref => {
  let {
    navigation,
    route
  } = _ref;
  // TODO(jl): When implementing goal codes the `autoRedirectConnectionToHome`
  // logic should be: if this property is set, rather than showing the
  // delay message, the user should be redirected to the home screen.
  const {
    connectionTimerDelay,
    autoRedirectConnectionToHome
  } = useConfiguration();
  const connTimerDelay = connectionTimerDelay ?? 10000; // in ms
  const {
    connectionId,
    threadId
  } = route.params;
  const timerRef = useRef(null);
  const connection = connectionId ? useConnectionById(connectionId) : undefined;
  const {
    t
  } = useTranslation();
  const {
    notifications
  } = useNotifications();
  const {
    ColorPallet,
    TextTheme
  } = useTheme();
  const {
    ConnectionLoading
  } = useAnimatedComponents();
  const oobRecord = useOutOfBandByConnectionId(connectionId ?? '');
  const goalCode = oobRecord === null || oobRecord === void 0 ? void 0 : oobRecord.outOfBandInvitation.goalCode;
  const merge = (current, next) => ({
    ...current,
    ...next
  });
  const [state, dispatch] = useReducer(merge, {
    isVisible: true,
    isInitialized: false,
    shouldShowDelayMessage: false,
    connectionIsActive: false
  });
  const styles = StyleSheet.create({
    container: {
      height: '100%',
      backgroundColor: ColorPallet.brand.modalPrimaryBackground,
      padding: 20
    },
    image: {
      marginTop: 20
    },
    messageContainer: {
      alignItems: 'center'
    },
    messageText: {
      fontWeight: TextTheme.normal.fontWeight,
      textAlign: 'center',
      marginTop: 30
    },
    controlsContainer: {
      marginTop: 'auto',
      margin: 20
    },
    delayMessageText: {
      textAlign: 'center',
      marginTop: 20
    }
  });
  const goalCodeAction = goalCode => {
    const codes = {
      'aries.vc.verify': () => navigation.navigate(Screens.ProofRequest, {
        proofId: state.notificationRecord.id
      }),
      'aries.vc.issue': () => navigation.navigate(Screens.CredentialOffer, {
        credentialId: state.notificationRecord.id
      })
    };
    let action = codes[goalCode];
    if (action === undefined) {
      const matchCode = Object.keys(codes).find(code => goalCode.startsWith(code));
      action = codes[matchCode ?? ''];
      if (action === undefined) {
        throw new Error('Unhandled goal code type');
      }
    }
    return action;
  };
  const startTimer = () => {
    if (!state.isInitialized) {
      timerRef.current = setTimeout(() => {
        dispatch({
          shouldShowDelayMessage: true
        });
        timerRef.current = null;
      }, connTimerDelay);
      dispatch({
        isInitialized: true
      });
    }
  };
  const abortTimer = () => {
    if (timerRef.current) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
  };
  const onDismissModalTouched = () => {
    var _navigation$getParent;
    dispatch({
      shouldShowDelayMessage: false,
      isVisible: false
    });
    (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 ? void 0 : _navigation$getParent.navigate(TabStacks.HomeStack, {
      screen: Screens.Home
    });
  };
  useEffect(() => {
    const backHandler = BackHandler.addEventListener('hardwareBackPress', () => true);
    return () => backHandler.remove();
  }, []);
  useEffect(() => {
    if (state.shouldShowDelayMessage && !state.notificationRecord) {
      if (autoRedirectConnectionToHome) {
        var _navigation$getParent2;
        dispatch({
          shouldShowDelayMessage: false,
          isVisible: false
        });
        (_navigation$getParent2 = navigation.getParent()) === null || _navigation$getParent2 === void 0 ? void 0 : _navigation$getParent2.navigate(TabStacks.HomeStack, {
          screen: Screens.Home
        });
      } else {
        AccessibilityInfo.announceForAccessibility(t('Connection.TakingTooLong'));
      }
    }
  }, [state.shouldShowDelayMessage]);
  useEffect(() => {
    // for non-connectionless, invalid connections stay in the below state
    if (connection && connection.state === DidExchangeState.RequestSent) {
      return;
    }
    if (!connectionId && !oobRecord && !goalCode && state.notificationRecord && state.notificationRecord.state === 'request-received') {
      navigation.replace(Screens.ProofRequest, {
        proofId: state.notificationRecord.id
      });
      dispatch({
        isVisible: false
      });
      return;
    }
    if (connectionId && oobRecord && (!goalCode || !goalCode.startsWith('aries.vc.verify') && !goalCode.startsWith('aries.vc.issue'))) {
      var _navigation$getParent3;
      // No goal code, we don't know what to expect next,
      // navigate to the chat screen and set home screen as only history
      (_navigation$getParent3 = navigation.getParent()) === null || _navigation$getParent3 === void 0 ? void 0 : _navigation$getParent3.dispatch(CommonActions.reset({
        index: 1,
        routes: [{
          name: Stacks.TabStack
        }, {
          name: Screens.Chat,
          params: {
            connectionId
          }
        }]
      }));
      dispatch({
        isVisible: false
      });
      return;
    }
    if (state.notificationRecord && goalCode) {
      goalCodeAction(goalCode)();
    }
  }, [connection, connection === null || connection === void 0 ? void 0 : connection.state, oobRecord, goalCode, state.notificationRecord]);
  useMemo(() => {
    startTimer();
    return () => abortTimer;
  }, []);
  useFocusEffect(useCallback(() => {
    startTimer();
    return () => abortTimer;
  }, []));
  useEffect(() => {
    if (state.isVisible) {
      for (const notification of notifications) {
        // no action taken for BasicMessageRecords
        if (notification.type !== 'BasicMessageRecord') {
          if (!state.notificationRecord && (connectionId && notification.connectionId === connectionId || threadId && notification.threadId == threadId)) {
            dispatch({
              notificationRecord: notification,
              isVisible: false
            });
            break;
          }
          if (!connection && notification.state === 'request-received') {
            dispatch({
              notificationRecord: notification,
              isVisible: false
            });
            break;
          }
        }
      }
    }
  }, [notifications]);
  return /*#__PURE__*/React.createElement(Modal, {
    visible: state.isVisible,
    transparent: true,
    animationType: 'slide',
    onRequestClose: () => {
      dispatch({
        isVisible: false
      });
    }
  }, /*#__PURE__*/React.createElement(SafeAreaView, {
    style: {
      backgroundColor: ColorPallet.brand.modalPrimaryBackground
    }
  }, /*#__PURE__*/React.createElement(ScrollView, {
    style: [styles.container]
  }, /*#__PURE__*/React.createElement(View, {
    style: [styles.messageContainer]
  }, /*#__PURE__*/React.createElement(Text, {
    style: [TextTheme.modalHeadingThree, styles.messageText],
    testID: testIdWithKey('CredentialOnTheWay')
  }, t('Connection.JustAMoment'))), /*#__PURE__*/React.createElement(View, {
    style: [styles.image]
  }, /*#__PURE__*/React.createElement(ConnectionLoading, null)), state.shouldShowDelayMessage && /*#__PURE__*/React.createElement(Text, {
    style: [TextTheme.modalNormal, styles.delayMessageText],
    testID: testIdWithKey('TakingTooLong')
  }, t('Connection.TakingTooLong'))), /*#__PURE__*/React.createElement(View, {
    style: [styles.controlsContainer]
  }, /*#__PURE__*/React.createElement(Button, {
    title: t('Loading.BackToHome'),
    accessibilityLabel: t('Loading.BackToHome'),
    testID: testIdWithKey('BackToHome'),
    onPress: onDismissModalTouched,
    buttonType: ButtonType.ModalSecondary
  }))));
};
export default Connection;
//# sourceMappingURL=Connection.js.map