import request from '../utils/request';
import constants from '../utils/constants';
const baseURL = sessionStorage.getItem('AI-INTELLIGENT-TOOLS_BASE_URL') || constants.publicPath;
let prefix = sessionStorage.getItem('AI-INTELLIGENT-TOOLS_PRE_FIX') || constants.publicPrefix;
// import cache from '../plugins/cache';
// import { EventSourcePolyfill } from 'event-source-polyfill';

// 0.0.9 版本接口升级
prefix += '/plugin/v2';
export const ssoAuth = (token, businessSource) => {
    return request({
        url: prefix + '/sso/assistant',
        method: 'get',
        params: {
            token: token,
            businessSource
        },
    });
}

export const authUser = (headers) => {
    return request({
        url: '/uc/authuser',
        method: 'get',
        headers: headers,
    });
}

export const criticismApply = (id) => {
    return request({
        url: prefix + `/criticism/${id}/apply`,
        method: 'post',
    });
}

export const generateReview = (data, sseSessionId, courseId) =>{
    return request({
        url: prefix + '/criticism',
        method: 'post',
        data: data,
        // headers: {
        //     'X-Sse-SessionId': sseSessionId
        // },
        params: {
            courseId: courseId
        }
    });
}

export const reanswerReview = (sseSessionId, courseId) =>{
    return request({
        url: prefix + '/criticism/comment',
        method: 'put',
        // headers: {
        //     'X-Sse-SessionId': sseSessionId
        // },
        params: {
            commentId: courseId
        }
    });
}

export const setFeedback = (params, data) => {
    return request({
        url: prefix + `/criticism/feedback`,
        method: 'post',
        data,
        params
    });
}

export const loopPoll = (data) => {
    return request({
        url: prefix + `/criticism/poll`,
        method: 'post',
        data,
    });
}

export const analysisSave = (data) => {
    return request({
        url: prefix + '/criticism/analysis/save',
        method: 'post',
        data,
    });
}

// export const registerSse = (sseSessionId, fn, successFn) => {
//     const source = new EventSourcePolyfill(baseURL + prefix + '/sse?id=' + sseSessionId, {
//         headers: {
//             'X-Id-Token': cache.session.getJSON('SRKJ_TOKEN_CACHE'),
//         },
//         // heartbeatTimeout: 120000,
//         heartbeatTimeout: 8000,
//     });
//     source.onmessage = function (event) {
//         const { data } = event;
//         if (data === '连接成功') {
//             if (successFn) successFn();
//         }
//     };
//     source.addEventListener('CRITICISM_ANSWER', (event) => {
//         const data = event.data;
//         fn(JSON.parse(data))
//     });
//     source.addEventListener('CRITICISM_COMMENT', (event) => {
//         const data = event.data;
//         fn({comments: [JSON.parse(data)]}, 'reanswer');
//     });
//
//     return source;
// }