import React from 'react'
import { LuX } from 'react-icons/lu'

interface InputWithClearButtonProps {
  id?: string
  type?: string
  className?: string
  placeholder?: string
  value: string
  onClick?: () => void
  onClear?: () => void
  readOnly?: boolean
  showClearButton?: boolean
}

const InputWithClearButton: React.FC<InputWithClearButtonProps> = ({
  id,
  type = 'text',
  className = '',
  placeholder,
  value,
  onClick,
  onClear,
  readOnly = false,
  showClearButton = false,
}) => {
  return (
    <div className="relative">
      <input
        id={id}
        type={type}
        className={`
          block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 
          focus:border-blue-500 focus:ring-blue-500 disabled:cursor-not-allowed disabled:opacity-50
          hover:border-blue-500 transition-colors duration-200
          ${showClearButton ? 'pr-8' : 'pr-2.5'}
          ${className}
        `}
        placeholder={placeholder}
        value={value}
        onClick={onClick}
        readOnly={readOnly}
        style={{ cursor: readOnly ? 'pointer' : 'text' }}
      />

      {/* Clear button integrated within the input styling */}
      {showClearButton && (
        <button
          type="button"
          onClick={onClear}
          className="absolute inset-y-0 right-0 flex items-center pr-2.5 text-gray-400 hover:text-gray-600 transition-colors focus:outline-none"
          style={{ background: 'none', border: 'none' }}
        >
          <LuX size={14} />
        </button>
      )}
    </div>
  )
}

export default InputWithClearButton
