import isNumber from 'lodash/isNumber'
import find from 'lodash/find'
import round from 'lodash/round'

import type { Param } from 'requests'

import { useMatchPageStore } from 'features/MatchPage/store'

export const useTeamsStatsTable = () => {
  const { profile, teamsStats } = useMatchPageStore()

  const getDisplayedValue = (val: any) => (
    isNumber(val) ? round(val, 2) : '-'
  )

  const getStatItemById = (paramId: number) => {
    if (!profile) return null

    return find(teamsStats[profile?.team2.id], ({ param1 }) => param1.id === paramId) || null
  }

  const isClickable = (param: Param) => (
    Boolean(param.val) && param.clickable
  )

  return {
    getDisplayedValue,
    getStatItemById,
    isClickable,
  }
}
