const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
const tools = require('../../../tools');
const helper = require('../../helper');
const CMD = {}
CMD.search_id = async function (bookId) {
    const url = 'https://new-media-fx.qimao.com/api/content/book/list';
    
    // 请求参数
    const params = new URLSearchParams({
        book_id: bookId,
        page: '1',
        page_size: '50',
        env: '',
        t: Date.now()  // 当前时间戳
    });

    // 请求头
    var headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'app-external-id': '4c6e8d8709c14de9a4397b6cbb978f85',
        'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0MDg4MjI1LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0MTc0NjI1LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQwODgyMjUsIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.LaEHd147NoCt1Wfo9ZZlCs4pludA7D3LajN4xff2H5o',
        'project': 'reader_paid_dyminiapp',
        'signature': 'b4782a395b0d0de3f78bb5c8157cb35ab0d6c51f3677a1cfb4b5390c39f63169',
        'target-id': '544180064332673816',
        'cookie': 'sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; sajssdk_2015_new_user_new-media-fx_qimao_com=1; acw_tc=1a0c380f17341043811111513e003f4b4a7b61bafaac1abb4e5d21e565003f; sa_jssdk_2015_new-media-fx_qimao_com=%7B%22distinct_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzYmRjMTI0NjQ1ZTItMGNiZWFlZTE3MmQ0Y2U4LTRjNjU3YjU4LTkyMTYwMC0xOTNiZGMxMjQ2NTU2NyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D',
        'Referer': 'https://new-media-fx.qimao.com/backend/postback-configuration/index?projectId=reader_paid_dyminiapp&appId=4c6e8d8709c14de9a4397b6cbb978f85&accountId=544180064332673816'
    };

    headers['authorization'] = await tools.getQMToken()
    try {
        const response = await fetch(`${url}?${params}`, {
            method: 'GET',
            headers: headers
        });

        const data = await response.json();
        if(data.code!=200){
            throw data
        }
        // console.log('响应状态:', response.status);
        // console.log('响应数据:', data.data.list[0]);
        if(data.data.list.length<=0){
            return null
        }
        let book_info = data.data.list[0]
        let info = {}
        info.words =  Math.floor(parseFloat(book_info.book_word_count)*10000)
        info.book_name =  book_info.book_name
        info.book_id =  book_info.book_id
        info.genre = 999;
        if(book_info.book_type==2){  //七猫只有长或者短
            info.genre = 3;
        }else{
            info.genre = 1;
        }
        return info;
    } catch (error) {
        return null
    }
}

// Node.js 18及以上版本可以直接运行
// Node.js 18以下版本需要先安装 node-fetch:
// npm install node-fetch@2

module.exports = CMD;