
let hoverColor = "#e9b82f";
let normalColor = "#efbd95";

const rep2 = "assets/map";

function get_name(id){
    return id.replace("map-", "");
}

function clicked(elem) {


    let payload = {
        "function": 'get_map_info',
        'selector': get_name(elem.id),
    };

    function doChecks(response) {
        if (!response.ok) {
            throw Error(response.statusText);
        }
        return response;
    }

    function readIt(response) {
        return response.json()
    }

    fetch(rep2 + '/' + 'ajax.php', {
        method: 'POST',
        body: JSON.stringify(payload),
        headers: {
            "Content-type": "application/json; charset=UTF-8"
        }
    }).then(doChecks).then(readIt).then((data) => {
        if (data.length > 0) {
            Swal.fire({
                title: '<strong id="modal-title">' + data[0]['title'] + '</strong>',
                html: data[0]['description']
            })
        } else {
            Swal.fire({
                title: "Erreur",
                html: "Une erreur est survenue",
                timer: 3000,
                timerProgressBar: true
            })
        }
    }).catch((error) => {
        console.error(error);
    });

}

function hover_in(elem){
    const css = elem.style

    css.fill = hoverColor
    css.cursor = "pointer"
}

function hover_out(elem){
    elem.style.fill = normalColor
}