import { cn } from './utils';
import type { VariantProps } from 'class-variance-authority';
import { cva } from 'class-variance-authority';
import { forwardRef } from 'react';

const variants = cva(
  '[&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none focus:ring-0 hover:border-primary-500 focus:border-primary-500 block w-full rounded-sm border disabled:hover:border-gray-300 border-gray-300 h-9 p-2 px-3 text-sm text-gray-900 outline-none transition-all ease-in-out disabled:cursor-not-allowed disabled:text-opacity-30'
);

export type InputProps = React.InputHTMLAttributes<HTMLInputElement> &
  VariantProps<typeof variants> & { readonly loading?: boolean };

export const Input = forwardRef<HTMLInputElement, InputProps>(
  ({ className, loading, ...props }, ref) => {
    if (loading) return <div className="h-full w-full animate-pulse rounded bg-gray-100" />;

    return <input ref={ref} type="text" className={cn(variants({ className }))} {...props} />;
  }
);

export const InputNumber = forwardRef<HTMLInputElement, InputProps>((props, ref) => (
  <Input ref={ref} type="number" {...props} />
));

export const InputFile = forwardRef<HTMLInputElement, InputProps>((props, ref) => (
  <input
    {...props}
    ref={ref}
    type="file"
    className="file:bg-primary-50 file:text-primary-500 hover:file:bg-primary-100 block w-full text-sm
        text-slate-500 file:mr-4 file:rounded-sm file:border-0
        file:px-4 file:py-2 file:text-sm file:font-semibold
        hover:file:cursor-pointer disabled:cursor-not-allowed disabled:opacity-30 disabled:file:cursor-not-allowed"
  />
));
