/**
 * @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 { T_ajax } from '../../../types/ajax';
import { T_el, T_fn, T_null, T_obj, T_sel } from '../../../types/ax';
import { T_content_type, T_getcontent } from '../../../types/utils';
import ax from '../ax';
import config from '../global/config';
import prefix from '../global/prefix';
import alias from '../global/alias';
import Spy from '../modules/Spy';
import ajax from './ajax';
import createEl from './createEl';
import deepClone from './deepClone';
import getContent from './getContent';
import getDataType from './getDataType';
import getEl from './getEl';
import getStrFromTpl from './getStrFromTpl';
import isEmpty from './isEmpty';
import isNull from './isNull';
import renderTpl from './renderTpl';
import treeTools from './treeTools';
import valToArr from './valToArr';
type T_getSlidersData = {
    content: any,
    contType?: T_content_type,
    contData?: T_obj,
    ajax?: T_ajax,
    hyphen?: string,
    parent?: T_sel,
    wrap?: T_sel,
    tplStr?: string,
    tplEng?: T_fn,
    lazy?: boolean,
    cb?: T_fn,
}
const getSlidersData = async function (this: any, opts: T_getSlidersData) {
    let wrapEl = getEl(opts.wrap),
        parentEl = getEl(opts.parent),
        grossData: any = [];
    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: any) => {
                grossData = data;
            }
        });
    } else if (wrapEl && wrapEl.innerHTML.trim()) {
        grossData = [...wrapEl.children];
    } else {
        grossData = [];
    }
    let fillSlider = async (node: T_el, data: T_obj) => {
        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: any) => {
                        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: T_obj[] = [];
    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: any) => {
            return { source: k, slider: k };
        });
    } else if (dataType === 'Array' || dataType === 'Object') {
        let tmp = (dataType === 'Object') ? [grossData] : grossData.filter((k: any) => !isNull(k));
        if (tmp.length === 0) return;
        result = tmp.map((k: any) => {
            let childType = getDataType(k),
                slider:T_el;
            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;
