"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextInputValidation = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeReanimated = _interopRequireDefault(require("react-native-reanimated"));
var _context = require("../../context");
var _IOTransitions = require("../../core/IOTransitions");
var _functions = require("../../functions");
var _icons = require("../icons");
var _TextInputBase = require("./TextInputBase");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function isValidationWithOptions(validation) {
  return typeof validation === "object" && "isValid" in validation && "errorMessage" in validation;
}
const feedbackIconSize = 24;
const TextInputValidation = exports.TextInputValidation = /*#__PURE__*/(0, _react.forwardRef)(({
  onValidate,
  errorMessage,
  value,
  bottomMessage,
  onBlur,
  onFocus,
  validationMode = "onBlur",
  accessibilityErrorLabel,
  ...props
}, ref) => {
  const theme = (0, _context.useIOTheme)();
  const [isValid, setIsValid] = (0, _react.useState)(undefined);
  const [errMessage, setErrMessage] = (0, _react.useState)(errorMessage);
  const getErrorFeedback = (0, _react.useCallback)((isValid, message) => {
    setIsValid(isValid);
    setErrMessage(message);
    if (!isValid) {
      (0, _functions.triggerHaptic)("notificationError");
      _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions(accessibilityErrorLabel ?? message, {
        queue: true
      });
    } else {
      (0, _functions.triggerHaptic)("notificationSuccess");
    }
  }, [accessibilityErrorLabel]);
  const validateInput = (0, _react.useCallback)(() => {
    const validation = onValidate(value);
    if (isValidationWithOptions(validation)) {
      getErrorFeedback(validation.isValid, validation.errorMessage);
    } else {
      getErrorFeedback(validation, errorMessage);
    }
  }, [value, errorMessage, onValidate, getErrorFeedback]);

  // Expose the validateInput function to the parent component
  (0, _react.useImperativeHandle)(ref, () => ({
    validateInput
  }));
  const onBlurHandler = (0, _react.useCallback)(() => {
    if (validationMode === "onBlur") {
      validateInput();
    }
    onBlur?.();
  }, [validationMode, validateInput, onBlur]);
  const onFocusHandler = (0, _react.useCallback)(() => {
    setIsValid(undefined);
    onFocus?.();
  }, [onFocus]);
  const labelError = (0, _react.useMemo)(() => isValid === false && errMessage ? errMessage : bottomMessage, [isValid, errMessage, bottomMessage]);
  const labelErrorColor = (0, _react.useMemo)(() => isValid === false && errMessage ? theme.errorText : undefined, [isValid, errMessage, theme.errorText]);
  const feedbackIconAttrMap = (0, _react.useMemo)(() => ({
    valid: {
      name: "success",
      color: theme.successIcon
    },
    notValid: {
      name: "errorFilled",
      color: theme.errorIcon
    }
  }), [theme]);
  const feedbackIcon = (0, _react.useMemo)(() => {
    const validationStatus = isValid ? "valid" : "notValid";
    return isValid !== undefined ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
      entering: _IOTransitions.enterTransitionInputIcon,
      exiting: _IOTransitions.exitTransitionInputIcon,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
        name: feedbackIconAttrMap[validationStatus].name,
        color: feedbackIconAttrMap[validationStatus].color,
        size: feedbackIconSize
      })
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        width: feedbackIconSize,
        height: feedbackIconSize
      }
    });
  }, [feedbackIconAttrMap, isValid]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextInputBase.TextInputBase, {
    ...props,
    value: value,
    status: isValid === false ? "error" : undefined,
    bottomMessage: labelError,
    bottomMessageColor: labelErrorColor,
    rightElement: feedbackIcon,
    onBlur: onBlurHandler,
    onFocus: onFocusHandler
  });
});
//# sourceMappingURL=TextInputValidation.js.map