import * as React from "react";
import { cn } from "@/lib/utils";
import { Eye, EyeClosed, EyeClosedIcon, EyeOffIcon } from "lucide-react";

interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  error?: string[];
  label?: string;
  helperText?: string;
  required?: boolean;
}

const Input = React.forwardRef<HTMLInputElement, InputProps>(
  ({ className, type, error, label, helperText, required = false, ...props }, ref) => {

    const [showPassword , setShowPassword] = React.useState<boolean>(false)
    return (
      <div className="w-full space-y-2">
        {label && (
          <div className="flex text-sm font-medium text-gray-700">
            {label}
            {required && <span className="text-red-500 ml-1">*</span>}
          </div>
        )}
        <div className="relative">
          <input
            type={type == 'password' ? showPassword ? 'text' : 'password' : type}
            className={cn(
              "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
              error && error.length > 0 && "border-red-500 focus-visible:ring-red-500",
              className
            )}
            ref={ref}
            {...props}
          />
          {
            type == 'password' && 
            <button type="button" onClick={() => setShowPassword((prev) => !prev)} className="absolute top-[50%] translate-y-[-50%] right-4">
              {
                showPassword ? <Eye className="w-4 h-4"/> : <EyeOffIcon className="w-4 h-4"/>
              }
            </button>
          }
        </div>
        {helperText && (
          <p className="text-sm text-muted-foreground">
            {helperText}
          </p>
        )}
        {error && error.map((message, index) => (
          <p key={index} className="text-sm font-medium text-red-500">
            {message}
          </p>
        ))}
      </div>
    )
  }
)

Input.displayName = "Input"

export { Input }