import {GetCpuInfo, GetCpuUsage, GetNetworkCounter, GetVirtualMemory} from "frontend/wailsjs/go/system/InfoUtils";
import {computed, reactive, ref, watch} from "vue";
import {asyncComputed} from "@vueuse/core";
import {bytesToSizeWithUnit} from "src/utils/file/file";
import {cpu as CPU} from "frontend/wailsjs/go/models";
import InfoStat = CPU.InfoStat;

let cpu = reactive({
    info: <InfoStat>{
        modelName: ""
    },
    usage: {
        data: <number[]>[],
        interval: <NodeJS.Timeout | undefined>undefined
    },
})

export function useCpuUsage(){
    clearInterval(cpu.usage.interval)

    function loop(){
        GetCpuUsage().then(result=>cpu.usage.data = result)
        cpu.usage.interval = setTimeout(()=>{
            loop()
        },1000)
    }

    loop()
    return computed(()=>cpu.usage.data)
}

export function useCpuInfo(){
    GetCpuInfo().then(result=>{
        Object.assign(cpu.info,result[0] || {})
    })

    return cpu.info
}
