import { Alert, toast } from '@/components/ui';
import { t } from 'i18next';

const NalertToast = ({message, type}) => {
  if (typeof message === 'object') {
    return toast.push(
      <Alert showIcon closable type={type} rounded={false} duration={5000}>
        <ul key="NalertToast" className="list-disc px-10">
          {message.map((m) => {
            return <li key={m.statusCode}> {t(m.errorMessages)}</li>;
          })}
        </ul>
      </Alert>,
      {
        offsetX: 0,
        offsetY: 0,
        transitionType: 'fade',
        block: true,
      },
    );
  } else {
    // if message from axios or includes (axios) error return server error
    if (message.includes('axios') || message.includes('Axios')) {
      if (message.includes('401')) {
        message = t('UNAUTHORIZED');
      } else if (message.includes('403')) {
        message = t('FORBIDDEN');
      } else {
        message = t('SERVER_ERROR');
      }
    }
    return toast.push(
      <Alert showIcon closable type={type} rounded={false} duration={3000}>
        {t(message)}
      </Alert>,
      {
        offsetX: 0,
        offsetY: 0,
        transitionType: 'fade',
        block: true,
      },
    );
  }
};

export default NalertToast;
