import * as React from "react";
import { cn } from "@/lib/utils";
import { useLocaleDirection } from "@/hooks/useLocale";

interface InputProps extends React.ComponentProps<"input"> {
  icon?: React.ReactNode;
}

function Input({ className, type, icon, ...props }: InputProps) {
  const { locale } = useLocaleDirection();
  return (
    <div className="relative w-full">
      {icon && (
        <span className="absolute top-1/2 -translate-y-1/2 start-3 flex items-center w-[18px] h-[18px] pointer-events-none">
          {icon}
        </span>
      )}
      <input
        type={type}
        className={cn(
          "w-full h-11 flex-1 px-4 rounded-[8px] bg-white border border-white/20 text-black placeholder:text-black focus:outline-none focus:border-secondary-main",
          icon && "ps-[34px]",
          locale === "ar" ? "text-right" : "text-left",
          className
        )}
        {...props}
      />
    </div>
  );
}

interface InputWithLabelProps extends InputProps {
  label: string;
  required?: boolean;
}

function InputWithLabel({
  className,
  type,
  icon,
  label,
  required,
  ...props
}: InputWithLabelProps) {
  const { locale } = useLocaleDirection();
  return (
    <div className="relative w-full bg-white rounded-[8px]">
      {icon && (
        <span className="absolute top-1/2 -translate-y-1/2 start-3 flex items-center w-[18px] h-[18px] pointer-events-none">
          {icon}
        </span>
      )}
      <input
        type={type}
        placeholder=" "
        className={cn(
          "peer w-full h-11 flex-1 px-4 rounded-[8px] border border-white/20 text-black focus:outline-none focus:border-secondary-main relative z-2",
          icon && "ps-[34px]",
          locale === "ar" ? "text-right" : "text-left",
          className
        )}
        required={required}
        {...props}
      />
      <label
        className={cn(
          "absolute start-4 top-1/2 -translate-y-1/2 text-gray-400 text-sm pointer-events-none",
          "peer-placeholder-shown:block peer-not-placeholder-shown:hidden"
        )}
      >
        {label} {required && <span className="text-red-500">*</span>}
      </label>
    </div>
  );
}

export { Input, InputWithLabel };
