import { toast } from 'sonner';

export const validateFormConversion = (
  formField: typeof initialStateConversion,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'id_currency_origin', label: 'Currency Origin' },
    { key: 'id_currency_destination', label: 'Currency Destination' },
    { key: 'buy', label: 'Buy' },
    { key: 'sell', label: 'Sell' },
    { key: 'status', label: 'Status' }
  ];

  const newErrors: Record<string, string> = {};
  let isValid = true;

  requiredFields.forEach(({ key, label }) => {
    if (
      formField[key as keyof typeof formField] === '' ||
      formField[key as keyof typeof formField] === null ||
      formField[key as keyof typeof formField] === undefined
    ) {
      newErrors[key] = `${label} is required`;
      toast.error(`${label} is required`);
      isValid = false;
    }
  });

  setErrors(newErrors);
  return isValid;
};

export const initialStateConversion: {
  status: string;
  id_currency_origin: string;
  id_currency_destination: string;
  buy: number | null;
  sell: number | null;
  created_by: string;
  created_at: string;
} = {
  status: '',
  id_currency_origin: '',
  id_currency_destination: '',
  buy: null,
  sell: null,
  created_by: '',
  created_at: ''
};
