/**
 * @since Last modified: 2024/04/20 14:39:01
 * @function ulToArr
 * @description Convert the ul+li/ol+li node tree to an array of objects, in the format: [{node:'',label:'',value:''},{node:'',label:'',value:''}...].If you need to bring the id, pId, path and floor properties you need to use the "treeTools.addIdPath" method.
 * @param {HTMLElement} el - Node selector
 * @param {boolean} [replace=true] - Whether to use the label text as the value if there is no value attribute.
 * @returns {object[]}  - Returns the One- or two-dimensional tree array data.
 * @see {@link https://codepen.io/axui/pen/OJGVZOJ|demo @ codepen}
 * @example
 * ax.ulToArr('#id');
 * Node's format:
 * <ul>
 * <li><i>TEXT</i></li>
 * <li><i>TEXT</i>
 *      <ul>
 *          <li><i>TEXT</i></li>
 *          <li><i>TEXT</i></li>
 *      </ul>
 * </li>
 * </ul>
 * ul+li or ol+li are ok!
 */
'use strict';
import getEl from "./getEl";
import treeTools from "./treeTools";
const ulToArr = (el, replace = true) => {
    let elem = getEl(el);
    if (!elem) {
        throw new Error(`No node, no access to data!`);
    }
    let getChild = () => {
        let data = [];
        [...elem.children].forEach(item => {
            data.push(getJson(item));
        });
        return data;
    }, getJson = (elem) => {
        let node = elem.firstElementChild, obj = { node: elem, ...treeTools.getValsFromAttrs(elem) };
        obj.label = node.textContent.trim();
        if (replace) {
            obj.value = node.hasAttribute('value') ? node.getAttribute('value') : obj.label;
        }
        else {
            node.hasAttribute('value') ? obj.value = node.getAttribute('value') : null;
        }
        let parent = elem.querySelector('ul') || elem.querySelector('ol');
        if (parent) {
            obj.children = [];
            [...parent.children].forEach(item => {
                obj.children.push(getJson(item));
            });
        }
        return obj;
    };
    return getChild();
};
export default ulToArr;
