'use strict';

function attachHandlerSelector(section, selector, event, func) {
    section.querySelectorAll(selector).forEach(item => {
        item.addEventListener(event, func);
    });
}

window.addEventListener('DOMContentLoaded', () => {
    attachHandlerSelector(document, 'button.delete-button', 'click', (ev) => {
        let resp = confirm("Voulez-vous vraiment supprimer cette ligne ?");
        if (!resp) { ev.preventDefault(); ev.stopImmediatePropagation(); }
    });
    attachHandlerSelector(document, 'img.fichier_son', 'click', (ev) => {
        const soundModal = document.getElementById('modal_sound');
        soundModal.querySelector("audio").src = ev.target.dataset.path;
        soundModal.querySelector("#download-link").href = ev.target.dataset.path;
        soundModal.querySelector("#song-title").textContent = (
            `${ev.target.closest("tr").querySelector(".titre_chant").textContent} - ${ev.target.ariaLabel}`
        );
        const myModal = new bootstrap.Modal(soundModal);
        myModal.show();
    });
    attachHandlerSelector(document, '#modal_sound', 'hidden.bs.modal', (ev) => {
        const audio = ev.target.querySelector("audio");
        audio.pause(); // Stop playing
        audio.currentTime = 0;
        audio.src = "";
    })
    attachHandlerSelector(document, ".affiche img", "click", (ev) => {
        const src = ev.target.src;
        document.querySelector("#modal-img").src = ev.target.src;
        new bootstrap.Modal('#imagemodal').show();
    });
    const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
    const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
})
