function crearEnlace(href, innerHTML) {
    let a = document.createElement("a");
    a.setAttribute("href", href);
    a.innerHTML = innerHTML;
    return a;
}

function generarTOC(toc) {
    let i1 = 0, i2 = 0, i3 = 0, i4 = 0;
    // let div = document.createElement("div");
    // div.id = 'toc-lista';
    // toc = toc.appendChild(div);
    toc = window.document.getElementById("indice");
    toc = toc.appendChild(document.createElement("ul"));
    let contenido = window.document.getElementById("contenido");
    for (let i = 0; i < contenido.childNodes.length; i++) {
        let node = contenido.childNodes[i];
        let tagName = node.nodeName.toLowerCase();
        if (tagName === "h4") {
            i4++;
            if (i4 === 1) toc.lastChild.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("ul"));
            let section = i1 + "." + i2 + "." + i3 + "." + i4;
            node.insertBefore(document.createTextNode(section + "."), node.firstChild);
            node.id = "section" + section;
            node.setAttribute("class", "ancla");
            toc.lastChild.lastChild.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("li")).appendChild(crearEnlace("#section" + section, node.innerHTML));
        } else if (tagName === "h3") {
            i3++;
            i4 = 0;
            if (i3 === 1) toc.lastChild.lastChild.lastChild.appendChild(document.createElement("ul"));
            let section = i1 + "." + i2 + "." + i3;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            node.setAttribute("class", "ancla");
            toc.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("li")).appendChild(crearEnlace("#section" + section, node.innerHTML));
        } else if (tagName === "h2") {
            i2++;
            i3 = 0;
            i4 = 0;
            if (i2 === 1) toc.lastChild.appendChild(document.createElement("ul"));
            let section = i1 + "." + i2;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            node.setAttribute("class", "ancla");
            toc.lastChild.lastChild.appendChild(document.createElement("li")).appendChild(crearEnlace("#section" + section, node.innerHTML));
        } else if (tagName === "h1") {
            i1++;
            i2 = 0;
            i3 = 0;
            i4 = 0;
            let section = i1;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section
            node.setAttribute("class", "ancla");
            toc.appendChild(document.createElement("li")).appendChild(crearEnlace("#section" + section, node.innerHTML));
        }
    }
}

function cambiarTitulo() {
    let h1 = document.getElementsByTagName('h1');
    document.title = h1[0].textContent;
}


function mostrar_indice() {
    if (window.document.getElementById("indice").style.visibility === "visible") {
        window.document.getElementById("indice").style.visibility = "hidden"
    } else {
        window.document.getElementById("indice").style.visibility = "visible"
    }
}

function click_aside() {
    window.document.getElementById("indice").onclick = function () {
        window.document.getElementById("indice").style.visibility = "hidden"
    }
}


window.onload = function () {
    // crear_toc();
    cambiarTitulo();
    click_aside();
    generarTOC(document.getElementById('indice'));

    // ocultar();
};
