"use strict";

const LeyesDeOposiciones = (function(){

    /**
     * @constructs LeyesDeOposiciones
     * @returns {void}
     * @access private
     * @static
     */
    const LeyesDeOposiciones = function(){};

    /**
     * @returns {void}
     * @access public
     * @static
     */
    LeyesDeOposiciones.cookies_reject = () => {
        if (document.querySelector(".fc-dialog-container")) {
            document.querySelectorAll(".fc-dialog-container [type=checkbox]").forEach(checkbox => {
                checkbox.checked = false;
            });
            document.querySelector(".fc-dialog-container button.fc-manage-vendors").click();
            document.querySelectorAll(".fc-dialog-container [type=checkbox]:checked").forEach(checkbox => {
                checkbox.checked = false;
            });
            document.querySelector(".fc-dialog-container button.fc-confirm-choices").click();
        };
    };
    
    /**
     * @returns {void}
     * @access public
     * @static
     */
    LeyesDeOposiciones.autoselect_answers = () => {
        document.querySelectorAll(".wq-question").forEach(question => {
            question.querySelector(".chosen") || 
            question.querySelector(".wq_answerTxtCtr").click();
        });
    };

    /**
     * @returns {void}
     * @access public
     * @static
     */
    LeyesDeOposiciones.load_test_data = () => {
        console.log(JSON.stringify([{
            /** @type {string} */
            origin : "LeyesDeOposiciones.es",
            /** @type {Array.<string>} */
            sources : [window.location.href], 
            /** @type {string} */
            title : document.querySelector(".wp-block-heading").innerText.trim(),
            /** @type {Array.<Object.<string, Array.<string>|string>>} */
            queries : [...document.querySelectorAll(".wq-question")].map(question => {
                return {
                    /** @type {string} */
                    question : question.querySelector(".wq_questionTextCtr>h4").innerText.trim(),
                    /** @type {Object.<string, Array.<string>>} */
                    ...[...question.querySelectorAll(".wq_answerTxtCtr")].reduce((answers, answer) => {

                        answers[answer.parentNode.classList.contains("wq_correctAnswer") ? "rights" : "wrongs"].push(answer.innerText.trim().replace(/^[a-z]\)\s*/i, ""));

                        return answers;
                    }, {rights : [], wrongs : []})
                };
            })
        }]));
    };

    return LeyesDeOposiciones;
}());

LeyesDeOposiciones.cookies_reject();
LeyesDeOposiciones.autoselect_answers();
LeyesDeOposiciones.load_test_data();