/**
 * @since Last modified: 2024-09-10 11:20:41
 * @function getSlidersData
 * @description Get data in synchronization or asynchronous manner.
 * @see {@link https://codepen.io/axui/pen/VwJMEmM|demo @ codepen}
 * @example
 * ax.getSlidersData({content:'#demo'});
 */
'use strict';
import Spy from '../modules/Spy';
import createEl from './createEl';
import getContent from './getContent';
import getDataType from './getDataType';
import getEl from './getEl';
import getStrFromTpl from './getStrFromTpl';
import isEmpty from './isEmpty';
import isNull from './isNull';
const getSlidersData = async function (opts) {
    let wrapEl = getEl(opts.wrap), parentEl = getEl(opts.parent), grossData = [];
    if (!isEmpty(opts.content)) {
        await getContent.call(this, {
            content: opts.content,
            contType: opts.contType,
            contData: opts.contData,
            hyphen: opts.hyphen,
            ajax: {
                xhrName: 'contXhr',
                spinSel: parentEl,
                ...(opts.ajax || {})
            },
            cb: (data) => {
                grossData = data;
            }
        });
    }
    else if (wrapEl && wrapEl.innerHTML.trim()) {
        grossData = [...wrapEl.children];
    }
    else {
        grossData = [];
    }
    let fillSlider = async (node, data) => {
        let childType = getDataType(data);
        if (childType === 'String' || childType === 'Number') {
            /*  result = grossData.map((k: any) => {
                 return { content: k, source: k }
             }); */
            node.innerHTML = data;
        }
        else if (childType === 'Object') {
            if (data.hasOwnProperty('content')) {
                await getContent.call(this, {
                    content: data.content,
                    contType: data.contType,
                    contData: data.contData,
                    hyphen: data.hyphen || opts.hyphen,
                    ajax: {
                        xhrName: 'contXhr',
                        spinSel: node,
                        ...(data.ajax || {})
                    },
                    cb: (resp) => {
                        let respType = getDataType(resp);
                        if (['String', 'Number'].includes(respType)) {
                            node.innerHTML = resp;
                        }
                        else if (respType.includes('HTML')) {
                            node.appendChild(resp);
                        }
                        else if (['Object', 'Array'].includes(respType)) {
                            node.innerHTML = getStrFromTpl.call(this, resp, data.tplStr, data.tplEng);
                        }
                    }
                });
            }
            else {
                node.innerHTML = getStrFromTpl.call(this, data);
            }
        }
        else if (childType === 'Array') {
            node.innerHTML = getStrFromTpl.call(this, data);
        }
    }, dataType = getDataType(grossData), result = [];
    if (dataType.includes('HTML')) {
        result = [{ source: grossData, slider: grossData }];
    }
    else if (dataType === 'String') {
        let tmp = createEl('div', '', grossData), els = [...(tmp.querySelector('ul,ol') || tmp).children];
        result = els.map((k) => {
            return { source: k, slider: k };
        });
    }
    else if (dataType === 'Array' || dataType === 'Object') {
        let tmp = (dataType === 'Object') ? [grossData] : grossData.filter((k) => !isNull(k));
        if (tmp.length === 0)
            return;
        result = tmp.map((k) => {
            let childType = getDataType(k), slider;
            if (childType.includes('HTML')) {
                slider = k;
            }
            else {
                slider = createEl('li');
                if (opts.lazy) {
                    k.spy = new Spy(slider, {
                        root: parentEl || wrapEl,
                        repeat: false,
                        onIn: () => {
                            fillSlider(slider, k);
                        }
                    });
                }
                else {
                    fillSlider(slider, k);
                }
            }
            return { source: k, slider };
        });
    }
    opts.cb && opts.cb(result);
    return result;
};
export default getSlidersData;
