/**
 * @since Last modified: 2024-09-11 11:42:41
 * @function createEl
 * @description Create a node or a group of father-son nodes.
 * @param {string} name="div"  - Node name, requierd.
 * @param {object} [attrs]  - Node attributes.Format : {class:'demo',style:'width:100px;height:50px;',disabled:''}
 * @param {string|number|array} [content]  - Node content.If it is an array,create a sub-node of the cycle.
 * @returns {HTMLElement|null} Return a node or null
 * @see {@link https://codepen.io/axui/pen/LYqzJyd|demo @ codepen}
 * @example
 * ax.createEl('div',{class:'demo'},'I love you');
 * <!--return a HTMLElement node-->
 */
'use strict';
import getDataType from './getDataType';
import getEl from './getEl';
const createEl = (name, attrs, content) => {
    name = name || 'div';
    let rootName = name.toUpperCase().trim(), rootEl = document.createElement(rootName), attrsType = getDataType(attrs), loop = (host, data) => {
        if (data === '' || data === null || data === undefined) {
            return false;
        }
        let dataType = getDataType(data);
        if (rootName === 'TEMPLATE') {
            host.innerHTML = data.toString();
        }
        else {
            if (dataType === 'Array' && data.length > 0) {
                data.forEach((i) => {
                    let child = createEl(i.name, i.attrs, i.content);
                    child && host.appendChild(child);
                });
            }
            else if (dataType.includes('HTML')) {
                host.appendChild(data);
            }
            else if (dataType === 'String' && data.trim().startsWith('#') && data.trim().length > 1) {
                let el = getEl(data);
                if (!el)
                    return;
                el.nodeName === 'TEMPLATE' ? host.appendChild(el.content.cloneNode(true)) : host.insertAdjacentHTML('beforeEnd', el.innerHTML);
            }
            else {
                host.insertAdjacentHTML('beforeEnd', data);
            }
        }
    };
    if (attrs && attrsType === 'Object') {
        for (let k in attrs) {
            attrs.hasOwnProperty(k) && rootEl.setAttribute(k, attrs[k]);
        }
    }
    loop(rootEl, content);
    return rootEl;
};
export default createEl;
