'use client'

import { useCustomQuery } from '@/hooks/api'
import { request } from '@/utils'
import type { IActiveTable } from '@/types/data'
import type { IBaseResponse } from '@/@core/types/api'

export const AVAILABLE_TABLES_QUERY_KEY = 'available-tables'

export type AvailableTablesQuery = {
  entity_model_id?: string | number
}

export const useGetAvailableTables = (query?: AvailableTablesQuery) => {
  return useCustomQuery<IBaseResponse<IActiveTable[]>>(
    [AVAILABLE_TABLES_QUERY_KEY, query],
    () => {
      const params: Record<string, string> = {}
      if (query?.entity_model_id) {
        params.entity_model_id = String(query.entity_model_id)
      }

      return request({
        url: '/entity/active-tables',
        params,
        withToken: false
      })
    },
    {
      staleTime: 1000 * 60 * 5
    }
  )
}
