// DatePickerField.tsx

import {
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@repo/shadcn-ui/components";

import { cn } from "@repo/shadcn-ui/lib/utils";
import { Control, FieldPath, FieldValues } from "react-hook-form";
import { useTranslation } from "../../i18n.ts";
import { TaxesMultiSelect } from "../taxes-multi-select.tsx";

type TaxesMultiSelectFieldProps<TFormValues extends FieldValues> = {
  control: Control<TFormValues>;
  name: FieldPath<TFormValues>;
  label?: string;
  placeholder?: string;
  description?: string;
  disabled?: boolean;
  required?: boolean;
  readOnly?: boolean;
  className?: string;
};

export function TaxesMultiSelectField<TFormValues extends FieldValues>({
  control,
  name,
  label,
  placeholder,
  description,
  disabled = false,
  required = false,
  readOnly = false,
  className,
}: TaxesMultiSelectFieldProps<TFormValues>) {
  const { t } = useTranslation();
  const isDisabled = disabled || readOnly;

  return (
    <FormField
      control={control}
      name={name}
      render={({ field }) => (
        <FormItem className={cn("space-y-0", className)}>
          {label && (
            <div className='mb-1 flex justify-between'>
              <div className='flex items-center gap-2'>
                <FormLabel htmlFor={name} className='m-0'>
                  {label}
                </FormLabel>
                {required && (
                  <span className='text-xs text-destructive'>{t("common.required")}</span>
                )}
              </div>
            </div>
          )}
          <FormControl>
            <TaxesMultiSelect disabled={isDisabled} placeholder={placeholder} {...field} />
          </FormControl>

          <FormDescription
            className={cn("text-xs text-muted-foreground", !description && "invisible")}
          >
            {description || "\u00A0"}
          </FormDescription>
          <FormMessage />
        </FormItem>
      )}
    />
  );
}
