import { Row } from '@tanstack/react-table'

import AmountAndValue from 'components/common/AmountAndValue'
import { BN_ZERO } from 'constants/math'
import useMarketEnabledAssets from 'hooks/assets/useMarketEnabledAssets'
import { byDenom } from 'utils/array'

export const DEBT_META = {
  accessorKey: 'debt',
  header: 'Debt',
}

export const debtSortingFn = (
  a: Row<BorrowMarketTableData>,
  b: Row<BorrowMarketTableData>,
): number => {
  const assetA = a.original.asset
  const assetB = b.original.asset
  if (!a.original.accountDebt || !b.original.accountDebt) return 0
  const debtA = a.original.accountDebt.shiftedBy(-assetA.decimals)
  const debtB = b.original.accountDebt.shiftedBy(-assetB.decimals)
  return debtA.minus(debtB).toNumber()
}

interface Props {
  data: BorrowMarketTableData
}

export default function Debt(props: Props) {
  const marketAssets = useMarketEnabledAssets()
  const asset = marketAssets.find(byDenom(props.data.asset.denom))

  if (!asset) return null

  return <AmountAndValue asset={asset} amount={props.data?.accountDebt ?? BN_ZERO} />
}
