"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 _ButtonApi = require("../components/buttons/Button-api");
var _PINInput = _interopRequireDefault(require("../components/inputs/PINInput"));
var _PINValidationHelper = _interopRequireDefault(require("../components/misc/PINValidationHelper"));
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 _usePINValidation = require("../hooks/usePINValidation");
var _error = require("../types/error");
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; }
// eslint-disable-next-line import/no-named-as-default

const PINCreate = ({
  setAuthenticated,
  explainedStatus
}) => {
  const {
    setPIN: setWalletPIN
  } = (0, _auth.useAuth)();
  const [PIN, setPIN] = (0, _react.useState)('');
  const [PINTwo, setPINTwo] = (0, _react.useState)('');
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  const [, dispatch] = (0, _store2.useStore)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const {
    ButtonLoading
  } = (0, _animatedComponents.useAnimatedComponents)();
  const PINTwoInputRef = (0, _react.useRef)(null);
  const createPINButtonRef = (0, _react.useRef)(null);
  const [PINExplainer, PINHeader, {
    showPINExplainer
  }, Button, inlineMessages] = (0, _containerApi.useServices)([_containerApi.TOKENS.SCREEN_PIN_EXPLAINER, _containerApi.TOKENS.COMPONENT_PIN_HEADER, _containerApi.TOKENS.CONFIG, _containerApi.TOKENS.COMP_BUTTON, _containerApi.TOKENS.INLINE_ERRORS]);
  const [explained, setExplained] = (0, _react.useState)(explainedStatus || showPINExplainer === false);
  const {
    PINValidations,
    validatePINEntry,
    inlineMessageField1,
    inlineMessageField2,
    modalState,
    PINSecurity
  } = (0, _usePINValidation.usePINValidation)(PIN, PINTwo);
  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 {
      await setWalletPIN(PIN);
      setAuthenticated(true);
      // this dispatch finishes this step of onboarding and will cause a navigation
      dispatch({
        type: _store.DispatchAction.DID_CREATE_PIN
      });
    } 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, t]);
  const handleCreatePinTap = (0, _react.useCallback)(async () => {
    setIsLoading(true);
    if (validatePINEntry(PIN, PINTwo)) {
      await passcodeCreate(PIN);
    }
    setIsLoading(false);
  }, [PIN, PINTwo, passcodeCreate, validatePINEntry]);
  const isContinueDisabled = (0, _react.useMemo)(() => {
    if (inlineMessages.enabled) {
      return false;
    }
    return isLoading || PIN.length < _constants.minPINLength || PINTwo.length < _constants.minPINLength;
  }, [isLoading, PIN, PINTwo, inlineMessages]);
  const continueCreatePIN = (0, _react.useCallback)(() => {
    setExplained(true);
  }, []);
  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(PINHeader, null), /*#__PURE__*/_react.default.createElement(_PINInput.default, {
    label: t('PINCreate.EnterPINTitle'),
    onPINChanged: p => {
      setPIN(p);
      if (p.length === _constants.minPINLength && PINTwoInputRef !== null && PINTwoInputRef !== void 0 && PINTwoInputRef.current) {
        PINTwoInputRef.current.focus();
        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'),
    onPINChanged: p => {
      setPINTwo(p);
      if (p.length === _constants.minPINLength) {
        _reactNative.Keyboard.dismiss();
        const reactTag = (createPINButtonRef === null || createPINButtonRef === void 0 ? void 0 : createPINButtonRef.current) && (0, _reactNative.findNodeHandle)(createPINButtonRef.current);
        if (reactTag) {
          _reactNative.AccessibilityInfo.setAccessibilityFocus(reactTag);
        }
      }
    },
    testID: (0, _testable.testIdWithKey)('ReenterPIN'),
    accessibilityLabel: t('PINCreate.ReenterPIN'),
    autoFocus: false,
    ref: PINTwoInputRef,
    inlineMessage: inlineMessageField2
  }), PINSecurity.displayHelper && /*#__PURE__*/_react.default.createElement(_PINValidationHelper.default, {
    validations: PINValidations
  }), modalState.visible && /*#__PURE__*/_react.default.createElement(_AlertModal.default, {
    title: modalState.title,
    message: modalState.message,
    submit: modalState.onModalDismiss
  })), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.controlsContainer
  }, /*#__PURE__*/_react.default.createElement(Button, {
    title: t('PINCreate.CreatePIN'),
    testID: (0, _testable.testIdWithKey)('CreatePIN'),
    accessibilityLabel: t('PINCreate.CreatePIN'),
    buttonType: _ButtonApi.ButtonType.Primary,
    disabled: isContinueDisabled,
    onPress: handleCreatePinTap,
    ref: createPINButtonRef
  }, isLoading ? /*#__PURE__*/_react.default.createElement(ButtonLoading, null) : null)))) : /*#__PURE__*/_react.default.createElement(PINExplainer, {
    continueCreatePIN: continueCreatePIN
  });
};
var _default = exports.default = PINCreate;
//# sourceMappingURL=PINCreate.js.map