import { getValueFormat } from '@baurine/grafana-value-formats'
import { Typography } from '@tidbcloud/uikit'

import { TiDBComputedProps } from 'dbaas/screens/NextGen/shared/model/shared-tidb-state'

export function formatValue<T>(row: T, key: keyof T, unit: string): JSX.Element {
  const formatFn = getValueFormat(unit)
  const val = row[key]
  if (typeof val !== 'number') {
    return <></>
  }

  const fmtVal = unit === 'short' ? formatFn(val, 0, 1) : formatFn(val, 1)
  return (
    <Typography w={80} variant="body-lg">
      {fmtVal}
    </Typography>
  )
}

export const isNextGenCluster = (cfg: TiDBComputedProps) => {
  return cfg.isStarter || cfg.isEssential || cfg.isPremium
}
