import {
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/shared/ui/form";
import React from "react";
import { useFormContext } from "react-hook-form";
import { Switch } from "../ui/switch";
import { useTranslations } from "next-intl";

export interface FormSwitchProps {
  label?: React.ReactNode | string;
  name: string;
  required?: boolean;
  className?: string;
  onCheckedChange?: (checked: boolean) => void;
}

const FormSwitch: React.FC<FormSwitchProps> = ({
  name,
  label,
  className,
  required,
  onCheckedChange,
  ...props
}) => {
  const form = useFormContext();
  const t = useTranslations("LABELS");
  return (
    <FormField
      control={form.control}
      name={name}
      render={({ field }) => (
        <FormItem>
          <FormControl>
            <div className="flex items-center gap-2 ps-4">
              <Switch
                id={name}
                checked={field.value}
                onCheckedChange={onCheckedChange || field.onChange}
                required={required ? true : false}
                className={className}
                {...field}
                {...props}
              />
              <FormLabel>{typeof label === "string" ? t(label) : label}</FormLabel>
            </div>
          </FormControl>
          <FormMessage />
        </FormItem>
      )}
    />
  );
};

export default FormSwitch;
