"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.PINEntryUsage = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _Button = _interopRequireWildcard(require("../components/buttons/Button"));
var _PINInput = _interopRequireDefault(require("../components/inputs/PINInput"));
var _InfoBox = require("../components/misc/InfoBox");
var _PopupModal = _interopRequireDefault(require("../components/modals/PopupModal"));
var _ThemedText = require("../components/texts/ThemedText");
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 _theme = require("../contexts/theme");
var _screenCapture = _interopRequireDefault(require("../hooks/screen-capture"));
var _testable = require("../utils/testable");
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; }
let PINEntryUsage = exports.PINEntryUsage = /*#__PURE__*/function (PINEntryUsage) {
  PINEntryUsage[PINEntryUsage["PINCheck"] = 0] = "PINCheck";
  PINEntryUsage[PINEntryUsage["ChangeBiometrics"] = 1] = "ChangeBiometrics";
  return PINEntryUsage;
}({});
const PINVerify = ({
  setAuthenticated,
  usage = PINEntryUsage.PINCheck,
  onCancelAuth
}) => {
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    verifyPIN
  } = (0, _auth.useAuth)();
  const [PIN, setPIN] = (0, _react.useState)('');
  const [continueDisabled, setContinueDisabled] = (0, _react.useState)(false);
  const [loading, setLoading] = (0, _react.useState)(false);
  const [alertModalVisible, setAlertModalVisible] = (0, _react.useState)(false);
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const {
    ButtonLoading
  } = (0, _animatedComponents.useAnimatedComponents)();
  const [inlineMessageField, setInlineMessageField] = (0, _react.useState)();
  // Temporary until all use cases are built with the new design
  const isNewDesign = usage === PINEntryUsage.ChangeBiometrics;
  const [{
    preventScreenCapture
  }] = (0, _containerApi.useServices)([_containerApi.TOKENS.CONFIG]);
  (0, _screenCapture.default)(preventScreenCapture);
  (0, _react.useEffect)(() => {
    setInlineMessageField(undefined);
  }, [PIN]);
  const clearAlertModal = (0, _react.useCallback)(() => {
    setAlertModalVisible(false);
    setAuthenticated(false);
  }, [setAlertModalVisible, setAuthenticated]);
  const onPINInputCompleted = (0, _react.useCallback)(async () => {
    _reactNative.Keyboard.dismiss();
    setLoading(true);
    setContinueDisabled(true);
    const isPINVerified = await verifyPIN(PIN);
    if (isPINVerified) {
      setAuthenticated(true);
    } else {
      setAlertModalVisible(true);
    }
    setLoading(false);
    setContinueDisabled(false);
  }, [verifyPIN, setLoading, setAuthenticated, setContinueDisabled, PIN]);
  const inputLabelText = {
    [PINEntryUsage.ChangeBiometrics]: t('PINEnter.ChangeBiometricsInputLabel'),
    [PINEntryUsage.PINCheck]: t('PINEnter.AppSettingChangedEnterPIN')
  };
  const inputTestId = {
    [PINEntryUsage.ChangeBiometrics]: 'BiometricChangedEnterPIN',
    [PINEntryUsage.PINCheck]: 'AppSettingChangedEnterPIN'
  };
  const primaryButtonText = {
    [PINEntryUsage.ChangeBiometrics]: t('Global.Continue'),
    [PINEntryUsage.PINCheck]: t('PINEnter.AppSettingSave')
  };
  const primaryButtonTestId = {
    [PINEntryUsage.ChangeBiometrics]: 'Continue',
    [PINEntryUsage.PINCheck]: 'AppSettingSave'
  };
  const helpText = {
    [PINEntryUsage.ChangeBiometrics]: t('PINEnter.ChangeBiometricsSubtext'),
    [PINEntryUsage.PINCheck]: t('PINEnter.AppSettingChanged')
  };
  const isContinueDisabled = continueDisabled || PIN.length < _constants.minPINLength;
  const style = _reactNative.StyleSheet.create({
    screenContainer: {
      flex: 1,
      padding: 20,
      backgroundColor: ColorPallet.brand.primaryBackground,
      justifyContent: isNewDesign ? 'flex-start' : 'space-between'
    },
    buttonContainer: {
      marginTop: 'auto',
      width: '100%'
    },
    helpText: {
      alignSelf: 'auto',
      textAlign: 'left',
      marginBottom: isNewDesign ? 40 : 20
    },
    inputLabelText: {
      alignSelf: 'auto',
      textAlign: 'left',
      marginBottom: isNewDesign ? 20 : 4
    },
    modalText: {
      marginVertical: 5
    },
    changeBiometricsHeader: {
      marginTop: 0,
      marginBottom: isNewDesign ? 40 : 20
    }
  });
  return /*#__PURE__*/_react.default.createElement(_KeyboardView.default, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.screenContainer
  }, usage === PINEntryUsage.ChangeBiometrics && /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "headingTwo",
    style: style.changeBiometricsHeader
  }, t('PINEnter.ChangeBiometricsHeader')), /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    style: style.helpText
  }, helpText[usage]), /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "bold",
    style: style.inputLabelText
  }, inputLabelText[usage], usage === PINEntryUsage.ChangeBiometrics && /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "caption"
  }, ` `, t('PINEnter.ChangeBiometricsInputLabelParenthesis'))), /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    onPINChanged: p => {
      setPIN(p);
      if (p.length === _constants.minPINLength) {
        _reactNative.Keyboard.dismiss();
      }
    },
    testID: (0, _testable.testIdWithKey)(inputTestId[usage]),
    accessibilityLabel: inputLabelText[usage],
    autoFocus: true,
    inlineMessage: inlineMessageField
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.buttonContainer
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: primaryButtonText[usage],
    buttonType: _Button.ButtonType.Primary,
    testID: (0, _testable.testIdWithKey)(primaryButtonTestId[usage]),
    disabled: isContinueDisabled,
    accessibilityLabel: primaryButtonText[usage],
    onPress: async () => {
      await onPINInputCompleted();
    }
  }, loading && /*#__PURE__*/_react.default.createElement(ButtonLoading, null))), usage === PINEntryUsage.PINCheck && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: [style.buttonContainer, {
      marginTop: 10
    }]
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: t('PINEnter.AppSettingCancel'),
    buttonType: _Button.ButtonType.Secondary,
    testID: (0, _testable.testIdWithKey)('AppSettingCancel'),
    accessibilityLabel: t('PINEnter.AppSettingCancel'),
    onPress: () => onCancelAuth === null || onCancelAuth === void 0 ? void 0 : onCancelAuth(false)
  }))), alertModalVisible && /*#__PURE__*/_react.default.createElement(_PopupModal.default, {
    notificationType: _InfoBox.InfoBoxType.Info,
    title: t('PINEnter.IncorrectPIN'),
    onCallToActionLabel: t('Global.Okay'),
    onCallToActionPressed: clearAlertModal
  }));
};
var _default = exports.default = PINVerify;
//# sourceMappingURL=PINVerify.js.map