"use strict";

/**
 * @class LaConstitucionEs
 * @constructor
 * @returns {void}
 * @access public
 * @static
 */
const LaConstitucionEs = (function () {

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

    /** @type {Array.<Object.<string, string|Object.<string, string|Array.<string>>>>} */
    LaConstitucionEs.DATA = [];

    /**
     * @returns {void}
     * @access public
     * @static
     */
    LaConstitucionEs.reject_cookies = () => {
        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();
        };
    };

    /**
     * @param {HTMLElement} item 
     * @returns {string}
     * @access public
     * @static
     */
    LaConstitucionEs.get_answer_text = item => {

        while ((item = item.parentNode).tagName.toLowerCase() != "li");

        return item.querySelector(".respuesta_txt").innerText.trim();
    };

    /**
     * @returns {void}
     * @access public
     * @static
     */
    LaConstitucionEs.get_test_data = () => {

        /** @type {HTMLDivElement} */
        const base = document.querySelector(".panel-primary");

        if (base) {

            /** @type {Object.<string, string|Object.<string, string|Array.<string>>>} */
            const data = {
                /** @type {string} */
                origin : "laConstitucion.es",
                /** @type {string} */
                sources : [window.location.href], 
                /** @type {string} */
                title: base.querySelector(".panel-heading h1").innerText,
                /** @type {Array.<Object.<string, string|Array.<string>>>} */
                queries: [...base.querySelectorAll(".panel-body>ul")].map(query => ({
                    /** @type {string} */
                    question: query.querySelector("b").innerText.trim(),
                    /** @type {Array.<string>} */
                    rights: [...query.querySelectorAll("li .correcta")].map(LaConstitucionEs.get_answer_text),
                    /** @type {Array.<string>} */
                    wrongs: [...query.querySelectorAll("li .erronea")].map(LaConstitucionEs.get_answer_text)
                }))
            };
            /** @type {number} */
            let i;

            if (LaConstitucionEs.DATA.some((item, j) => {
                if (item.title == data.title) {
                    i = j;
                    return true;
                };
                return false;
            })) {
                data.queries.forEach(query => {

                    /** @type {number} */
                    let j;

                    if (LaConstitucionEs.DATA[i].queries.some((item, k) => {
                        if (item.question == query.question) {
                            j = k;
                            return true;
                        };
                        return false;
                    }))
                        ["rights", "wrongs"].forEach(type => {
                            LaConstitucionEs.DATA[i].queries[j][type].push(...query[type].filter(answer => !LaConstitucionEs.DATA[i].queries[j][type].includes(answer)));
                        });
                    else
                        LaConstitucionEs.DATA[i].queries.push(query);
                });
            } else
                LaConstitucionEs.DATA.push(data);

            console.log(JSON.stringify(LaConstitucionEs.DATA));

        };

    };

    return LaConstitucionEs;
})();

LaConstitucionEs.reject_cookies();
LaConstitucionEs.get_test_data();