// errorReport module

import $ from "./jquery.module.js";

function showDialog(contents) {
    let html = "<div class=\"my-dialog\">";
    if (contents.hasOwnProperty("header")) {
        html += "<h3>" + contents.header + "</h3>";
    }
    if (contents.hasOwnProperty("message")) {
        html += "<p>" + contents.message + "</p>"
    }
    if (contents.hasOwnProperty("buttons")) {
        for (let key in contents.buttons) {
            if (contents.buttons.hasOwnProperty(key)) {
                html += "<button class=\"my-dialog-btn\" data-dialogkey=\"" + key + "\">" + key + "</button>";
            }
        }
    }
    html += "</div>";

    $("body").append(html);

    $("[data-dialogkey]").each(function (index) {
        let key = $(this).attr("data-dialogkey");
        if (typeof(contents.buttons[key]) == "function") {
            $(this).click(function() {
                $(".my-dialog").remove();
                contents.buttons[key]();
            });
        }
        else {
            $(this).click(function() { $(".my-dialog").remove();});
        }
    });
}

export {showDialog};