/*
 * This scrupt serves to automatically add additional styling to the CLI options
 * section in the documentation.
 *
 * It will add some sub-headers to when you "expand" the CLI syntax options. It
 * will also stylize the CLI options, that may be listed under the CLI syntax
 * dropdown, as a table.
 */
function do_handle_div_dl_dd_dl_element(dl_element)
{
        const header_a4 = document.createElement("a4");
        const header_div = document.createElement("div");
        const header_span1 = document.createElement("span");
        const header_span2 = document.createElement("span");

        header_a4.classList.add("param-option-head");
        header_a4.innerHTML = 'Options';

        header_span1.innerHTML = 'Option'
        header_span2.innerHTML = 'Description'

        header_div.classList.add("param-option-head");
        header_div.append(header_span1)
        header_div.append(header_span2)

        dl_element.insertAdjacentElement("beforebegin", header_a4);
        dl_element.insertAdjacentElement("beforebegin", header_div);

        //Hack to change dt text content to code style
        var dt_elements = dl_element.querySelectorAll('dt');
        var j;
        for (j = 0; j < dt_elements.length; j++) {
                dt_element = dt_elements[j];

                var dt_code = dt_element.querySelector('code');
                if (!dt_code) {
                        dt_code = document.createElement('code');
                        dt_code.innerHTML = dt_element.innerHTML;
                        dt_element.innerHTML = '';
                        dt_element.insertAdjacentElement("afterbegin", dt_code);
                }
        }
}

function do_handle_div_dl_dd_dl_elements(dd_element)
{
        var param_list = dd_element.querySelectorAll('dl');
        var i;

        for (i = 0; i < param_list.length; i++) {
                var dl_element = param_list[i];

                do_handle_div_dl_dd_dl_element(dl_element)
        }
}

function do_handle_div_dl_dd_element(dd_element)
{
        const a4_description = document.createElement("a4");

        /* Add description header text */
        a4_description.classList.add("param-option-head");
        a4_description.innerHTML = 'Description';
        dd_element.insertAdjacentElement("afterbegin", a4_description);

        /* Add header text and create a table view for the syntax options */
        do_handle_div_dl_dd_dl_elements(dd_element);
}

function do_handle_div_dl_dd_elements()
{
        var param_list = document.querySelectorAll('div > dl > dd.hideable-param');
        var i;

        for (i = 0; i < param_list.length; i++) {
                var dd_element = param_list[i];

                do_handle_div_dl_dd_element(dd_element);
        }
}

do_handle_div_dl_dd_elements();
