"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 _reactNativeConfirmationCodeField = require("react-native-confirmation-code-field");
var _MaterialIcons = _interopRequireDefault(require("react-native-vector-icons/MaterialIcons"));
var _constants = require("../../constants");
var _theme = require("../../contexts/theme");
var _testable = require("../../utils/testable");
var _InlineErrorText = _interopRequireDefault(require("./InlineErrorText"));
var _error = require("../../types/error");
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; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const PINInputComponent = ({
  label,
  onPINChanged,
  testID,
  accessibilityLabel,
  autoFocus = false,
  inlineMessage
}, ref) => {
  // const accessible = accessibilityLabel && accessibilityLabel !== '' ? true : false
  const [PIN, setPIN] = (0, _react.useState)('');
  const [showPIN, setShowPIN] = (0, _react.useState)(false);
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    TextTheme,
    PINInputTheme
  } = (0, _theme.useTheme)();
  const cellHeight = 48;
  const onChangeText = value => {
    onPINChanged && onPINChanged(value);
    setPIN(value);
  };
  const [props, getCellOnLayoutHandler] = (0, _reactNativeConfirmationCodeField.useClearByFocusCell)({
    value: PIN,
    setValue: onChangeText
  });
  const style = _reactNative.StyleSheet.create({
    container: {
      flexDirection: 'column',
      marginBottom: 24
    },
    codeFieldContainer: {
      flex: 1
    },
    cell: {
      height: cellHeight,
      paddingHorizontal: 2,
      backgroundColor: PINInputTheme.cell.backgroundColor
    },
    cellText: {
      ...TextTheme.headingThree,
      color: PINInputTheme.cellText.color,
      textAlign: 'center',
      lineHeight: cellHeight
    },
    hideIcon: {
      paddingHorizontal: 10
    }
  });
  const content = () => /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: PINInputTheme.labelAndFieldContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.codeFieldContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNativeConfirmationCodeField.CodeField, _extends({}, props, {
    testID: testID,
    accessibilityLabel: accessibilityLabel,
    accessible: true,
    value: PIN,
    rootStyle: PINInputTheme.codeFieldRoot,
    onChangeText: onChangeText,
    cellCount: _constants.minPINLength,
    keyboardType: "numeric",
    textContentType: "password",
    renderCell: ({
      index,
      symbol,
      isFocused
    }) => {
      let child = '';
      if (symbol) {
        child = showPIN ? symbol : '●'; // Show or hide PIN
      } else if (isFocused) {
        child = /*#__PURE__*/_react.default.createElement(_reactNativeConfirmationCodeField.Cursor, null);
      }
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        key: index,
        style: style.cell,
        onLayout: getCellOnLayoutHandler(index)
      }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
        style: style.cellText,
        maxFontSizeMultiplier: 1
      }, child));
    },
    autoFocus: autoFocus,
    ref: ref
  }))), /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
    style: style.hideIcon,
    accessibilityLabel: showPIN ? t('PINCreate.Hide') : t('PINCreate.Show'),
    accessibilityRole: 'button',
    testID: showPIN ? (0, _testable.testIdWithKey)('Hide') : (0, _testable.testIdWithKey)('Show'),
    onPress: () => setShowPIN(!showPIN),
    hitSlop: _constants.hitSlop
  }, /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
    color: PINInputTheme.icon.color,
    name: showPIN ? 'visibility-off' : 'visibility',
    size: 30
  })));
  const inlineMessageView = ({
    message,
    inlineType,
    config
  }) => /*#__PURE__*/_react.default.createElement(_InlineErrorText.default, {
    message: message,
    inlineType: inlineType,
    config: config
  });
  const inlineMessagePlaceholder = placment => {
    if (inlineMessage && inlineMessage.config.position === placment) {
      return inlineMessageView(inlineMessage);
    }
    //This is a fallback in case no position provided
    if (inlineMessage && placment === _error.InlineErrorPosition.Above && !inlineMessage.config.position) {
      return inlineMessageView(inlineMessage);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.container
  }, label && /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.label, {
      marginBottom: 8
    }]
  }, label), inlineMessagePlaceholder(_error.InlineErrorPosition.Above), content(), inlineMessagePlaceholder(_error.InlineErrorPosition.Below));
};
const PINInput = /*#__PURE__*/(0, _react.forwardRef)(PINInputComponent);
var _default = exports.default = PINInput;
//# sourceMappingURL=PINInput.js.map