/**
 * @since Last modified: 2024-08-04 17:39:15
 * @function elsSort
 * @description Sort the array of nodes by parent-child relationship.
 * @param  {element[]} data - Element array.
 * @param  {boolean} [childFirst=true] - is child first.
 * @see {@link https://codepen.io/axui/pen/zYVwPbx|demo @ codepen}
 * @example
 * ax.elsSort([el1,el2,el3]);
 * <!--return []-->
 */
'use strict';
import getEl from "./getEl";
import unique from "./unique";
const elsSort = (data, reverse = false) => {
    let tmp = data.map((k) => getEl(k)).filter(Boolean), els = unique(tmp), result = [];
    let topo = new Map();
    for (let k of els) {
        topo.set(k, []);
    }
    for (let k of els) {
        for (let i of els) {
            if (k !== i && k.contains(i)) {
                topo.get(k).push(i);
            }
        }
    }
    for (let k of topo) {
        k[1].length > 0 && result.push(...k[1]);
        result.push(k[0]);
    }
    return reverse ? result.reverse() : result;
};
export default elsSort;
