'use client'

import { IBaseResponse, CustomUseQueryOptions } from '@/@core/types/api'
import { useCustomQuery } from '@/hooks/api'
import { ITableStatus } from '@/types/data/table'
import { request } from '@/utils'

export const TABLE_STATUS_QUERY_KEY = 'table-status'

export const useGetTableStatus = (
  tableId?: string,
  options?: CustomUseQueryOptions<IBaseResponse<ITableStatus>>
) => {
  const queryKey = [TABLE_STATUS_QUERY_KEY, tableId]
  const enabledOption = options?.enabled ?? true

  const tableStatus = useCustomQuery<IBaseResponse<ITableStatus>>(
    queryKey,
    () =>
      request<IBaseResponse<ITableStatus>>({
        url: `/tables/${tableId}/current-status`
      }),
    {
      ...options,
      enabled: Boolean(tableId) && enabledOption
    }
  )

  return tableStatus
}
