import { getAllDataSources,addDataSource,deleteDataSource ,getChartData} from "@/api/dataSource";
const state={
    allDataSources:[]
}
const mutations ={
    SET_ALL_DATA_SOURCES:(state,allDataSources)=>{
        state.allDataSources=allDataSources
      },
}
const actions={
    getAllDataSources({commit}){
        console.log('start to get all  data SOURCES')
        return new Promise((resolve,reject)=>{
            getAllDataSources().then(res=>{
                console.log('all data source res',res)
                commit('SET_ALL_DATA_SOURCES',res.data.data)
                console.log('state.dataSources',state.allDataSources)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    getChartData({commit},data){
        // console.log('start to get ChartData in store',data)
   // const aaa = JSON.stringify(data.sources)
        return new Promise((resolve,reject)=>{
            getChartData(data).then(res=>{
                // console.log('get ChartData res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })

        })
    },
    addDataSource({commit},data){
        console.log('start to add   data source',data)
        return new Promise((resolve,reject)=>{
           addDataSource(data).then(res=>{
                console.log('add datasource res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    deleteDataSource({commit},data){
        console.log('start to delete  data source',data)
        return new Promise((resolve,reject)=>{
           deleteDataSource(data).then(res=>{
                console.log('delete datasource res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
  }