import { CommonActions, useNavigation } from '@react-navigation/native';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { AccessibilityInfo, Keyboard, StyleSheet, Text, View, findNodeHandle, DeviceEventEmitter } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';

// eslint-disable-next-line import/no-named-as-default
import { ButtonType } from '../components/buttons/Button-api';
import PINInput from '../components/inputs/PINInput';
import AlertModal from '../components/modals/AlertModal';
import KeyboardView from '../components/views/KeyboardView';
import { EventTypes, minPINLength } from '../constants';
import { TOKENS, useServices } from '../container-api';
import { useAnimatedComponents } from '../contexts/animated-components';
import { useAuth } from '../contexts/auth';
import { DispatchAction } from '../contexts/reducers/store';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { BifoldError } from '../types/error';
import { Screens } from '../types/navigators';
import { PINCreationValidations } from '../utils/PINCreationValidation';
import { testIdWithKey } from '../utils/testable';
import { InlineErrorType } from '../components/inputs/InlineErrorText';
import { HistoryCardType } from '../modules/history/types';
import { useAppAgent } from '../utils/agent';
const PINCreate = ({
  setAuthenticated,
  explainedStatus,
  route
}) => {
  var _route$params;
  const updatePin = (_route$params = route.params) === null || _route$params === void 0 ? void 0 : _route$params.updatePin;
  const {
    agent
  } = useAppAgent();
  const {
    setPIN: setWalletPIN,
    checkPIN,
    rekeyWallet
  } = useAuth();
  const [PIN, setPIN] = useState('');
  const [PINTwo, setPINTwo] = useState('');
  const [PINOld, setPINOld] = useState('');
  const [continueEnabled, setContinueEnabled] = useState(true);
  const [isLoading, setLoading] = useState(false);
  const [modalState, setModalState] = useState({
    visible: false,
    title: '',
    message: ''
  });
  const iconSize = 24;
  const navigation = useNavigation();
  const [store, dispatch] = useStore();
  const {
    t
  } = useTranslation();
  const [inlineMessageField1, setInlineMessageField1] = useState();
  const [inlineMessageField2, setInlineMessageField2] = useState();
  const {
    ColorPallet,
    TextTheme
  } = useTheme();
  const {
    ButtonLoading
  } = useAnimatedComponents();
  const PINTwoInputRef = useRef(null);
  const createPINButtonRef = useRef(null);
  const actionButtonLabel = updatePin ? t('PINCreate.ChangePIN') : t('PINCreate.CreatePIN');
  const actionButtonTestId = updatePin ? testIdWithKey('ChangePIN') : testIdWithKey('CreatePIN');
  const [PINExplainer, PINCreateHeader, {
    PINSecurity,
    showPINExplainer
  }, Button, inlineMessages, logger, historyManagerCurried, historyEnabled, historyEventsLogger] = useServices([TOKENS.SCREEN_PIN_EXPLAINER, TOKENS.COMPONENT_PIN_CREATE_HEADER, TOKENS.CONFIG, TOKENS.COMP_BUTTON, TOKENS.INLINE_ERRORS, TOKENS.UTIL_LOGGER, TOKENS.FN_LOAD_HISTORY, TOKENS.HISTORY_ENABLED, TOKENS.HISTORY_EVENTS_LOGGER]);
  const [explained, setExplained] = useState(explainedStatus || showPINExplainer === false);
  const [PINOneValidations, setPINOneValidations] = useState(PINCreationValidations(PIN, PINSecurity.rules));
  const style = StyleSheet.create({
    screenContainer: {
      height: '100%',
      backgroundColor: ColorPallet.brand.primaryBackground,
      padding: 20,
      justifyContent: 'space-between'
    },
    // below used as helpful labels for views, no properties needed atp
    contentContainer: {},
    controlsContainer: {}
  });
  const passcodeCreate = useCallback(async PIN => {
    try {
      setContinueEnabled(false);
      await setWalletPIN(PIN);
      // This will trigger initAgent
      setAuthenticated(true);
      dispatch({
        type: DispatchAction.DID_CREATE_PIN
      });
      navigation.dispatch(CommonActions.reset({
        index: 0,
        routes: [{
          name: Screens.UseBiometry
        }]
      }));
    } catch (err) {
      const error = new BifoldError(t('Error.Title1040'), t('Error.Message1040'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1040);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    }
  }, [setWalletPIN, setAuthenticated, dispatch, navigation, t]);
  const displayModalMessage = (title, message) => {
    setModalState({
      visible: true,
      title: title,
      message: message
    });
  };
  const attentionMessage = useCallback((title, message, pinOne) => {
    if (inlineMessages.enabled) {
      const config = {
        message: message,
        inlineType: InlineErrorType.error,
        config: inlineMessages
      };
      if (pinOne) {
        setInlineMessageField1(config);
      } else {
        setInlineMessageField2(config);
      }
    } else {
      displayModalMessage(title, message);
    }
  }, [inlineMessages]);
  const validatePINEntry = useCallback((PINOne, PINTwo) => {
    for (const validation of PINOneValidations) {
      if (validation.isInvalid) {
        attentionMessage(t('PINCreate.InvalidPIN'), t(`PINCreate.Message.${validation.errorName}`), true);
        return false;
      }
    }
    if (PINOne !== PINTwo) {
      attentionMessage(t('PINCreate.InvalidPIN'), t('PINCreate.PINsDoNotMatch'), false);
      return false;
    }
    return true;
  }, [PINOneValidations, t, attentionMessage]);
  const checkOldPIN = useCallback(async PIN => {
    const valid = await checkPIN(PIN);
    if (!valid) {
      displayModalMessage(t('PINCreate.InvalidPIN'), t(`PINCreate.Message.OldPINIncorrect`));
    }
    return valid;
  }, [checkPIN, t]);
  const confirmEntry = useCallback(async (PINOne, PINTwo) => {
    if (!validatePINEntry(PINOne, PINTwo)) {
      return;
    }
    await passcodeCreate(PINOne);
  }, [validatePINEntry, passcodeCreate]);
  const logHistoryRecord = useCallback(() => {
    try {
      if (!(agent && historyEnabled)) {
        logger.trace(`[${PINCreate.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(`[${PINCreate.name}]:[logHistoryRecord] Error saving history: ${err}`);
    }
  }, [agent, historyEnabled, logger, historyManagerCurried]);
  const handleCreatePinTap = async () => {
    setLoading(true);
    if (updatePin) {
      const valid = validatePINEntry(PIN, PINTwo);
      if (valid) {
        setContinueEnabled(false);
        const oldPinValid = await checkOldPIN(PINOld);
        if (oldPinValid) {
          const success = await rekeyWallet(PINOld, PIN, store.preferences.useBiometry);
          if (success) {
            if (historyEventsLogger.logPinChanged) {
              logHistoryRecord();
            }
            setModalState({
              visible: true,
              title: t('PINCreate.PinChangeSuccessTitle'),
              message: t('PINCreate.PinChangeSuccessMessage'),
              onModalDismiss: () => {
                navigation.navigate(Screens.Settings);
              }
            });
          }
        }
        setContinueEnabled(true);
      }
    } else {
      await confirmEntry(PIN, PINTwo);
    }
    setLoading(false);
  };
  const isContinueDisabled = () => {
    if (inlineMessages) {
      return false;
    }
    return !continueEnabled || PIN.length < minPINLength || PINTwo.length < minPINLength;
  };
  useEffect(() => {
    if (updatePin) {
      setContinueEnabled(PIN !== '' && PINTwo !== '' && PINOld !== '');
    }
  }, [updatePin, PIN, PINTwo, PINOld]);
  const continueCreatePIN = () => {
    setExplained(true);
  };
  useEffect(() => {
    setInlineMessageField1(undefined);
    setInlineMessageField2(undefined);
  }, [PIN, PINTwo]);
  return explained ? /*#__PURE__*/React.createElement(KeyboardView, null, /*#__PURE__*/React.createElement(View, {
    style: style.screenContainer
  }, /*#__PURE__*/React.createElement(View, {
    style: style.contentContainer
  }, /*#__PURE__*/React.createElement(PINCreateHeader, {
    updatePin: updatePin
  }), updatePin && /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINCreate.EnterOldPINTitle'),
    testID: testIdWithKey('EnterOldPIN'),
    accessibilityLabel: t('PINCreate.EnterOldPIN'),
    onPINChanged: p => {
      setPINOld(p);
    }
  }), /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINCreate.EnterPINTitle', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    onPINChanged: p => {
      setPIN(p);
      setPINOneValidations(PINCreationValidations(p, PINSecurity.rules));
      if (p.length === minPINLength) {
        if (PINTwoInputRef && PINTwoInputRef.current) {
          PINTwoInputRef.current.focus();
          // NOTE:(jl) `findNodeHandle` will be deprecated in React 18.
          // https://reactnative.dev/docs/new-architecture-library-intro#preparing-your-javascript-codebase-for-the-new-react-native-renderer-fabric
          const reactTag = findNodeHandle(PINTwoInputRef.current);
          if (reactTag) {
            AccessibilityInfo.setAccessibilityFocus(reactTag);
          }
        }
      }
    },
    testID: testIdWithKey('EnterPIN'),
    accessibilityLabel: t('PINCreate.EnterPIN'),
    autoFocus: false,
    inlineMessage: inlineMessageField1
  }), /*#__PURE__*/React.createElement(PINInput, {
    label: t('PINCreate.ReenterPIN', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    onPINChanged: p => {
      setPINTwo(p);
      if (p.length === minPINLength) {
        Keyboard.dismiss();
        if (createPINButtonRef && createPINButtonRef.current) {
          // NOTE:(jl) `findNodeHandle` will be deprecated in React 18.
          // https://reactnative.dev/docs/new-architecture-library-intro#preparing-your-javascript-codebase-for-the-new-react-native-renderer-fabric
          const reactTag = findNodeHandle(createPINButtonRef.current);
          if (reactTag) {
            AccessibilityInfo.setAccessibilityFocus(reactTag);
          }
        }
      }
    },
    testID: testIdWithKey('ReenterPIN'),
    accessibilityLabel: t('PINCreate.ReenterPIN', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    autoFocus: false,
    ref: PINTwoInputRef,
    inlineMessage: inlineMessageField2
  }), PINSecurity.displayHelper && /*#__PURE__*/React.createElement(View, {
    style: {
      marginBottom: 16
    }
  }, PINOneValidations.map((validation, index) => {
    return /*#__PURE__*/React.createElement(View, {
      style: {
        flexDirection: 'row'
      },
      key: index
    }, validation.isInvalid ? /*#__PURE__*/React.createElement(Icon, {
      accessibilityLabel: t('PINCreate.Helper.ClearIcon'),
      name: "clear",
      size: iconSize,
      color: ColorPallet.notification.errorIcon
    }) : /*#__PURE__*/React.createElement(Icon, {
      accessibilityLabel: t('PINCreate.Helper.CheckIcon'),
      name: "check",
      size: iconSize,
      color: ColorPallet.notification.successIcon
    }), /*#__PURE__*/React.createElement(Text, {
      style: [TextTheme.normal, {
        paddingLeft: 4
      }]
    }, t(`PINCreate.Helper.${validation.errorName}`, validation === null || validation === void 0 ? void 0 : validation.errorTextAddition)));
  })), modalState.visible && /*#__PURE__*/React.createElement(AlertModal, {
    title: modalState.title,
    message: modalState.message,
    submit: () => {
      if (modalState.onModalDismiss) {
        modalState.onModalDismiss();
      }
      setModalState({
        ...modalState,
        visible: false,
        onModalDismiss: undefined
      });
    }
  })), /*#__PURE__*/React.createElement(View, {
    style: style.controlsContainer
  }, /*#__PURE__*/React.createElement(Button, {
    title: actionButtonLabel,
    testID: actionButtonTestId,
    accessibilityLabel: actionButtonLabel,
    buttonType: ButtonType.Primary,
    disabled: isContinueDisabled(),
    onPress: handleCreatePinTap,
    ref: createPINButtonRef
  }, !continueEnabled && isLoading ? /*#__PURE__*/React.createElement(ButtonLoading, null) : null)))) : /*#__PURE__*/React.createElement(PINExplainer, {
    continueCreatePIN: continueCreatePIN
  });
};
export default PINCreate;
//# sourceMappingURL=PINCreate.js.map