"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _native = require("@react-navigation/native");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _MaterialIcons = _interopRequireDefault(require("react-native-vector-icons/MaterialIcons"));
var _ButtonApi = require("../components/buttons/Button-api");
var _PINInput = _interopRequireDefault(require("../components/inputs/PINInput"));
var _AlertModal = _interopRequireDefault(require("../components/modals/AlertModal"));
var _KeyboardView = _interopRequireDefault(require("../components/views/KeyboardView"));
var _constants = require("../constants");
var _containerApi = require("../container-api");
var _animatedComponents = require("../contexts/animated-components");
var _auth = require("../contexts/auth");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _error = require("../types/error");
var _navigators = require("../types/navigators");
var _PINCreationValidation = require("../utils/PINCreationValidation");
var _testable = require("../utils/testable");
var _InlineErrorText = require("../components/inputs/InlineErrorText");
var _types = require("../modules/history/types");
var _agent = require("../utils/agent");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// eslint-disable-next-line import/no-named-as-default

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
  } = (0, _agent.useAppAgent)();
  const {
    setPIN: setWalletPIN,
    checkPIN,
    rekeyWallet
  } = (0, _auth.useAuth)();
  const [PIN, setPIN] = (0, _react.useState)('');
  const [PINTwo, setPINTwo] = (0, _react.useState)('');
  const [PINOld, setPINOld] = (0, _react.useState)('');
  const [continueEnabled, setContinueEnabled] = (0, _react.useState)(true);
  const [isLoading, setLoading] = (0, _react.useState)(false);
  const [modalState, setModalState] = (0, _react.useState)({
    visible: false,
    title: '',
    message: ''
  });
  const iconSize = 24;
  const navigation = (0, _native.useNavigation)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [inlineMessageField1, setInlineMessageField1] = (0, _react.useState)();
  const [inlineMessageField2, setInlineMessageField2] = (0, _react.useState)();
  const {
    ColorPallet,
    TextTheme
  } = (0, _theme.useTheme)();
  const {
    ButtonLoading
  } = (0, _animatedComponents.useAnimatedComponents)();
  const PINTwoInputRef = (0, _react.useRef)(null);
  const createPINButtonRef = (0, _react.useRef)(null);
  const actionButtonLabel = updatePin ? t('PINCreate.ChangePIN') : t('PINCreate.CreatePIN');
  const actionButtonTestId = updatePin ? (0, _testable.testIdWithKey)('ChangePIN') : (0, _testable.testIdWithKey)('CreatePIN');
  const [PINExplainer, PINCreateHeader, {
    PINSecurity,
    showPINExplainer
  }, Button, inlineMessages, logger, historyManagerCurried, historyEnabled, historyEventsLogger] = (0, _containerApi.useServices)([_containerApi.TOKENS.SCREEN_PIN_EXPLAINER, _containerApi.TOKENS.COMPONENT_PIN_CREATE_HEADER, _containerApi.TOKENS.CONFIG, _containerApi.TOKENS.COMP_BUTTON, _containerApi.TOKENS.INLINE_ERRORS, _containerApi.TOKENS.UTIL_LOGGER, _containerApi.TOKENS.FN_LOAD_HISTORY, _containerApi.TOKENS.HISTORY_ENABLED, _containerApi.TOKENS.HISTORY_EVENTS_LOGGER]);
  const [explained, setExplained] = (0, _react.useState)(explainedStatus || showPINExplainer === false);
  const [PINOneValidations, setPINOneValidations] = (0, _react.useState)((0, _PINCreationValidation.PINCreationValidations)(PIN, PINSecurity.rules));
  const style = _reactNative.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 = (0, _react.useCallback)(async PIN => {
    try {
      setContinueEnabled(false);
      await setWalletPIN(PIN);
      // This will trigger initAgent
      setAuthenticated(true);
      dispatch({
        type: _store.DispatchAction.DID_CREATE_PIN
      });
      navigation.dispatch(_native.CommonActions.reset({
        index: 0,
        routes: [{
          name: _navigators.Screens.UseBiometry
        }]
      }));
    } catch (err) {
      const error = new _error.BifoldError(t('Error.Title1040'), t('Error.Message1040'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1040);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, error);
    }
  }, [setWalletPIN, setAuthenticated, dispatch, navigation, t]);
  const displayModalMessage = (title, message) => {
    setModalState({
      visible: true,
      title: title,
      message: message
    });
  };
  const attentionMessage = (0, _react.useCallback)((title, message, pinOne) => {
    if (inlineMessages.enabled) {
      const config = {
        message: message,
        inlineType: _InlineErrorText.InlineErrorType.error,
        config: inlineMessages
      };
      if (pinOne) {
        setInlineMessageField1(config);
      } else {
        setInlineMessageField2(config);
      }
    } else {
      displayModalMessage(title, message);
    }
  }, [inlineMessages]);
  const validatePINEntry = (0, _react.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 = (0, _react.useCallback)(async PIN => {
    const valid = await checkPIN(PIN);
    if (!valid) {
      displayModalMessage(t('PINCreate.InvalidPIN'), t(`PINCreate.Message.OldPINIncorrect`));
    }
    return valid;
  }, [checkPIN, t]);
  const confirmEntry = (0, _react.useCallback)(async (PINOne, PINTwo) => {
    if (!validatePINEntry(PINOne, PINTwo)) {
      return;
    }
    await passcodeCreate(PINOne);
  }, [validatePINEntry, passcodeCreate]);
  const logHistoryRecord = (0, _react.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: _types.HistoryCardType.PinChanged,
        message: _types.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(_navigators.Screens.Settings);
              }
            });
          }
        }
        setContinueEnabled(true);
      }
    } else {
      await confirmEntry(PIN, PINTwo);
    }
    setLoading(false);
  };
  const isContinueDisabled = () => {
    if (inlineMessages) {
      return false;
    }
    return !continueEnabled || PIN.length < _constants.minPINLength || PINTwo.length < _constants.minPINLength;
  };
  (0, _react.useEffect)(() => {
    if (updatePin) {
      setContinueEnabled(PIN !== '' && PINTwo !== '' && PINOld !== '');
    }
  }, [updatePin, PIN, PINTwo, PINOld]);
  const continueCreatePIN = () => {
    setExplained(true);
  };
  (0, _react.useEffect)(() => {
    setInlineMessageField1(undefined);
    setInlineMessageField2(undefined);
  }, [PIN, PINTwo]);
  return explained ? /*#__PURE__*/_react.default.createElement(_KeyboardView.default, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.screenContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.contentContainer
  }, /*#__PURE__*/_react.default.createElement(PINCreateHeader, {
    updatePin: updatePin
  }), updatePin && /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    label: t('PINCreate.EnterOldPINTitle'),
    testID: (0, _testable.testIdWithKey)('EnterOldPIN'),
    accessibilityLabel: t('PINCreate.EnterOldPIN'),
    onPINChanged: p => {
      setPINOld(p);
    }
  }), /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    label: t('PINCreate.EnterPINTitle', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    onPINChanged: p => {
      setPIN(p);
      setPINOneValidations((0, _PINCreationValidation.PINCreationValidations)(p, PINSecurity.rules));
      if (p.length === _constants.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 = (0, _reactNative.findNodeHandle)(PINTwoInputRef.current);
          if (reactTag) {
            _reactNative.AccessibilityInfo.setAccessibilityFocus(reactTag);
          }
        }
      }
    },
    testID: (0, _testable.testIdWithKey)('EnterPIN'),
    accessibilityLabel: t('PINCreate.EnterPIN'),
    autoFocus: false,
    inlineMessage: inlineMessageField1
  }), /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    label: t('PINCreate.ReenterPIN', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    onPINChanged: p => {
      setPINTwo(p);
      if (p.length === _constants.minPINLength) {
        _reactNative.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 = (0, _reactNative.findNodeHandle)(createPINButtonRef.current);
          if (reactTag) {
            _reactNative.AccessibilityInfo.setAccessibilityFocus(reactTag);
          }
        }
      }
    },
    testID: (0, _testable.testIdWithKey)('ReenterPIN'),
    accessibilityLabel: t('PINCreate.ReenterPIN', {
      new: updatePin ? t('PINCreate.NewPIN') + ' ' : ''
    }),
    autoFocus: false,
    ref: PINTwoInputRef,
    inlineMessage: inlineMessageField2
  }), PINSecurity.displayHelper && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginBottom: 16
    }
  }, PINOneValidations.map((validation, index) => {
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        flexDirection: 'row'
      },
      key: index
    }, validation.isInvalid ? /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
      accessibilityLabel: t('PINCreate.Helper.ClearIcon'),
      name: "clear",
      size: iconSize,
      color: ColorPallet.notification.errorIcon
    }) : /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
      accessibilityLabel: t('PINCreate.Helper.CheckIcon'),
      name: "check",
      size: iconSize,
      color: ColorPallet.notification.successIcon
    }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [TextTheme.normal, {
        paddingLeft: 4
      }]
    }, t(`PINCreate.Helper.${validation.errorName}`, validation === null || validation === void 0 ? void 0 : validation.errorTextAddition)));
  })), modalState.visible && /*#__PURE__*/_react.default.createElement(_AlertModal.default, {
    title: modalState.title,
    message: modalState.message,
    submit: () => {
      if (modalState.onModalDismiss) {
        modalState.onModalDismiss();
      }
      setModalState({
        ...modalState,
        visible: false,
        onModalDismiss: undefined
      });
    }
  })), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.controlsContainer
  }, /*#__PURE__*/_react.default.createElement(Button, {
    title: actionButtonLabel,
    testID: actionButtonTestId,
    accessibilityLabel: actionButtonLabel,
    buttonType: _ButtonApi.ButtonType.Primary,
    disabled: isContinueDisabled(),
    onPress: handleCreatePinTap,
    ref: createPINButtonRef
  }, !continueEnabled && isLoading ? /*#__PURE__*/_react.default.createElement(ButtonLoading, null) : null)))) : /*#__PURE__*/_react.default.createElement(PINExplainer, {
    continueCreatePIN: continueCreatePIN
  });
};
var _default = exports.default = PINCreate;
//# sourceMappingURL=PINCreate.js.map