"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 _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 _luminance = require("../utils/luminance");
var _testable = require("../utils/testable");
var _PINEnter = _interopRequireWildcard(require("./PINEnter"));
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; }
var UseBiometryUsage = /*#__PURE__*/function (UseBiometryUsage) {
  UseBiometryUsage[UseBiometryUsage["InitialSetup"] = 0] = "InitialSetup";
  UseBiometryUsage[UseBiometryUsage["ToggleOnOff"] = 1] = "ToggleOnOff";
  return UseBiometryUsage;
}(UseBiometryUsage || {});
const UseBiometry = () => {
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    isBiometricsActive,
    commitPIN,
    disableBiometrics
  } = (0, _auth.useAuth)();
  const [biometryAvailable, setBiometryAvailable] = (0, _react.useState)(false);
  const [biometryEnabled, setBiometryEnabled] = (0, _react.useState)(store.preferences.useBiometry);
  const [continueEnabled, setContinueEnabled] = (0, _react.useState)(true);
  const [canSeeCheckPIN, setCanSeeCheckPIN] = (0, _react.useState)(false);
  const {
    ColorPallet,
    TextTheme,
    Assets
  } = (0, _theme.useTheme)();
  const {
    ButtonLoading
  } = (0, _animatedComponents.useAnimatedComponents)();
  const screenUsage = store.onboarding.didConsiderBiometry ? UseBiometryUsage.ToggleOnOff : UseBiometryUsage.InitialSetup;
  const styles = _reactNative.StyleSheet.create({
    container: {
      height: '100%',
      padding: 20,
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    image: {
      minWidth: 200,
      minHeight: 200,
      marginBottom: 66
    }
  });
  (0, _react.useEffect)(() => {
    isBiometricsActive().then(result => {
      setBiometryAvailable(result);
    });
  }, []);
  (0, _react.useEffect)(() => {
    if (screenUsage === UseBiometryUsage.InitialSetup) {
      return;
    }
    if (biometryEnabled) {
      commitPIN(biometryEnabled).then(() => {
        dispatch({
          type: _store.DispatchAction.USE_BIOMETRY,
          payload: [biometryEnabled]
        });
      });
    } else {
      disableBiometrics().then(() => {
        dispatch({
          type: _store.DispatchAction.USE_BIOMETRY,
          payload: [biometryEnabled]
        });
      });
    }
  }, [biometryEnabled]);
  const continueTouched = async () => {
    setContinueEnabled(false);
    await commitPIN(biometryEnabled);
    dispatch({
      type: _store.DispatchAction.USE_BIOMETRY,
      payload: [biometryEnabled]
    });
  };
  const toggleSwitch = () => {
    // If the user is toggling biometrics on/off they need
    // to first authenticate before this action is accepted
    if (screenUsage === UseBiometryUsage.ToggleOnOff) {
      setCanSeeCheckPIN(true);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.BIOMETRY_UPDATE, true);
      return;
    }
    setBiometryEnabled(previousState => !previousState);
  };
  const onAuthenticationComplete = status => {
    // If successfully authenticated the toggle may proceed.
    if (status) {
      setBiometryEnabled(previousState => !previousState);
    }
    _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.BIOMETRY_UPDATE, false);
    setCanSeeCheckPIN(false);
  };
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    edges: ['left', 'right', 'bottom']
  }, /*#__PURE__*/_react.default.createElement(_reactNative.StatusBar, {
    barStyle: _reactNative.Platform.OS === 'android' ? _luminance.StatusBarStyles.Light : (0, _luminance.statusBarStyleForColor)(ColorPallet.brand.primary)
  }), /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, {
    style: styles.container
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      alignItems: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(Assets.svg.biometrics, {
    style: [styles.image]
  })), biometryAvailable ? /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal]
  }, t('Biometry.EnabledText1')), /*#__PURE__*/_react.default.createElement(_reactNative.Text, null), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal]
  }, t('Biometry.EnabledText2'), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.bold]
  }, " ", t('Biometry.Warning')))) : /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal]
  }, t('Biometry.NotEnabledText1')), /*#__PURE__*/_react.default.createElement(_reactNative.Text, null), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal]
  }, t('Biometry.NotEnabledText2'))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flexDirection: 'row',
      marginVertical: 20
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flexShrink: 1,
      marginRight: 10,
      justifyContent: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.bold]
  }, t('Biometry.UseToUnlock'))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      justifyContent: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Pressable, {
    testID: (0, _testable.testIdWithKey)('ToggleBiometrics'),
    accessible: true,
    accessibilityLabel: t('Biometry.Toggle'),
    accessibilityRole: 'switch'
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Switch, {
    trackColor: {
      false: ColorPallet.grayscale.lightGrey,
      true: ColorPallet.brand.primaryDisabled
    },
    thumbColor: biometryEnabled ? ColorPallet.brand.primary : ColorPallet.grayscale.mediumGrey,
    ios_backgroundColor: ColorPallet.grayscale.lightGrey,
    onValueChange: toggleSwitch,
    value: biometryEnabled,
    disabled: !biometryAvailable
  }))))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginTop: 'auto',
      margin: 20
    }
  }, store.onboarding.didConsiderBiometry || /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: 'Continue',
    accessibilityLabel: 'Continue',
    testID: (0, _testable.testIdWithKey)('Continue'),
    onPress: continueTouched,
    buttonType: _Button.ButtonType.Primary,
    disabled: !continueEnabled
  }, !continueEnabled && /*#__PURE__*/_react.default.createElement(ButtonLoading, null))), /*#__PURE__*/_react.default.createElement(_reactNative.Modal, {
    style: {
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    visible: canSeeCheckPIN,
    transparent: false,
    animationType: 'slide'
  }, /*#__PURE__*/_react.default.createElement(_PINEnter.default, {
    usage: _PINEnter.PINEntryUsage.PINCheck,
    setAuthenticated: onAuthenticationComplete
  })));
};
var _default = UseBiometry;
exports.default = _default;
//# sourceMappingURL=UseBiometry.js.map