import { requireElement, requireInputElement } from '/js/iemjs/domUtils.js';

document.addEventListener("DOMContentLoaded", () => {
    const theButton = requireElement("thebutton");
    const theText = requireElement("thetext");
    const theTitle = requireElement("thetitle");
    const theImage = requireElement("theimage");
    const theGeojson = requireElement("thegeojson");
    const theEastCheckbox = requireInputElement("eastcheckbox");

    theButton.addEventListener("click", () => {
        const text = theText.value;
        const title = theTitle.value;
        const east = theEastCheckbox.checked;
        fetch("generate_plot.py", {
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            body: new URLSearchParams({ text, title, east })
        })
        .then(response => {
            if (!response.ok) {throw new Error("Network response was not ok");}
            return response.json();
        })
        .then(data => {
            theImage.setAttribute("src", data.imgurl);
            theGeojson.textContent = JSON.stringify(data.geojson);
        })
        .catch(() => {
            theGeojson.textContent = "Image/GeoJSON Generation Failed, sorry!";
        });
    });
});
