import useSWR from 'swr'
import { fetcher } from '../../http/axiosInstance'
import { BASE_URL } from '../../constants'
import { Text } from '@mantine/core'
import TableValue from './TableValue'

interface ITCBParameterProps {
    value: string,
    vtable: string,
    inactive?: boolean,
    name: string
}

const TCBParameter = ({
    value,
    vtable,
    name
}: ITCBParameterProps) => {

    //Get value
    const { data: tcbValue } = useSWR(
        `/general/params/tcb?id=${value}&vtable=${vtable}`,
        (url) => fetcher(url, BASE_URL.ems).then(res => res[0]),
        {
            revalidateOnFocus: false,
            revalidateIfStale: false
        }
    )

    const tables = [
        'vStreets',
        'tTypes',
        'vPipesGround',
        'vPipesLayer',
        'vPipesIsolation',
        'vRepairEvent',
        'vPipesMaterial',
        'vBoilers',
        'vHotWaterTypes',
        'vHeatingTypes',
        'vColdWaterTypes',
        'vCanalization',
        'vElectroSupplyTypes',
        'vGasSupplyTypes',
        'vFoundation',
        'vMaterialsWall',
        'vCovering',
        'vRoof',
        'vTechStatus',
        'vPipeOutDiameters',
        'vPipeDiameters',
    ]

    const TCBValue = (vtable: string) => {
        if (tables.includes(vtable)) {
            return (
                <TableValue value={tcbValue?.id} name={name} type='select' vtable={vtable} />
            )
        } else {
            return (
                <Text>
                    {JSON.stringify(name)}
                    {JSON.stringify(tcbValue)}
                </Text>
            )
        }
    }

    return (
        TCBValue(vtable)
    )
}

export default TCBParameter