"use strict";

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

    /**
     * @callback cookies_click_callback
     * @returns {void}
     */

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

    /**
     * @param {!string} i 
     * @param {!Object.<string, string>} fixer 
     * @param {!string} option 
     * @returns {string}
     * @access public
     * @static
     */
    Baquedano.get_answer_key = (i, fixer, option) => fixer[i] == option ? "rights" : "wrongs";

    /**
     * @returns {void}
     * @access public
     * @static
     */
    Baquedano.load_txt = () => {

        /** @type {string} */
        const string = document.querySelector("body").innerText, 
              /** @type {[string, string, string, string]} */
              [_, data_string, test_block, fix_block] = string.split(/^\#{3}\s*(?:test|fix|cor+ec+i[oó]n|data)/im), 
              /** @type {Object.<string, string>} */
              fix_values = [...fix_block.matchAll(/^([0-9]+) *([a-d])/gmi)].concat(
                  [...fix_block.matchAll(/^([a-d]) *([0-9]+)/gmi)].map(([_, right, question]) => [_, question, right])
              ).reduce((values, [_, question, right]) => {
              
                  values[question] = right;
              
                  return values;
              }, {}), 
              /** @type {Object.<string, string|Array.<string|Object.<string, string|Array.<string>>>} */
              data = JSON.parse(data_string.trim());
        let i = -1, question = "", answer, option, number;

        data.queries = [...test_block.trim().split(/\n/)].reduce((queries, line) => {

            if(!line.trim()){
                if(question){
                    queries.push({
                        question : question.trim(), 
                        rights : [], 
                        wrongs : []
                    });
                    i ++;
                    question = "";
                }else if(answer){
                    queries[i][Baquedano.get_answer_key(number, fix_values, option)].push(answer.trim());
                    answer = "";
                };
            }else if(question)
                question += " " + line.trim();
            else if(answer){
                if(/^[a-d]\) */i.test(line)){
                    queries[i][Baquedano.get_answer_key(number, fix_values, option)].push(answer.trim());
                    option = line[0];
                    answer = line.replace(/^[a-d]\) */i, "").trim();
                }else 
                    answer += " " + line.trim();
            }else if(/^[0-9]+ +/.test(line)){
                number = line.match(/^[0-9]+/)[0];
                question = line.trim().replace(/^[0-9]+ +/, "");
            }else if(/^[a-d]\) */i.test(line)){
                option = line[0];
                answer = line.replace(/^[a-d]\) */i, "").trim();
            };

            return queries;
        }, []);
        data.queries[i][Baquedano.get_answer_key(number, fix_values, option)].push(answer.trim());

        console.log(JSON.stringify([data]));

    };

    return Baquedano;
})();

Baquedano.load_txt();