import { Controller } from '@hotwired/stimulus';

/*
 * Contrôleur Stimulus pour le formulaire JEUX.
 */

export default class extends Controller {

    connect() {
        this.initDisabling();
        console.log("Stimulus: gestion des désactivation d'options dans les parties");
        
    }

initDisabling() {
    const checkInGamemaster = document.querySelector('#gamemaster-controller select');

    checkInGamemaster.addEventListener('change', () => {
        const selectedOption = checkInGamemaster.options[checkInGamemaster.selectedIndex];
        const gamemasterID = checkInGamemaster.value;
        const gamemasterCanMaster = selectedOption.dataset.games
            ? selectedOption.dataset.games.split("|")
            : [];

        const gamesToRefresh = Array.from(document.querySelectorAll("#game-controller option"));

        gamesToRefresh.forEach((el) => {
            if (!gamemasterID) {
                el.disabled = true;
            } else {
                el.disabled = !gamemasterCanMaster.includes(el.value);
            }
        });
    });
}

}