function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import { useAgent } from '@credo-ts/react-hooks';
import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
import { AppState, PanResponder, View } from 'react-native';
import { useAuth } from './auth';
import { useStore } from './store';
import { DispatchAction } from './reducers/store';
import { TOKENS, useServices } from '../container-api';
import { defaultAutoLockTime } from '../constants';

// number of minutes before the timeout action is triggered
// a value of 0 will never trigger the lock out action and
// an undefined value will default to 5 minutes
export const AutoLockTime = {
  OneMinute: 1,
  ThreeMinutes: 3,
  FiveMinutes: 5,
  Never: 0
};
export const ActivityContext = /*#__PURE__*/createContext(null);
export const ActivityProvider = ({
  children
}) => {
  const [logger] = useServices([TOKENS.UTIL_LOGGER]);
  const [store, dispatch] = useStore();
  const {
    agent
  } = useAgent();
  const {
    removeSavedWalletSecret
  } = useAuth();
  const lastActiveTimeRef = useRef(undefined);
  const timeoutInMilliseconds = useRef((store.preferences.autoLockTime ?? defaultAutoLockTime) * 60000);
  const inactivityTimeoutRef = useRef(null);
  const prevAppStateStatusRef = useRef(AppState.currentState);
  const [appStateStatus, setAppStateStatus] = useState(AppState.currentState);
  const panResponder = useRef(PanResponder.create({
    onStartShouldSetPanResponderCapture: () => {
      // some user interaction detected, reset timeout
      resetInactivityTimeout(timeoutInMilliseconds.current);

      // returns false so the PanResponder doesn't consume the touch event
      return false;
    }
  })).current;
  const lockOutUser = useCallback(async () => {
    if (!store.authentication.didAuthenticate) {
      return;
    }
    try {
      removeSavedWalletSecret();
      await (agent === null || agent === void 0 ? void 0 : agent.shutdown());
      logger.info('Agent shutdown successfully');
    } catch (error) {
      logger.error(`Error during agent shutdown: ${error}`);
    }
    dispatch({
      type: DispatchAction.DID_AUTHENTICATE,
      payload: [false]
    });
    dispatch({
      type: DispatchAction.LOCKOUT_UPDATED,
      payload: [{
        displayNotification: true
      }]
    });
  }, [agent, removeSavedWalletSecret, dispatch, logger, store.authentication.didAuthenticate]);
  const clearInactivityTimeoutIfExists = useCallback(() => {
    if (inactivityTimeoutRef.current) {
      clearTimeout(inactivityTimeoutRef.current);
    }
  }, []);
  const resetInactivityTimeout = useCallback(async milliseconds => {
    clearInactivityTimeoutIfExists();
    lastActiveTimeRef.current = Date.now();

    // do not set timeout if timeout duration is set to 0
    if (milliseconds > 0) {
      // create new timeout
      inactivityTimeoutRef.current = setTimeout(async () => {
        await lockOutUser();
      }, milliseconds);
    }
  }, [clearInactivityTimeoutIfExists, lockOutUser]);
  useEffect(() => {
    // listener for backgrounding / foregrounding
    const eventSubscription = AppState.addEventListener('change', async nextAppState => {
      // if going into the background
      if (['active', 'inactive'].includes(prevAppStateStatusRef.current) && nextAppState === 'background') {
        // remove timeout when backgrounded as timeout refs can be lost when app is backgrounded
        clearInactivityTimeoutIfExists();

        // if going to background after agent initialization, stop message pickup
        if (agent !== null && agent !== void 0 && agent.isInitialized) {
          try {
            await agent.mediationRecipient.stopMessagePickup();
            logger.info('Stopped agent message pickup');
          } catch (err) {
            logger.error(`Error stopping agent message pickup, ${err}`);
          }
        }
      }

      // if coming to the foreground
      if (prevAppStateStatusRef.current === 'background' && ['active', 'inactive'].includes(nextAppState)) {
        // if app was in background for longer than allowed time, lock out user
        if (lastActiveTimeRef.current && Date.now() - lastActiveTimeRef.current >= timeoutInMilliseconds.current && timeoutInMilliseconds.current > 0) {
          await lockOutUser();
        } else if (agent !== null && agent !== void 0 && agent.isInitialized) {
          // otherwise if agent is initialized, restart message pickup
          try {
            await agent.mediationRecipient.initiateMessagePickup();
            logger.info('Restarted agent message pickup');
          } catch (err) {
            logger.error(`Error restarting agent message pickup, ${err}`);
          }
        }

        // app coming into the foreground is 'user activity', reset timeout
        await resetInactivityTimeout(timeoutInMilliseconds.current);
      }
      prevAppStateStatusRef.current = nextAppState;
      setAppStateStatus(nextAppState);
    });

    // initial timeout setup
    resetInactivityTimeout(timeoutInMilliseconds.current);
    return () => {
      clearInactivityTimeoutIfExists();
      eventSubscription.remove();
    };
  }, [clearInactivityTimeoutIfExists, lockOutUser, resetInactivityTimeout, timeoutInMilliseconds, agent, logger]);
  useEffect(() => {
    // user has updated settings for auto lock time
    timeoutInMilliseconds.current = store.preferences.autoLockTime * 60000;
  }, [store.preferences.autoLockTime]);
  return /*#__PURE__*/React.createElement(ActivityContext.Provider, {
    value: {
      appStateStatus
    }
  }, /*#__PURE__*/React.createElement(View, _extends({
    style: {
      flex: 1
    }
  }, panResponder.panHandlers), children));
};
export const useActivity = () => useContext(ActivityContext);
//# sourceMappingURL=activity.js.map