import color from 'ansis'
import { get, post } from '../http'

export async function perfToggle(onOff?: string) {
  if (onOff && !['on', 'off', 'status'].includes(onOff)) {
    console.error('Usage: toes perf [on|off|status]')
    process.exit(1)
  }
  const body = onOff && onOff !== 'status' ? { on: onOff === 'on' } : {}
  const res = onOff === 'status'
    ? await get<{ perfTiming: boolean }>('/api/system/perf')
    : await post<{ perfTiming: boolean }>('/api/system/perf', body)
  if (res) {
    const label = res.perfTiming ? color.green('on') : color.red('off')
    console.log(`Perf timing ${onOff === 'status' ? 'is ' : ''}${label}`)
  }
}
