"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _Button = _interopRequireWildcard(require("../components/buttons/Button"));
var _constants = require("../constants");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _testable = require("../utils/testable");
var _ThemedText = require("../components/texts/ThemedText");
var _InfoTextBox = _interopRequireDefault(require("../components/texts/InfoTextBox"));
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; }
const AttemptLockout = () => {
  const {
    ColorPallet,
    Assets,
    Spacing
  } = (0, _theme.useTheme)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [state, dispatch] = (0, _store2.useStore)();
  const [time, setTime] = (0, _react.useState)();
  const [timeoutDone, setTimeoutDone] = (0, _react.useState)(false);
  const styles = _reactNative.StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: ColorPallet.brand.primaryBackground,
      paddingHorizontal: Spacing.lg
    },
    title: {
      textAlign: 'center',
      marginBottom: Spacing.lg
    },
    description: {
      textAlign: 'center',
      marginBottom: Spacing.lg
    },
    actionContainer: {
      marginBottom: Spacing.lg
    },
    tryAgain: {
      textAlign: 'center'
    },
    countDown: {
      textAlign: 'center'
    },
    image: {
      width: 150,
      height: 150,
      marginVertical: Spacing.lg,
      alignSelf: 'center'
    }
  });

  // update the countdown timer. Return true if the lockout penalty time is over
  const updateTimeRemaining = (0, _react.useCallback)(() => {
    let penaltyServed = true;
    const penalty = state.loginAttempt.lockoutDate;
    const currDate = Date.now();
    if (penalty) {
      let diff = penalty - currDate;
      if (diff > 0) {
        penaltyServed = false;
        const hoursLeft = Math.floor(diff / _constants.hour);
        diff = diff - hoursLeft * _constants.hour;
        const minutesLeft = Math.floor(diff / _constants.minute);
        diff = diff - minutesLeft * _constants.minute;
        const secondsLeft = Math.floor(diff / _constants.second);
        const timer = {
          hours: hoursLeft,
          minutes: minutesLeft,
          seconds: secondsLeft
        };
        setTime(timer);
      }
    }
    return penaltyServed;
  }, [state.loginAttempt.lockoutDate]);

  // run once immediately at screen initialization
  (0, _react.useEffect)(() => {
    setTimeoutDone(updateTimeRemaining());
  }, [updateTimeRemaining]);

  // make sure set timeout only runs once
  (0, _react.useEffect)(() => {
    const updateTimer = setTimeout(() => {
      // calculate time remaining
      const timerDone = updateTimeRemaining();
      setTimeoutDone(timerDone);
      if (timerDone) {
        clearInterval(updateTimer);
      }
    }, 1000);
  });
  const unlock = (0, _react.useCallback)(async () => {
    dispatch({
      type: _store.DispatchAction.ATTEMPT_UPDATED,
      payload: [{
        loginAttempts: state.loginAttempt.loginAttempts,
        lockoutDate: undefined,
        servedPenalty: true
      }]
    });
  }, [dispatch, state.loginAttempt.loginAttempts]);
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: styles.container,
    edges: ['bottom', 'left', 'right']
  }, /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, {
    showsVerticalScrollIndicator: false,
    contentContainerStyle: {
      flexGrow: 1
    }
  }, /*#__PURE__*/_react.default.createElement(Assets.svg.appLockout, {
    style: styles.image
  }), /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "headingThree",
    style: styles.title
  }, t('AttemptLockout.Title')), /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    style: styles.description
  }, t('AttemptLockout.Description')), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.actionContainer
  }, timeoutDone ? /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: t('Global.TryAgain'),
    buttonType: _Button.ButtonType.Primary,
    testID: (0, _testable.testIdWithKey)('Enter'),
    accessibilityLabel: t('Global.TryAgain'),
    onPress: unlock
  }) : /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    style: styles.tryAgain
  }, t('AttemptLockout.TryAgain')), time && /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "bold",
    style: styles.countDown
  }, time === null || time === void 0 ? void 0 : time.hours, " ", t('AttemptLockout.Hours'), " ", time === null || time === void 0 ? void 0 : time.minutes, " ", t('AttemptLockout.Minutes'), ' ', time === null || time === void 0 ? void 0 : time.seconds, " ", t('AttemptLockout.Seconds')))), /*#__PURE__*/_react.default.createElement(_InfoTextBox.default, {
    style: {
      flexShrink: 1,
      padding: Spacing.md
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, {
    variant: "bold",
    style: {
      marginBottom: Spacing.md
    }
  }, t('AttemptLockout.ForgotPIN')), /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, null, t('AttemptLockout.ForgotPINDescription'))))));
};
var _default = exports.default = AttemptLockout;
//# sourceMappingURL=AttemptLockout.js.map