/**
 * @since Last modified: 2024-07-11 09:30:49
 * @function setContent
 * @description Fill nodes with data in different formats.
 * @param {object} content  - Insert content with multiple format.
 * @param {getEl} el - The host node of content.
 * @param {'override' | 'afterbegin' | 'beforeend'} [position='override'] - Position of inserting content.
 * @param {function} [engine=renderTpl] - Template engine function.Need if content is in array or object format.
 * @param {getEl | string} [template] - Template text.Need if content is in array or object format.
 * @returns null.
 * @see {@link https://codepen.io/axui/pen/poGqNYp|demo @ codepen}
 * @example
 * ax.setContent({content:'I love you',el:'#demo');
 * <!--return null-->
 */
'use strict';
import getDataType from './getDataType';
import getEl from './getEl';
import isEmpty from './isEmpty';
import renderTpl from './renderTpl';
const setContent = ({ content, target, position = 'override', engine, template, list = {} }) => {
    let contType = getDataType(content), el = getEl(target);
    if (!el || isEmpty(content)) {
        return false;
    }
    if (contType.includes('HTML')) {
        (position === 'override') ? (el.innerHTML = '', el.appendChild(content)) : el.insertAdjacentElement(position, content);
    }
    else if (contType === 'String' || contType === 'Number') {
        (position === 'override') ? el.innerHTML = content : el.insertAdjacentHTML(position, content);
    }
    else if (contType === 'Array' || contType === 'Object') {
        let tplDom = getEl(template), result = '', tplStr = tplDom?.innerHTML || template, tplEng = engine || renderTpl;
        if (tplStr && tplEng) {
            try {
                result = tplEng.name === 'template' ? tplEng(tplStr)(content) : tplEng(tplStr, content);
            }
            catch (err) {
                console.error(err);
            }
            ;
        }
        else {
            console.warn('Be sure to fill in the template content and template engine!');
        }
        (position === 'override') ? el.innerHTML = result : el.insertAdjacentHTML(position, result);
    }
};
export default setContent;
