"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useThemeColors = require("../../hooks/useThemeColors");
var _Outline = require("./Addons/Outline");
var _enums = require("./Adornment/enums");
var _TextFieldAdornment = _interopRequireWildcard(require("./Adornment/TextFieldAdornment"));
var _constants = require("./constants");
var _helpers = require("./helpers");
var _InputLabel = _interopRequireDefault(require("./Label/InputLabel"));
var _LabelBackground = _interopRequireDefault(require("./Label/LabelBackground"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const TextInputOutlined = ({
  disabled = false,
  editable = true,
  label,
  error = false,
  selectionColor: customSelectionColor,
  cursorColor,
  underlineColor: _underlineColor,
  outlineColor: customOutlineColor,
  activeOutlineColor,
  outlineStyle,
  textColor,
  dense,
  style,
  theme,
  render = props => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TextInput, {
    ...props
  }),
  multiline = false,
  parentState,
  innerRef,
  onFocus,
  forceFocus,
  onBlur,
  onChangeText,
  onLayoutAnimatedText,
  onLabelTextLayout,
  onLeftAffixLayoutChange,
  onRightAffixLayoutChange,
  onInputLayout,
  onLayout,
  left,
  right,
  placeholderTextColor,
  testID = 'text-input-outlined',
  contentStyle,
  scaledLabel,
  ...rest
}) => {
  const adornmentConfig = (0, _TextFieldAdornment.getAdornmentConfig)({
    left,
    right
  });
  const {
    isV3,
    roundness
  } = theme;
  const font = isV3 ? theme.fonts.bodyLarge : theme.fonts.default;
  const hasActiveOutline = parentState.focused || error;
  const {
    INPUT_PADDING_HORIZONTAL,
    MIN_HEIGHT,
    ADORNMENT_OFFSET,
    MIN_WIDTH
  } = (0, _helpers.getConstants)(isV3);
  const themeColors = (0, _useThemeColors.useThemeColors)();
  const {
    fontSize: fontSizeStyle,
    fontWeight,
    lineHeight: lineHeightStyle,
    height,
    backgroundColor: styleBackgroundColor,
    textAlign,
    ...viewStyle
  } = _reactNative.StyleSheet.flatten(style) || {};
  const backgroundColor = styleBackgroundColor ?? themeColors.background;
  const fontSize = fontSizeStyle || _constants.MAXIMIZED_LABEL_FONT_SIZE;
  // Always set a minimum lineHeight to prevent text cutoff
  // Use at least 1.4x fontSize for better text spacing
  const lineHeight = lineHeightStyle || fontSize * 1.4;
  const {
    inputTextColor,
    activeColor,
    outlineColor,
    placeholderColor,
    errorColor,
    selectionColor
  } = (0, _helpers.getOutlinedInputColors)({
    activeOutlineColor,
    customOutlineColor,
    customSelectionColor,
    textColor,
    disabled,
    error,
    theme
  });

  // Override text color with theme color (unless custom textColor prop is provided)
  // If disabled, keep the original inputTextColor which handles opacity correctly
  const finalInputTextColor = textColor ?? (disabled ? inputTextColor : themeColors.text);
  const densePaddingTop = label ? _constants.LABEL_PADDING_TOP_DENSE : 0;
  const paddingTop = label ? _constants.LABEL_PADDING_TOP : 0;
  const yOffset = label ? _constants.OUTLINE_MINIMIZED_LABEL_Y_OFFSET : 0;
  const labelScale = _constants.MINIMIZED_LABEL_FONT_SIZE / fontSize;
  const fontScale = _constants.MAXIMIZED_LABEL_FONT_SIZE / fontSize;
  const labelWidth = parentState.labelLayout.width;
  const labelHeight = parentState.labelLayout.height;
  const labelHalfWidth = labelWidth / 2;
  const labelHalfHeight = labelHeight / 2;
  const baseLabelTranslateX = (_reactNative.I18nManager.getConstants().isRTL ? 1 : -1) * (labelHalfWidth - labelScale * labelWidth / 2 - (fontSize - _constants.MINIMIZED_LABEL_FONT_SIZE) * labelScale);
  let labelTranslationXOffset = 0;
  const isAdornmentLeftIcon = adornmentConfig.some(({
    side,
    type
  }) => side === _enums.AdornmentSide.Left && type === _enums.AdornmentType.Icon);
  const isAdornmentRightIcon = adornmentConfig.some(({
    side,
    type
  }) => side === _enums.AdornmentSide.Right && type === _enums.AdornmentType.Icon);
  if (isAdornmentLeftIcon) {
    labelTranslationXOffset = (_reactNative.I18nManager.getConstants().isRTL ? -1 : 1) * (_constants.ADORNMENT_SIZE + ADORNMENT_OFFSET - (isV3 ? 0 : 8));
  }
  const minInputHeight = (dense ? _constants.MIN_DENSE_HEIGHT_OUTLINED : MIN_HEIGHT) - paddingTop;
  const inputHeight = (0, _helpers.calculateInputHeight)(labelHeight, height, minInputHeight);
  const topPosition = (0, _helpers.calculateLabelTopPosition)(labelHeight, inputHeight, paddingTop);
  if (height && typeof height !== 'number') {
    // eslint-disable-next-line
    console.warn('Currently we support only numbers in height prop');
  }
  const paddingSettings = {
    height: height ? +height : null,
    labelHalfHeight,
    offset: paddingTop,
    multiline: multiline ? multiline : null,
    dense: dense ? dense : null,
    topPosition,
    fontSize,
    lineHeight,
    label,
    scale: fontScale,
    isAndroid: _reactNative.Platform.OS === 'android',
    styles: _reactNative.StyleSheet.flatten(dense ? styles.inputOutlinedDense : styles.inputOutlined)
  };
  const pad = (0, _helpers.calculatePadding)(paddingSettings);
  const paddingOut = (0, _helpers.adjustPaddingOut)({
    ...paddingSettings,
    pad
  });
  const baseLabelTranslateY = -labelHalfHeight - (topPosition + yOffset);
  const {
    current: placeholderOpacityAnims
  } = React.useRef([new _reactNative.Animated.Value(0), new _reactNative.Animated.Value(1)]);
  const placeholderOpacity = hasActiveOutline ? parentState.labeled : placeholderOpacityAnims[parentState.labelLayout.measured ? 1 : 0];
  const placeholderStyle = {
    position: 'absolute',
    left: 0,
    paddingHorizontal: INPUT_PADDING_HORIZONTAL
  };
  const placeholderTextColorBasedOnState = parentState.displayPlaceholder ? placeholderTextColor ?? placeholderColor : 'transparent';
  const labelBackgroundColor = backgroundColor === 'transparent' ? theme.colors.background : backgroundColor;
  const labelProps = {
    label,
    onLayoutAnimatedText,
    onLabelTextLayout,
    placeholderOpacity,
    labelError: error,
    placeholderStyle,
    baseLabelTranslateY,
    baseLabelTranslateX,
    font,
    fontSize,
    lineHeight,
    fontWeight,
    labelScale,
    wiggleOffsetX: _constants.LABEL_WIGGLE_X_OFFSET,
    topPosition,
    hasActiveOutline,
    activeColor,
    placeholderColor,
    backgroundColor: labelBackgroundColor,
    errorColor,
    labelTranslationXOffset,
    roundness,
    maxFontSizeMultiplier: rest.maxFontSizeMultiplier,
    testID,
    contentStyle,
    inputContainerLayout: {
      width: parentState.inputContainerLayout.width + (isAdornmentRightIcon || isAdornmentLeftIcon ? INPUT_PADDING_HORIZONTAL : 0)
    },
    opacity: parentState.value || parentState.focused ? parentState.labelLayout.measured ? 1 : 0 : 1,
    isV3
  };
  const onLayoutChange = React.useCallback(e => {
    onInputLayout(e);
    onLayout?.(e);
  }, [onLayout, onInputLayout]);
  const minHeight = height || (dense ? _constants.MIN_DENSE_HEIGHT_OUTLINED : MIN_HEIGHT);
  const outlinedHeight = inputHeight + (dense ? densePaddingTop / 2 : paddingTop);
  const {
    leftLayout,
    rightLayout
  } = parentState;
  const leftAffixTopPosition = (0, _helpers.calculateOutlinedIconAndAffixTopPosition)({
    height: outlinedHeight,
    affixHeight: leftLayout.height || 0,
    labelYOffset: -yOffset
  });
  const rightAffixTopPosition = (0, _helpers.calculateOutlinedIconAndAffixTopPosition)({
    height: outlinedHeight,
    affixHeight: rightLayout.height || 0,
    labelYOffset: -yOffset
  });
  const iconTopPosition = (0, _helpers.calculateOutlinedIconAndAffixTopPosition)({
    height: outlinedHeight,
    affixHeight: _constants.ADORNMENT_SIZE,
    labelYOffset: -yOffset
  });
  const rightAffixWidth = right ? rightLayout.width || _constants.ADORNMENT_SIZE : _constants.ADORNMENT_SIZE;
  const leftAffixWidth = left ? leftLayout.width || _constants.ADORNMENT_SIZE : _constants.ADORNMENT_SIZE;
  const adornmentStyleAdjustmentForNativeInput = (0, _TextFieldAdornment.getAdornmentStyleAdjustmentForNativeInput)({
    adornmentConfig,
    rightAffixWidth,
    leftAffixWidth,
    mode: 'outlined',
    isV3
  });
  const affixTopPosition = {
    [_enums.AdornmentSide.Left]: leftAffixTopPosition,
    [_enums.AdornmentSide.Right]: rightAffixTopPosition
  };
  const onAffixChange = {
    [_enums.AdornmentSide.Left]: onLeftAffixLayoutChange,
    [_enums.AdornmentSide.Right]: onRightAffixLayoutChange
  };
  let adornmentProps = {
    adornmentConfig,
    forceFocus,
    topPosition: {
      [_enums.AdornmentType.Icon]: iconTopPosition,
      [_enums.AdornmentType.Affix]: affixTopPosition
    },
    onAffixChange,
    isTextInputFocused: parentState.focused,
    maxFontSizeMultiplier: rest.maxFontSizeMultiplier,
    disabled
  };
  if (adornmentConfig.length) {
    adornmentProps = {
      ...adornmentProps,
      left,
      right,
      textStyle: {
        ...font,
        fontSize,
        lineHeight,
        fontWeight
      },
      visible: parentState.labeled
    };
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: viewStyle,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Outline.Outline, {
      isV3: isV3,
      style: outlineStyle,
      label: label,
      roundness: roundness,
      hasActiveOutline: hasActiveOutline,
      focused: parentState.focused,
      activeColor: activeColor,
      outlineColor: outlineColor,
      backgroundColor: backgroundColor
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: [styles.labelContainer, {
        paddingTop,
        minHeight,
        flex: 1,
        flexShrink: 1
      }],
      children: [label ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_InputLabel.default, {
        labeled: parentState.labeled,
        error: parentState.error,
        focused: parentState.focused,
        scaledLabel: scaledLabel,
        wiggle: Boolean(parentState.value && labelProps.labelError),
        labelLayoutMeasured: parentState.labelLayout.measured,
        labelLayoutWidth: parentState.labelLayout.width,
        labelLayoutHeight: parentState.labelLayout.height,
        ...labelProps,
        labelBackground: _LabelBackground.default,
        maxFontSizeMultiplier: rest.maxFontSizeMultiplier
      }) : null, render?.({
        ...rest,
        ref: innerRef,
        onLayout: onLayoutChange,
        onChangeText,
        placeholder: rest.placeholder,
        editable: !disabled && editable,
        selectionColor,
        cursorColor: typeof cursorColor === 'undefined' ? activeColor : cursorColor,
        placeholderTextColor: placeholderTextColorBasedOnState,
        onFocus,
        onBlur,
        underlineColorAndroid: 'transparent',
        multiline,
        style: [styles.input, !multiline || multiline && height ? {
          height: inputHeight
        } : {}, paddingOut, {
          ...font,
          fontSize,
          lineHeight,
          fontWeight,
          color: finalInputTextColor,
          textAlignVertical: multiline ? 'top' : 'center',
          textAlign: textAlign ? textAlign : _reactNative.I18nManager.getConstants().isRTL ? 'right' : 'left',
          paddingHorizontal: INPUT_PADDING_HORIZONTAL,
          flex: 1,
          flexShrink: 1,
          minWidth: 0
        }, _reactNative.Platform.OS === 'web' ? {
          outline: 'none'
        } : undefined, adornmentStyleAdjustmentForNativeInput, contentStyle],
        testID
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextFieldAdornment.default, {
      ...adornmentProps
    })]
  });
};
var _default = exports.default = TextInputOutlined;
const styles = _reactNative.StyleSheet.create({
  labelContainer: {
    paddingBottom: 0,
    flexGrow: 1,
    overflow: 'visible'
  },
  input: {
    margin: 0,
    flexGrow: 1,
    flexShrink: 1,
    minWidth: 0
  },
  inputOutlined: {
    paddingTop: 8,
    paddingBottom: 12
  },
  inputOutlinedDense: {
    paddingTop: 4,
    paddingBottom: 10
  }
});
//# sourceMappingURL=TextFieldOutlined.js.map