document.addEventListener('DOMContentLoaded', () => {
    const pole = document.getElementById('poleSelect');
    const discipline =
        document.getElementById('disciplineSelect');
    const preview =
        document.getElementById('courseCodePreview');
    const audienceBadge =
        document.getElementById('audienceBadge');
    const warning =
        document.getElementById('musicChildWarning');

    const publicInputs = Array.from(
        document.querySelectorAll(
            '[data-public-code]'
        )
    );

    const selectedPublicCodes = () =>
        publicInputs
            .filter(input => input.checked)
            .map(input => input.dataset.publicCode);

    const audienceCode = () => {
        const codes = selectedPublicCodes();
        const child = codes.includes('ENF');
        const adult = codes.includes('ADU');

        if (child && adult) return 'MIX';
        if (child) return 'ENF';
        if (adult) return 'ADU';

        return 'PUB';
    };

    const filterDisciplines = () => {
        const poleId = pole?.value || '';

        Array.from(discipline?.options || [])
            .forEach(option => {
                if (!option.value) {
                    option.hidden = false;
                    return;
                }

                option.hidden =
                    option.dataset.pole !== poleId;
            });

        const current =
            discipline?.selectedOptions?.[0];

        if (
            current?.value &&
            current.dataset.pole !== poleId
        ) {
            discipline.value = '';
        }
    };

    const refresh = () => {
        const poleCode =
            pole?.selectedOptions?.[0]
                ?.dataset?.code || 'POL';

        const disciplineCode =
            discipline?.selectedOptions?.[0]
                ?.dataset?.code || 'DISC';

        const audience = audienceCode();

        if (preview) {
            preview.textContent =
                poleCode +
                '-' +
                disciplineCode +
                '-' +
                audience;
        }

        if (audienceBadge) {
            audienceBadge.textContent =
                audience === 'MIX'
                    ? 'Public mixte'
                    : audience === 'ENF'
                    ? 'Enfant'
                    : audience === 'ADU'
                    ? 'Adulte'
                    : 'Aucun public';

            audienceBadge.dataset.type =
                audience.toLowerCase();
        }

        if (warning) {
            warning.hidden = !(
                poleCode === 'MUS' &&
                selectedPublicCodes()
                    .includes('ENF')
            );
        }

        publicInputs.forEach(input => {
            input.closest('.public-choice')
                ?.classList.toggle(
                    'is-selected',
                    input.checked
                );
        });
    };

    pole?.addEventListener('change', () => {
        filterDisciplines();
        refresh();
    });

    discipline?.addEventListener(
        'change',
        refresh
    );

    publicInputs.forEach(input => {
        input.addEventListener(
            'change',
            refresh
        );
    });

    filterDisciplines();
    refresh();
});
