// 列表接口数据封装
export function listPackage(data, isHome = false) {
    let parser = new DOMParser();
    let doc = parser.parseFromString(data, "text/html");
    let el = doc.querySelectorAll(".section-box");
    let newData = [];
    el.forEach((item) => {
        let titleEl = item.querySelector(
            ".section-header-title"
        );
        let moreEl = item.querySelector(
            ".section-header-more"
        );
        let moduleEl = item.querySelectorAll(".module-item");

        let moduleData = [];
        moduleEl.forEach((items) => {
            if (items.querySelector("a.v-item")) {
                let img = 'https://61.147.93.252:15002' + items.querySelector('img.lazy.lazyload').getAttribute('data-original');
                let url = 'https://www.keke1.app' + items.querySelector('a').getAttribute('href');
                let title = items.querySelector('img.lazy.lazyload').getAttribute('title');
                // let state = (items.querySelector('div.v-item-bottom>span')).innerHTML.trim();
                moduleData.push({
                    title: title,
                    img: img,
                    // state: state,
                    url: url,
                });
            }
        });
        if (moduleData && moduleData.length > 0) {
            if (titleEl) {
                if (isHome) {
                    let titleMap = {
                        "近期热门电影": "热门电影",
                        "近期热门剧集": "热门连续剧",
                        "热播动漫": "热门动漫",
                        "热播综艺纪录": "热门综艺",
                    }
                    if (titleMap[titleEl.innerHTML]) {
                        newData.push({
                            title: titleMap[titleEl.innerHTML],
                            url: moreEl && moreEl.getAttribute('href'),
                            children: moduleData,
                        });
                    }
                } else {
                    newData.push({
                        title: titleEl.innerHTML,
                        url: moreEl && moreEl.getAttribute('href'),
                        children: moduleData,
                    });
                }
            } else {
                newData = moduleData
            }
        }
    });

    return newData;
}

// 搜索接口数据封装
export function searchListPackage(data) {
    let parser = new DOMParser();
    let doc = parser.parseFromString(data, "text/html");
    let el = doc.querySelectorAll(".search-result-item");
    let newData = [];
    el.forEach((item) => {
        let img = 'https://61.147.93.252:15002' + item.querySelector('img.lazy.lazyload').getAttribute('data-original');
        let url = 'https://www.keke1.app' + item.getAttribute('href');
        let searchItemMain = item.querySelector('.search-result-item-main');
        let title = searchItemMain.querySelector('.title').innerHTML.trim();
        let desc = searchItemMain.querySelector('.desc').innerHTML.trim();
        let tag = item.querySelector('.search-result-item-header>div').innerHTML.trim();
        newData.push({
            title: title,
            img: img,
            url: url,
            desc: desc,
            tag: tag,
        });
    });
    console.log(newData, 'newData');
    return newData;
}