import { useListChangefeedSpecsX } from 'dbaas/services/dataflow'
import { ConsoleapiChangefeedType } from 'dbaas/services/dataflow/interface'

import { SpecificationProperty } from '../ChangefeedDetail/SpecificationProperty'

const usePremiumSpecProperty = (specId: string, type: ConsoleapiChangefeedType, clusterId: string) => {
  const { data, isLoading } = useListChangefeedSpecsX(
    clusterId ?? '',
    { changefeed_type: type },
    {
      query: {
        enabled: !!clusterId && !!type,
        refetchOnWindowFocus: false,
        refetchOnMount: false
      }
    }
  )

  const spec = data?.items?.find((item) => item.name === specId)
  const specDisplayName = spec?.rcu_num ? `${spec.rcu_num} CCUs` : '-'
  const rps = spec?.rps_limit ?? 0

  return {
    specDisplayName: isLoading ? 'Loading...' : specDisplayName,
    specDescription: `The maximum incremental replication performance is ${rps > 0 ? rps : '-'} rows/s`
  }
}

export const PremiumSpecificationProperty = ({
  specId,
  type,
  clusterId
}: {
  specId: string
  type: ConsoleapiChangefeedType
  clusterId: string
}) => {
  const { specDisplayName: displayName, specDescription: description } = usePremiumSpecProperty(specId, type, clusterId)
  return <SpecificationProperty displayName={displayName} description={description} />
}
