import { toast } from 'sonner';

export const initialStateCurrency: {
  code: string;
  name: string;
  prefix: string;
  status: string;
  created_by: string;
  created_at: string;
} = {
  code: '',
  name: '',
  prefix: '',
  status: '',
  created_by: '',
  created_at: ''
};

export const validateFormCurrency = (
  formField: typeof initialStateCurrency,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'code', label: 'Code' },
    { key: 'name', label: 'Name' },
    { key: 'prefix', label: 'Prefix' },
    { 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;
};
