import { Header, HeaderBackButton, useHeaderHeight } from '@react-navigation/elements';
import React, { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import BiometryControl from '../components/inputs/BiometryControl';
import SafeAreaModal from '../components/modals/SafeAreaModal';
import { TOKENS, useServices } from '../container-api';
import { useAuth } from '../contexts/auth';
import { DispatchAction } from '../contexts/reducers/store';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { HistoryCardType } from '../modules/history/types';
import { useAppAgent } from '../utils/agent';
import PINEnter, { PINEntryUsage } from './PINEnter';
const BackButton = ({
  setCanSeeCheckPIN
}) => /*#__PURE__*/React.createElement(HeaderBackButton, {
  onPress: () => setCanSeeCheckPIN(false),
  tintColor: "white",
  labelVisible: false
});
const ToggleBiometry = () => {
  const [store, dispatch] = useStore();
  const {
    agent
  } = useAppAgent();
  const {
    t
  } = useTranslation();
  const [logger, historyManagerCurried, historyEnabled, historyEventsLogger] = useServices([TOKENS.UTIL_LOGGER, TOKENS.FN_LOAD_HISTORY, TOKENS.HISTORY_ENABLED, TOKENS.HISTORY_EVENTS_LOGGER]);
  const {
    commitWalletToKeychain,
    disableBiometrics
  } = useAuth();
  const [biometryEnabled, setBiometryEnabled] = useState(store.preferences.useBiometry);
  const [canSeeCheckPIN, setCanSeeCheckPIN] = useState(false);
  const {
    TextTheme,
    ColorPallet
  } = useTheme();
  const headerHeight = useHeaderHeight();
  const logHistoryRecord = useCallback(type => {
    try {
      if (!(agent && historyEnabled)) {
        logger.trace(`[${ToggleBiometry.name}]:[logHistoryRecord] Skipping history log, either history function disabled or agent undefined!`);
        return;
      }
      const historyManager = historyManagerCurried(agent);

      /** Save history record for card accepted */
      const recordData = {
        type: type,
        message: type,
        createdAt: new Date()
      };
      historyManager.saveHistory(recordData);
    } catch (err) {
      logger.error(`[${ToggleBiometry.name}]:[logHistoryRecord] Error saving history: ${err}`);
    }
  }, [agent, historyEnabled, logger, historyManagerCurried]);
  const onSwitchToggleAllowed = useCallback(() => {
    setCanSeeCheckPIN(true);
    if (historyEventsLogger.logToggleBiometry && store.onboarding.didAgreeToTerms && store.onboarding.didConsiderBiometry) {
      const type = HistoryCardType.ActivateBiometry;
      logHistoryRecord(type);
    }
  }, [historyEventsLogger.logToggleBiometry, logHistoryRecord, store.onboarding.didAgreeToTerms, store.onboarding.didConsiderBiometry]);
  const handleBiometryToggle = useCallback(newValue => {
    if (newValue === biometryEnabled) return;
    onSwitchToggleAllowed();
  }, [biometryEnabled, onSwitchToggleAllowed]);
  const onAuthenticationComplete = useCallback(status => {
    // If successfully authenticated the toggle may proceed.
    if (status) {
      const newValue = !biometryEnabled;
      setBiometryEnabled(newValue);
      if (newValue) {
        commitWalletToKeychain(newValue).then(() => {
          dispatch({
            type: DispatchAction.USE_BIOMETRY,
            payload: [newValue]
          });
        });
      } else {
        disableBiometrics().then(() => {
          dispatch({
            type: DispatchAction.USE_BIOMETRY,
            payload: [newValue]
          });
        });
      }
      if (historyEventsLogger.logToggleBiometry && store.onboarding.didAgreeToTerms && store.onboarding.didConsiderBiometry) {
        const type = HistoryCardType.DeactivateBiometry;
        logHistoryRecord(type);
      }
    }
    setCanSeeCheckPIN(false);
  }, [biometryEnabled, commitWalletToKeychain, disableBiometrics, dispatch, historyEventsLogger.logToggleBiometry, logHistoryRecord, store.onboarding.didAgreeToTerms, store.onboarding.didConsiderBiometry]);
  const renderHeaderLeft = useCallback(() => /*#__PURE__*/React.createElement(BackButton, {
    setCanSeeCheckPIN: () => setCanSeeCheckPIN(false)
  }), [setCanSeeCheckPIN]);
  return /*#__PURE__*/React.createElement(BiometryControl, {
    biometryEnabled: biometryEnabled,
    onBiometryToggle: handleBiometryToggle
  }, /*#__PURE__*/React.createElement(SafeAreaModal, {
    style: {
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    visible: canSeeCheckPIN,
    transparent: false,
    animationType: 'slide',
    presentationStyle: "fullScreen"
  }, /*#__PURE__*/React.createElement(Header, {
    title: t('Screens.EnterPIN'),
    headerTitleStyle: TextTheme.headerTitle,
    headerStyle: {
      height: headerHeight
    },
    headerLeft: renderHeaderLeft
  }), /*#__PURE__*/React.createElement(PINEnter, {
    usage: PINEntryUsage.ChangeBiometrics,
    setAuthenticated: onAuthenticationComplete,
    onCancelAuth: setCanSeeCheckPIN
  })));
};
export default ToggleBiometry;
//# sourceMappingURL=ToggleBiometry.js.map