"use strict";

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

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

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

    /**
     * @returns {void}
     * @access public
     * @static
     */
    TestDeLeyCom.load_data = () => {

        /** @type {string} */
        const title = document.querySelector("#centro .nombreley2").innerText.trim();
        // No se establece como hijo porque hay casuística de anidamiento en UL.

        if(!TestDeLeyCom.DATA.some(item => item.title == title)){

            /** @type {number} */
            const i = TestDeLeyCom.DATA.length;

            TestDeLeyCom.DATA.push({
                /** @type {string} */
                origin : "TestDeLey.com",
                /** @type {Array.<string>} */
                sources : [window.location.href], 
                /** @type {string} */
                title : title,
                /** @type {Array.<Object.<string, string|Array.<string>>>} */
                queries : []
            });
            document.querySelectorAll("#listado>li").forEach(query_box => {

                /** @type {number} */
                const query = query_box.childNodes[0].nodeValue.trim().replace(/^[0-9]+\.\s*/, "");

                if(!TestDeLeyCom.DATA[i].queries.some(item => item.question == query)){

                    /** @type {number} */
                    const j = TestDeLeyCom.DATA[i].queries.length;
                    
                    TestDeLeyCom.DATA[i].queries.push({
                        /** @type {string} */
                        question: query,
                        /** @type {Array.<string>} */
                        rights : [], 
                        /** @type {Array.<string>} */
                        wrongs : []
                    });
                    query_box.querySelectorAll("div").forEach(answer_box => {

                        /** @type {string} */
                        const answer = answer_box.childNodes[0].nodeValue.trim(), 
                              /** @type {string} */
                              type = answer_box.classList.contains("correcto") ? "rights" : "wrongs";

                        ["rights", "wrongs"].some(type => TestDeLeyCom.DATA[i].queries[j][type].includes(answer)) || 
                        TestDeLeyCom.DATA[i].queries[j][type].push(answer);

                    });

                };

            });

        };

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

    };

    return TestDeLeyCom;
})();

TestDeLeyCom.load_data();