"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@react-navigation/core");
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 _Button = _interopRequireWildcard(require("../components/buttons/Button"));
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 _animatedComponents = require("../contexts/animated-components");
var _auth = require("../contexts/auth");
var _configuration = require("../contexts/configuration");
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 _luminance = require("../utils/luminance");
var _testable = require("../utils/testable");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// eslint-disable-next-line import/no-named-as-default

const PINCreate = _ref => {
  var _route$params;
  let {
    setAuthenticated,
    route
  } = _ref;
  const updatePin = (_route$params = route.params) === null || _route$params === void 0 ? void 0 : _route$params.updatePin;
  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, _core.useNavigation)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    PINSecurity
  } = (0, _configuration.useConfiguration)();
  const [PINOneValidations, setPINOneValidations] = (0, _react.useState)((0, _PINCreationValidation.PINCreationValidations)(PIN, PINSecurity.rules));
  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 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 = async PIN => {
    try {
      setContinueEnabled(false);
      await setWalletPIN(PIN);
      // This will trigger initAgent
      setAuthenticated(true);
      dispatch({
        type: _store.DispatchAction.DID_CREATE_PIN
      });

      // TODO: Navigate back if in settings
      if (store.preferences.enableWalletNaming) {
        navigation.dispatch(_native.CommonActions.reset({
          index: 0,
          routes: [{
            name: _navigators.Screens.NameWallet
          }]
        }));
      } else {
        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);
    }
  };
  const validatePINEntry = (PINOne, PINTwo) => {
    for (const validation of PINOneValidations) {
      if (validation.isInvalid) {
        setModalState({
          visible: true,
          title: t('PINCreate.InvalidPIN'),
          message: t(`PINCreate.Message.${validation.errorName}`)
        });
        return false;
      }
    }
    if (PINOne !== PINTwo) {
      setModalState({
        visible: true,
        title: t('PINCreate.InvalidPIN'),
        message: t('PINCreate.PINsDoNotMatch')
      });
      return false;
    }
    return true;
  };
  const checkOldPIN = async PIN => {
    const valid = await checkPIN(PIN);
    if (!valid) {
      setModalState({
        visible: true,
        title: t('PINCreate.InvalidPIN'),
        message: t(`PINCreate.Message.OldPINIncorrect`)
      });
    }
    return valid;
  };
  const confirmEntry = async (PINOne, PINTwo) => {
    if (!validatePINEntry(PINOne, PINTwo)) {
      return;
    }
    await passcodeCreate(PINOne);
  };
  (0, _react.useEffect)(() => {
    if (updatePin) {
      setContinueEnabled(PIN !== '' && PINTwo !== '' && PINOld !== '');
    }
  }, [PINOld, PIN, PINTwo]);
  return /*#__PURE__*/_react.default.createElement(_KeyboardView.default, null, /*#__PURE__*/_react.default.createElement(_reactNative.StatusBar, {
    barStyle: _luminance.StatusBarStyles.Light
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.screenContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.contentContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal, {
      marginBottom: 16
    }]
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: {
      fontWeight: 'bold'
    }
  }, updatePin ? t('PINCreate.RememberChangePIN') : t('PINCreate.RememberPIN')), ' ', t('PINCreate.PINDisclaimer')), updatePin && /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    label: t('PINCreate.EnterOldPINTitle'),
    testID: (0, _testable.testIdWithKey)('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
  }), 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, {
      name: "clear",
      size: iconSize,
      color: ColorPallet.notification.errorIcon
    }) : /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
      name: "check",
      size: iconSize,
      color: ColorPallet.notification.successIcon
    }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [TextTheme.normal, {
        paddingLeft: 4
      }]
    }, t(`PINCreate.Helper.${validation.errorName}`)));
  })), /*#__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
  }), 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.default, {
    title: actionButtonLabel,
    testID: actionButtonTestId,
    accessibilityLabel: actionButtonLabel,
    buttonType: _Button.ButtonType.Primary,
    disabled: !continueEnabled || PIN.length < _constants.minPINLength || PINTwo.length < _constants.minPINLength,
    onPress: 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) {
              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);
    },
    ref: createPINButtonRef
  }, !continueEnabled && isLoading ? /*#__PURE__*/_react.default.createElement(ButtonLoading, null) : null))));
};
var _default = PINCreate;
exports.default = _default;
//# sourceMappingURL=PINCreate.js.map