/**
 * @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 { T_el, T_obj } from "../../../types/ax";
import contains from "./contains";
import getEl from "./getEl";
import unique from "./unique";
const elsSort = (data: T_el[], reverse: boolean = false) => {
    let tmp = data.map((k: any) => getEl(k)).filter(Boolean),
        els = unique(tmp),
        result: T_el[] = [];
    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;
