import Toast from "./toast"
import Vue from 'vue'

let currentToast;

const index = (message, color) => {
    if (currentToast) {
        currentToast.close()
    }

    currentToast = createToast({
        Vue,
        message,
        color: color || 'success',
        onClose: () => {
            currentToast = null
        }
    });
};

function createToast({Vue, message, color, onClose}) {
    const Constructor = Vue.extend(Toast)

    let toast = new Constructor()
    toast.message = message
    toast.color = color
    toast.timeout = 1000
    toast.$mount();
    toast.$on('close', onClose)
    document.body.appendChild(toast.$el)
    return toast
}

export default index