import { toast } from 'sonner';

export const initialStateMenu: {
  module: string;
  name: string;
  link: string;
  id_parent: string;
  order_number: string | number | null;
  icon: string;
  status: string;
} = {
  module: '',
  name: '',
  link: '',
  id_parent: '',
  order_number: null,
  icon: '',
  status: ''
};

export const validateFormsMenu = (
  formField: typeof initialStateMenu,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'module', label: 'Module' },
    { key: 'name', label: 'Name' },
    { key: 'link', label: 'Link' },
    { 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;
};
