// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * "Tout déplier / Tout replier" link above the 3 fr-accordion sections
 * (Évaluations/Stages/CAPP) of the Vue élève screen (templates/
 * student_view.mustache). Deliberately not a .fr-accordions-group wrapper:
 * that DSFR class makes the group exclusive (opening one closes the
 * others), which would break the existing default of all 3 sections open
 * at once — see dsfr.module.min.js's AccordionsGroup (isGrouped=true by
 * default). Instead each button is driven individually, via a real
 * .click() rather than touching aria-expanded/fr-collapse--expanded by
 * hand, so DSFR's own Collapse instance stays the single source of truth
 * for expand/collapse state and the height transition still animates.
 *
 * @module     local_kid/student_accordions
 * @copyright  2026 INSP
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
import {get_string as getString} from 'core/str';

const SELECTORS = {
    CONTAINER: '[data-region="kid-student-sections"]',
    TOGGLE: '[data-role="kid-accordion-toggle-all"]',
    BUTTON: '.fr-accordion__btn',
};

/**
 * @param {Element[]} buttons
 * @returns {Boolean}
 */
const allExpanded = (buttons) => buttons.every((btn) => btn.getAttribute('aria-expanded') === 'true');

/**
 * @param {Element} toggle
 * @param {Boolean} expanded target state once clicked (i.e. the state the
 *        buttons are currently in)
 */
const refreshLabel = (toggle, expanded) => {
    const key = expanded ? 'student_accordions_collapse_all' : 'student_accordions_expand_all';
    getString(key, 'local_kid')
        .then((str) => {
            toggle.textContent = str;
            return str;
        })
        .catch(() => {
            // Non-critical: the button stays clickable with its previous label.
        });
};

/**
 * Entry point, called once by pages/student.php.
 */
export const init = () => {
    const container = document.querySelector(SELECTORS.CONTAINER);
    const toggle = document.querySelector(SELECTORS.TOGGLE);
    if (!container || !toggle) {
        return;
    }

    toggle.addEventListener('click', () => {
        const buttons = Array.from(container.querySelectorAll(SELECTORS.BUTTON));
        const target = !allExpanded(buttons);
        buttons
            .filter((btn) => (btn.getAttribute('aria-expanded') === 'true') !== target)
            .forEach((btn) => btn.click());
        refreshLabel(toggle, target);
    });
};
