import React, { Component } from 'react';

import { toast } from 'react-toastify';
import { FormattedMessage } from 'react-intl';

import messages from '../../messages.js';
import msgs from './messages.js';



function saved(payload, options) {
    toast.success(renderContent(messages.notificationSaved, payload), o(options));
}

function deleted(payload, options) {
    toast.info(renderContent(messages.notificationDeleted, payload), o(options));
}

function warning(payload, options) {
    toast.warn(renderContent(messages.notificationError, payload), o(options));
}

function error(e, payload, options) {
    toast.error(renderContent(messages.notificationError, payload, e), o(options, { autoClose: false }));
}




function emailSent(e, payload, options) {
    toast.success(renderContent(msgs.emailSent, payload), o(options));
}

function validationFailed(errors, payload, options) {
    toast.warn(renderContent(msgs.validationErrors, null, errors), o(options));
}


function renderContent(msg, payload, data) {
    if (msg && msg.id) {
        return <FormattedMessage {...msg} values={ data } />
    }
    return payload;
}

function o(o1, o2) {
    let opts = {};
    if (o1) {
        opts = { ...opts, ...o1 };
    }
    if (o2) {
        opts = { ...opts, ...o2 };    
    }
}

export default {

    saved, deleted, error,

    emailSent, validationFailed

};