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 demandes");
        
    }

initDisabling() {
    const checkInGame = document.querySelector('#game-controller select');

    checkInGame.addEventListener('change', () => {
        const selectedOption = checkInGame.options[checkInGame.selectedIndex];
        const gameID = checkInGame.value;
        const gameCanHaveMaster = selectedOption.dataset.gamemasters
            ? selectedOption.dataset.gamemasters.split("|")
            : [];

        const gamemastersToRefresh = Array.from(document.querySelectorAll("#gamemaster-controller option"));

        gamemastersToRefresh.forEach((el) => {
            if (!gameID) {
                el.disabled = true;
            } else {
                el.disabled = !gameCanHaveMaster.includes(el.value);
            }
        });
    });
}

}