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 = [
        'PipesTypes',
        'vAddRepairEvent',
        'vBoilers',
        'vBoilersAppointment',
        'vBoilersBalance',
        'vBoilersCondition',
        'vBoilersFuels',
        'vBoilersHotWater',
        'vBoilersPerimeter',
        'vBoilersPeriods',
        'vBoilersScheme',
        'vBoilersState',
        'vBoilersTypes',
        'vBuildingManagement',
        'vBuildingOwner',
        'vCanalization',
        'vColdWaterTypes',
        'vConditionEquipment',
        'vCovering',
        'vDryer',
        'vElectroSupplyTypes',
        'vEquipmentsTypes',
        'vFoundation',
        'vFuelsFeed',
        'vGasSupplyTypes',
        'vHeatingTypes',
        'vHeatTransfer',
        'vHotWaterTypes',
        'vMaterialsWall',
        'vNormative',
        'vPipeDiameters',
        'vPipeOutDiameters',
        'vPipesBearingType',
        'vPipesCovering',
        'vPipesGround',
        'vPipesIsolation',
        'vPipesLayer',
        'vPipesMaterial',
        'vRepairEvent',
        'vRoof',
        'vRPSType',
        'vStreets',
        'vTechStatus',
        'vTrash',
        'vVentilation',
        'vWallingEquipment',
        'tTypes',
    ]

    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