import React, { useCallback, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { DeviceEventEmitter, StyleSheet, View } from 'react-native';
import { ButtonType } from '../components/buttons/Button-api';
import PINInput from '../components/inputs/PINInput';
import PINValidationHelper from '../components/misc/PINValidationHelper';
import AlertModal from '../components/modals/AlertModal';
import ScreenWrapper from '../components/views/ScreenWrapper';
import { EventTypes, minPINLength } from '../constants';
import { TOKENS, useServices } from '../container-api';
import { useAnimatedComponents } from '../contexts/animated-components';
import { useTheme } from '../contexts/theme';
import usePreventScreenCapture from '../hooks/screen-capture';
import { usePINValidation } from '../hooks/usePINValidation';
import VerifyPINModal from '../components/modals/VerifyPINModal';
import { BifoldError } from '../types/error';
import { Screens } from '../types/navigators';
import { testIdWithKey } from '../utils/testable';
import { PINEntryUsage } from './PINVerify';
import ConfirmPINModal, { ConfirmPINModalUsage } from '../components/modals/ConfirmPINModal';
import { useAppAgent } from '../utils/agent';
import { HistoryCardType } from '../modules/history/types';
import { useAuth } from '../contexts/auth';
import { useStore } from '../contexts/store';
const PINChange = ({
  navigation
}) => {
  const {
    checkWalletPIN,
    rekeyWallet
  } = useAuth();
  const {
    agent
  } = useAppAgent();
  const [PIN, setPIN] = useState('');
  const [PINTwo, setPINTwo] = useState('');
  const [PINOld, setPINOld] = useState('');
  const [store] = useStore();
  const [isLoading, setIsLoading] = useState(false);
  const {
    t
  } = useTranslation();
  const {
    ColorPalette,
    Spacing
  } = useTheme();
  const {
    ButtonLoading
  } = useAnimatedComponents();
  const createPINButtonRef = useRef(null);
  const [Button, inlineMessages, logger, historyManagerCurried, historyEnabled, historyEventsLogger, {
    preventScreenCapture,
    PINScreensConfig
  }, PINHeader] = useServices([TOKENS.COMP_BUTTON, TOKENS.INLINE_ERRORS, TOKENS.UTIL_LOGGER, TOKENS.FN_LOAD_HISTORY, TOKENS.HISTORY_ENABLED, TOKENS.HISTORY_EVENTS_LOGGER, TOKENS.CONFIG, TOKENS.COMPONENT_PIN_HEADER]);
  const [verifyPINModalVisible, setVerifyPINModalVisible] = useState(PINScreensConfig.useNewPINDesign);
  const [confirmPINModalVisible, setPINConfirmModalVisible] = useState(false);
  const {
    PINValidations,
    validatePINEntry,
    inlineMessageField1,
    inlineMessageField2,
    modalState,
    PINSecurity,
    clearModal,
    setModalState,
    comparePINEntries,
    setInlineMessageField1,
    setInlineMessageField2
  } = usePINValidation(PIN);
  usePreventScreenCapture(preventScreenCapture);
  const style = StyleSheet.create({
    screenContainer: {
      height: '100%',
      backgroundColor: ColorPalette.brand.primaryBackground,
      padding: Spacing.md,
      justifyContent: 'space-between'
    },
    // below used as helpful labels for views, no properties needed atp
    contentContainer: {},
    controlsContainer: {},
    loadingContainer: {
      justifyContent: 'center',
      alignItems: 'center'
    }
  });
  const onVerifyPINModalBackPressed = useCallback(() => {
    navigation.pop();
  }, [navigation]);
  const onConfirmPINModalBackPressed = useCallback(() => {
    setPINConfirmModalVisible(false);
  }, [setPINConfirmModalVisible]);
  const onAuthenticationComplete = useCallback(pin => {
    setPINOld(pin);
    setVerifyPINModalVisible(false);
  }, [setPINOld, setVerifyPINModalVisible]);
  const onCancelAuth = useCallback(() => {
    navigation.navigate(Screens.Settings);
  }, [navigation]);
  const checkOldPIN = useCallback(async PIN => {
    const valid = await checkWalletPIN(PIN);
    if (!valid) {
      setModalState({
        visible: true,
        title: t('PINCreate.InvalidPIN'),
        message: t(`PINCreate.Message.OldPINIncorrect`),
        onModalDismiss: clearModal
      });
    }
    return valid;
  }, [checkWalletPIN, t, setModalState, clearModal]);
  const logHistoryRecord = useCallback(() => {
    try {
      if (!(agent && historyEnabled)) {
        logger.trace(`[${PINChange.name}]:[logHistoryRecord] Skipping history log, either history function disabled or agent undefined`);
        return;
      }
      const historyManager = historyManagerCurried(agent);
      /** Save history record for pin edited */
      const recordData = {
        type: HistoryCardType.PinChanged,
        message: HistoryCardType.PinChanged,
        createdAt: new Date()
      };
      historyManager.saveHistory(recordData);
    } catch (err) {
      logger.error(`[${PINChange.name}]:[logHistoryRecord] Error saving history: ${err}`);
    }
  }, [agent, historyEnabled, logger, historyManagerCurried]);
  const handleConfirmPINModal = async userPinInput => {
    try {
      setIsLoading(true);
      const valid = validatePINEntry(userPinInput, userPinInput);
      if (valid) {
        setPINConfirmModalVisible(true);
      }
    } catch (err) {
      const error = new BifoldError(t('Error.Title1049'), t('Error.Message1049'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1049);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    } finally {
      setIsLoading(false);
    }
  };
  const handleChangePinTap = async () => {
    try {
      setIsLoading(true);
      const valid = validatePINEntry(PIN, PINTwo);
      if (valid) {
        const oldPinValid = await checkOldPIN(PINOld);
        if (oldPinValid) {
          const success = await rekeyWallet(agent, PINOld, PIN, store.preferences.useBiometry);
          if (success) {
            if (historyEventsLogger.logPinChanged) {
              logHistoryRecord();
            }
            setModalState({
              visible: true,
              title: t('PINChange.PinChangeSuccessTitle'),
              message: t('PINChange.PinChangeSuccessMessage'),
              onModalDismiss: () => {
                navigation.navigate(Screens.Settings);
                clearModal();
              }
            });
          }
        }
      }
    } catch (err) {
      const error = new BifoldError(t('Error.Title1049'), t('Error.Message1049'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1049);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    } finally {
      setIsLoading(false);
    }
  };
  const handleConfirmPIN = async userPinInput => {
    try {
      setIsLoading(true);
      const valid = comparePINEntries(PIN, userPinInput);
      if (valid) {
        const success = await rekeyWallet(agent, PINOld, PIN, store.preferences.useBiometry);
        if (success) {
          if (historyEventsLogger.logPinChanged) {
            logHistoryRecord();
          }
          if (PINScreensConfig.useNewPINDesign) {
            setPINConfirmModalVisible(false);
            navigation.navigate(Screens.ChangePINSuccess);
          } else {
            navigation.navigate(Screens.Settings);
          }
        }
      }
    } catch (err) {
      const error = new BifoldError(t('Error.Title1049'), t('Error.Message1049'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1049);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    } finally {
      setIsLoading(false);
    }
  };
  const isContinueDisabled = useMemo(() => {
    if (inlineMessages || isLoading) {
      return false;
    }
    return PIN === '' || PIN.length < minPINLength;
  }, [inlineMessages, isLoading, PIN]);
  return /*#__PURE__*/React.createElement(ScreenWrapper, {
    keyboardActive: true,
    padded: false
  }, /*#__PURE__*/React.createElement(View, {
    style: style.screenContainer
  }, /*#__PURE__*/React.createElement(View, {
    style: style.contentContainer
  }, /*#__PURE__*/React.createElement(PINHeader, {
    updatePin: true
  }), !PINScreensConfig.useNewPINDesign && /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINChange.EnterOldPINTitle'),
    testID: testIdWithKey('EnterOldPIN'),
    accessibilityLabel: t('PINChange.EnterOldPIN'),
    onPINChanged: p => {
      setPINOld(p);
    },
    onSubmitEditing: handleChangePinTap
  }), /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINChange.EnterPINTitle'),
    onPINChanged: async userPinInput => {
      setInlineMessageField1(undefined);
      setPIN(userPinInput);
      if (userPinInput.length === minPINLength && PINScreensConfig.useNewPINDesign) {
        await handleConfirmPINModal(userPinInput);
      }
    },
    testID: testIdWithKey('EnterPIN'),
    accessibilityLabel: t('PINCreate.EnterPIN'),
    autoFocus: false,
    inlineMessage: inlineMessageField1,
    onSubmitEditing: handleChangePinTap
  }), !PINScreensConfig.useNewPINDesign && /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINCreateConfirm.PINInputLabel'),
    onPINChanged: async userPinInput => {
      setInlineMessageField2(undefined);
      setPINTwo(userPinInput);
    },
    testID: testIdWithKey('ConfirmPIN'),
    accessibilityLabel: t('PINCreateConfirm.PINInputLabel'),
    autoFocus: false,
    inlineMessage: inlineMessageField2,
    onSubmitEditing: handleChangePinTap
  }), PINSecurity.displayHelper && /*#__PURE__*/React.createElement(PINValidationHelper, {
    validations: PINValidations
  }), modalState.visible && /*#__PURE__*/React.createElement(AlertModal, {
    title: modalState.title,
    message: modalState.message,
    submit: modalState.onModalDismiss
  })), !PINScreensConfig.useNewPINDesign && /*#__PURE__*/React.createElement(View, {
    style: style.controlsContainer
  }, /*#__PURE__*/React.createElement(Button, {
    title: t('PINChange.ChangePIN'),
    testID: testIdWithKey('ChangePIN'),
    accessibilityLabel: t('Global.Continue'),
    buttonType: ButtonType.Primary,
    disabled: isContinueDisabled,
    onPress: handleChangePinTap,
    ref: createPINButtonRef
  }, isLoading ? /*#__PURE__*/React.createElement(ButtonLoading, null) : null))), /*#__PURE__*/React.createElement(VerifyPINModal, {
    onAuthenticationComplete: onAuthenticationComplete,
    onBackPressed: onVerifyPINModalBackPressed,
    onCancelAuth: onCancelAuth,
    PINVerifyModalUsage: PINEntryUsage.ChangePIN,
    title: t('Screens.EnterPIN'),
    visible: verifyPINModalVisible
  }), /*#__PURE__*/React.createElement(ConfirmPINModal, {
    errorModalState: modalState,
    isLoading: isLoading,
    modalUsage: ConfirmPINModalUsage.PIN_CHANGE,
    onBackPressed: onConfirmPINModalBackPressed,
    onConfirmPIN: handleConfirmPIN,
    PINOne: PIN,
    setPINTwo: setPINTwo,
    title: t('Screens.ConfirmPIN'),
    visible: confirmPINModalVisible
  }));
};
export default PINChange;
//# sourceMappingURL=PINChange.js.map