import toastr from 'toastr';

toastr.options = {
    "closeButton": true,
    "debug": false,
    "newestOnTop": true,
    "progressBar": true,
    "positionClass": "toast-bottom-right",
    "preventDuplicates": false,
    "onclick": null,
    "showDuration": "300",
    "hideDuration": "1000",
    "timeOut": "10000",
    "extendedTimeOut": "1000",
    "showEasing": "swing",
    "hideEasing": "linear",
    "showMethod": "fadeIn",
    "hideMethod": "fadeOut"
};

function getWrapper() {
    return document.querySelector('#toast-container-builder');
}

function flashbagNotification() {
    let container = getWrapper();
    let notifications = container.querySelectorAll(':scope gda-error-node');

    // Parse notifications
    for (let i = 0; i < notifications.length; i++) {
        let level = notifications[i].getAttribute('data-level');
        let message = notifications[i].getAttribute('data-message');

        toastr[level](message);
    }

    container.innerHTML = '';// Clear container
}

function addError(type, message) {
    const toastContainerBuilder = getWrapper();
    if (toastContainerBuilder) {
        const error = document.createElement('gda-error-node');
        error.dataset.level = type;
        error.dataset.message = message;
        toastContainerBuilder.appendChild(error);
    }
}

function triggerFlashNotification(type, message) {
    addError(type, message);

    flashbagNotification();
}

export {
    triggerFlashNotification,
    flashbagNotification,
}