import { marked } from 'marked'
import * as echarts from 'echarts';
import { ResultData } from '@/types/chatConversation';
//渲染markdown
export const renderMarkDown = (text:string)=>{
    let html = undefined;
    try{
        html = marked.parse(text);
    }catch(e){
        return text;
    }
    
    return html;
}
//渲染echarts图表
export const renderEcharts = (optionJson:string)=>{
    let option = undefined;
    try{
        option = JSON.parse(optionJson);
    }catch(e){
        return optionJson;
    }
    
    if(option.option){
        //防止套了两层
        option = option.option;
    }
    let div = document.createElement('div');
    let echart = echarts.init(div);
    echart.dispose();
    echart.setOption(option);
    //获取html代码
    let html = div;
    return html;
}
//渲染
export const render = (result:ResultData)=>{
    let html = result.conclusion;
    html += '<br>'
    if(result.status === 1){
        //表格
        let table = renderMarkDown(result.data);
        html += table;
    }else{
        let charts = renderEcharts(result.data);
        html += charts;
    }
    return html;
}