"use strict";

import { forwardRef, useContext, useImperativeHandle, useRef, useState } from 'react';
import { TextInput, TouchableOpacity, View } from 'react-native';
import { ApplicationContext } from "../Context/index.js";
import { Icon } from "../Icon/index.js";
import { Shadow, Spacing, Styles } from "../Consts/index.js";
import { getFontStyle } from "./common.js";
import styles from "./styles.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const InputSearch = /*#__PURE__*/forwardRef(({
  onFocus,
  onBlur,
  size = 'medium',
  onChangeText,
  icon,
  iconColor,
  onPressIcon,
  useShadow,
  style,
  ...props
}, ref) => {
  const inputRef = useRef(null);
  const {
    theme
  } = useContext(ApplicationContext);
  const [focused, setFocused] = useState(false);
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current?.focus();
    },
    blur: () => {
      inputRef.current?.blur();
    },
    clear: onClearText,
    setText: value => {
      inputRef.current?.setNativeProps?.({
        text: value
      });
      _onChangeText(value);
    }
  }));
  const _onFocus = e => {
    setFocused(true);
    onFocus?.(e);
  };
  const _onBlur = e => {
    setFocused(false);
    onBlur?.(e);
  };
  const onClearText = () => {
    inputRef.current?.clear();
    _onChangeText('');
  };
  const _onChangeText = text => {
    onChangeText?.(text);
  };
  const getSizeStyle = () => {
    switch (size) {
      case 'large':
        return styles.searchInputLarge;
      case 'medium':
        return styles.searchInputMedium;
      default:
        return styles.searchInputSmall;
    }
  };
  const getIconSize = () => {
    switch (size) {
      case 'large':
        return 24;
      case 'medium':
        return 20;
      default:
        return 18;
    }
  };
  const renderInputView = () => {
    let textColor = theme.colors.text.default;
    let placeholderColor = theme.colors.text.hint;
    return /*#__PURE__*/_jsx(TextInput, {
      ref: inputRef,
      ...props,
      style: [getSizeStyle(), getFontStyle(theme, 'small'), {
        color: textColor,
        lineHeight: undefined
      }, style],
      onChangeText: _onChangeText,
      onFocus: _onFocus,
      onBlur: _onBlur,
      selectionColor: theme.colors.primary.default,
      placeholderTextColor: placeholderColor
    });
  };
  const renderIconView = () => {
    if (icon) {
      return /*#__PURE__*/_jsx(TouchableOpacity, {
        onPress: onPressIcon,
        style: Styles.paddingXS,
        children: typeof icon === 'string' ? /*#__PURE__*/_jsx(Icon, {
          name: icon,
          size: getIconSize(),
          color: iconColor
        }) : icon
      });
    }
    return null;
  };
  const defaultStyle = {
    borderWidth: 0.5,
    borderColor: theme.colors.border.default
  };
  return /*#__PURE__*/_jsxs(View, {
    style: [styles.searchInputWrapper, {
      backgroundColor: theme.colors.background.surface,
      paddingHorizontal: size === 'small' ? Spacing.S : Spacing.M
    }, useShadow ? Shadow.light : defaultStyle, focused && {
      borderColor: theme.colors.primary.default
    }],
    children: [/*#__PURE__*/_jsx(Icon, {
      name: 'magnify',
      size: getIconSize()
    }), renderInputView(), /*#__PURE__*/_jsxs(View, {
      style: Styles.rowCenter,
      children: [focused && /*#__PURE__*/_jsx(TouchableOpacity, {
        onPress: onClearText,
        style: Styles.paddingXS,
        children: /*#__PURE__*/_jsx(Icon, {
          name: 'close',
          size: getIconSize(),
          color: theme.colors.text.hint
        })
      }), renderIconView()]
    })]
  });
});
export default InputSearch;
//# sourceMappingURL=InputSearch.js.map