/**
 * @since Last modified: 2024/05/16 10:53:00
 * @function getEls
 * @description Get the node array.
 * @param {*} data  - It can be a single node selector or a node array.
 * @param {HTMLElement} parent  - Parent node, get sub-node from this node, default document.body
 * @returns {array} Return a node array.
 * @see {@link https://codepen.io/axui/pen/JjxNQEO|demo @ codepen}
 * @example
 * ax.getEls('#demo');
 * <!--return [node]-->
 * ax.getEls('#demo1,#demo2,#demo3');
 * <!--return [node1,node2,node3]-->
 * ax.getEls(['#demo1','#demo2,'#demo3']);
 * <!--return [node1,node2,node3]-->
 */




'use strict';
import { T_el, T_sel } from '../../../types/ax';
import getDataType from './getDataType';
import getEl from './getEl';
import isEmpty from './isEmpty';
const getEls = (data: T_sel | T_sel[], parent?: T_sel): T_el[] => {
    let type = getDataType(data),
        parentEl = getEl(parent) || document,
        result: any[] = [];
        
    if (isEmpty(data)) { return result; }
    if (type.includes('HTML')) {
        
        result.push(data);
    } else if (type === 'String') {
        data = (data as string).trim();
        
        result = data.split(',').map((k: any) => {
            return [...(parentEl as T_el).querySelectorAll(k)];
        }).flat();
    } else if (type === 'Array') {
        result = (data as T_sel[]).map((k: any) => {
            return getEl(k, parent);
        });
    }
    return result.filter(Boolean);
}
export default getEls;
