import React from 'react';
import { toast } from 'sonner';

export interface WalletProps {
  id: string;
  name: string;
  description: string;
  id_currency: string;
  status: string;
  group: WalletGroupProps[];
}

export interface WalletGroupProps {
  id: string;
  name: string;
  description: string;
  status: string;
}

export interface CurrencyProps {
  ID: string;
  code: string;
  name: string;
  prefix: string;
  status: string;
}

export interface GroupProps {
  id: string;
  name: string;
  description: string;
  status: string;
}

export const initialStateWallet: {
  name: string;
  description: string;
  status: string;
  group?: string[];
  id_currency?: string;
} = {
  name: '',
  description: '',
  status: '',
  group: [],
  id_currency: ''
};

export const validateFormsWallet = (
  formField: typeof initialStateWallet,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>,
  mode: 'create' | 'update' = 'create' // default: create
) => {
  const requiredFields =
    mode === 'create'
      ? [
          { key: 'name', label: 'Name' },
          { key: 'description', label: 'Description' },
          { key: 'status', label: 'Status' },
          { key: 'group', label: 'Group' },
          { key: 'id_currency', label: 'Currency' }
        ]
      : [
          { key: 'name', label: 'Name' },
          { key: 'description', label: 'Description' },
          { key: 'status', label: 'Status' }
        ];

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

  requiredFields.forEach(({ key, label }) => {
    const value = formField[key as keyof typeof formField];

    if (value === '' || value === null || value === undefined) {
      newErrors[key] = `${label} is required`;
      toast.error(`${label} is required`);
      isValid = false;
    }
  });

  setErrors(newErrors);
  return isValid;
};
