import { Util } from "../../util.js";

export class ToastsTemplate {
    static update(render, { message = "" } = {}) {
        if (!message) {
            return;
        }

        const toasts = ToastsTemplate.toasts;

        toasts.push(message);

        if (ToastsTemplate.isRunnning) {
            return;
        }

        ToastsTemplate.run(render, toasts, () => {
            ToastsTemplate.isRunnning = false;

            ToastsTemplate.renderReady(render);
        });
    }

    static run(render, toasts, callback) {
        ToastsTemplate.isRunnning = true;

        if (toasts.length > 0) {
            ToastsTemplate.renderToast(render, toasts.shift());
            setTimeout(() => {
                ToastsTemplate.run(render, toasts, callback);
            }, 2000);
        } else {
            return callback();
        }

        return true;
    }

    static renderReady(render) {
        const now = Util.formatDate(new Date());

        console.warn(now, "Ready");
        render`<span>${now} Ready.</span>`;
    }

    static renderToast(render, message) {
        const now = Util.formatDate(new Date());

        console.warn(now, message);
        render`<span class="bg-yellow">${message}.</span>`;

    }
}

ToastsTemplate.toasts = [];
ToastsTemplate.isRunnning = false;

@@ -117,63 +117,8 @@
 ) %7B%0A
-        if (!message) %7B%0A            return;%0A        %7D%0A%0A
     
@@ -160,16 +160,43 @@
 oasts;%0A%0A
+        if (message) %7B%0A    
         
@@ -216,16 +216,26 @@
 essage);
+%0A        %7D
 %0A%0A      

Fix Ready toats.