"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.IconAdornment = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _utils = require("./utils");
var _theming = require("../../theming");
var _IconButton = _interopRequireDefault(require("../../IconButton/IconButton"));
var _constants = require("../constants");
var _helpers = require("../helpers");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const StyleContext = /*#__PURE__*/_react.default.createContext({
  style: {},
  isTextInputFocused: false,
  forceFocus: () => {},
  testID: ''
});
const IconAdornment = ({
  icon,
  topPosition,
  side,
  isTextInputFocused,
  forceFocus,
  testID,
  theme: themeOverrides,
  disabled
}) => {
  const {
    isV3
  } = (0, _theming.useInternalTheme)(themeOverrides);
  const {
    ICON_OFFSET
  } = (0, _helpers.getConstants)(isV3);
  const style = {
    top: topPosition,
    [side]: ICON_OFFSET
  };
  const contextState = {
    style,
    isTextInputFocused,
    forceFocus,
    testID,
    disabled
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyleContext.Provider, {
    value: contextState,
    children: icon
  });
};

/**
 * A component to render a leading / trailing icon in the TextInput
 *
 * ## Usage
 * ```js
 * import * as React from 'react';
 * import { TextInput } from 'react-native-paper';
 *
 * const MyComponent = () => {
 *   const [text, setText] = React.useState('');
 *
 *   return (
 *     <TextInput
 *       label="Password"
 *       secureTextEntry
 *       right={<TextInput.Icon icon="eye" />}
 *     />
 *   );
 * };
 *
 * export default MyComponent;
 * ```
 */
exports.IconAdornment = IconAdornment;
const TextFieldIcon = ({
  icon,
  onPress,
  forceTextInputFocus = true,
  color: customColor,
  theme: themeOverrides,
  rippleColor,
  ...rest
}) => {
  const {
    style,
    isTextInputFocused,
    forceFocus,
    testID,
    disabled
  } = _react.default.useContext(StyleContext);
  const onPressWithFocusControl = _react.default.useCallback(e => {
    if (forceTextInputFocus && !isTextInputFocused) {
      forceFocus();
    }
    onPress?.(e);
  }, [forceTextInputFocus, forceFocus, isTextInputFocused, onPress]);
  const theme = (0, _theming.useInternalTheme)(themeOverrides);
  const iconColor = (0, _utils.getIconColor)({
    theme,
    disabled,
    isTextInputFocused,
    customColor
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: [styles.container, style],
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_IconButton.default, {
      icon: icon,
      style: styles.iconButton,
      size: _constants.ICON_SIZE,
      onPress: onPressWithFocusControl,
      iconColor: iconColor,
      testID: testID,
      theme: themeOverrides,
      rippleColor: rippleColor,
      ...rest
    })
  });
};
TextFieldIcon.displayName = 'TextField.Icon';
const styles = _reactNative.StyleSheet.create({
  container: {
    position: 'absolute',
    width: _constants.ICON_SIZE,
    height: _constants.ICON_SIZE,
    justifyContent: 'center',
    alignItems: 'center'
  },
  iconButton: {
    margin: 0
  }
});
var _default = exports.default = TextFieldIcon; // @component-docs ignore-next-line
//# sourceMappingURL=TextFieldIcon.js.map