export default {
    /**
     * 从一个个书中获取属性
     */
    getAttr(doc,str){
        let selector = "";
        top.testEle = doc;
        let sq = str?.split("@");// 必然分出前面tag. 或class. 或id. 最后是 某个属性或者text html
        for (let index = 0; index < sq.length-1; index++) {
            let regex = /^(\w+)\.([\w\s-]+)(?:\.(\d+))?$/ 
            let parm = sq[index].match(regex);
            if(parm[1]=="tag"){
                selector = selector + parm[2];
                if(parm[3]) {
                    selector = selector + `:eq(${parm[3]})`
                }
            }
            if(parm[1]=="class"){
                selector = selector + `[class*='${parm[2]}']`
                if(parm[3]) {
                    selector = selector + `:eq(${parm[3]})`
                }
            }
            if(parm[2]=="id"){
                selector = selector + `[id*='${parm[2]}']`
            }
            selector = selector + " ";
        }
        let res = undefined;
        if(sq[sq.length-1]){
            if(sq[sq.length-1]=="text"){
                let result =  $(doc).find(selector);
                if(result.length>1) {
                    res = result.map(function(index, item) {
                        return $(item)?.text()
                    });
                } else {
                    res = $(doc).find(selector)?.text()
                }
            } else {
                let result =  $(doc).find(selector);
                if(result.length>1) {
                    res = result.map(function(index, item) {
                        return $(item)?.attr(sq[sq.length-1])
                    });
                } else {
                    res = $(doc).find(selector)?.attr(sq[sq.length-1])
                }
            }
        }
        return res;
    },
    getBookPreview(doc,str){
        return {
            "bookItem": doc,
            "bookUrl": this.getAttr(doc,str.bookUrl),
            "realUrl": this.getAttr(doc,str.realUrl),
            "coverUrl": this.getAttr(doc,str.coverUrl),
            "kind": this.getAttr(doc,str.kind),
            "name": this.getAttr(doc,str.name)
        }
    },
    getBookContent(doc,str){
        return {
            "content":this.selector(doc,str),
        }
    },
    getBookInfo(doc,str){
        return {
            "bookItem": doc,
            "coverUrl": this.getAttr(doc,str.coverUrl),
            // "init": "",
            "intro": this.getAttr(doc,str.intro),
            "kind": this.getAttr(doc,str.kind),
            "name": this.getAttr(doc,str.name),
        }
    },
    selector(doc,str){
        let selector = "";
        let sq = str.split("@");// 必然分出前面tag. 或class. 或id. 最后是 某个属性或者text html
        for (let index = 0; index < sq.length; index++) {
            let regex = /^(\w+)\.([\w\s-]+)(?:\.(\d+))?$/ 
            let parm = sq[index].match(regex);
            if(parm[1]=="tag"){
                selector = selector + parm[2];
                if(parm[3]) {
                    selector = selector + `:eq(${parm[3]})`
                }
            }
            if(parm[1]=="class"){
                selector = selector + `[class*='${parm[2]}']`
                if(parm[3]) {
                    selector = selector + `:eq(${parm[3]})`
                }
            }
            if(parm[2]=="id"){
                selector = selector + `[id*='${parm[2]}']`
            }
            selector = selector + " ";
        }
        console.log("selector,",doc.find(selector));
        return doc.find(selector);
    },
    //返回搜索页面
    ruleSearch(source,key){
        return fetch('/api/scrape', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ url: source.searchUrl.replace("{{key}}",key)??this.$route.query.url })
        })
        .then(response => response.text())
        .then(html => {
            const doc = $(html);
            return doc;
        })
        .catch(error => console.error(error));
    },
    ruleBookInfo(source,key){
        return fetch('/api/scrape', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ url: source.bookSourceUrl + key ?? this.$route.query.url })
        })
        .then(response => response.text())
        .then(html => {
            const doc = $(html);
            return doc;
        })
        .catch(error => console.error(error));
    },
    ruleContent(source,key){
        return fetch('/api/scrape', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ url: source.bookSourceUrl + key ?? this.$route.query.url })
        })
        .then(response => response.text())
        .then(html => {
            const doc = $(html);
            return doc;
        })
        .catch(error => console.error(error));
    }
}