import { api } from './client'

export function fetchActiveEncounters(department) {
  const params = new URLSearchParams({ status: 'ACTIVE' })
  if (department) params.set('department', department)
  return api.get(`/api/v1/encounters?${params}`)
}

export function fetchEncounter(id) {
  return api.get(`/api/v1/encounters/${id}`)
}

export async function fetchObservations(encounterId, { limit = 50 } = {}) {
  const all = []
  let cursor = null
  do {
    const params = new URLSearchParams({ limit: String(limit) })
    if (cursor) params.set('cursor', cursor)
    const page = await api.get(`/api/v1/encounters/${encounterId}/observations?${params}`)
    all.push(...(page.items ?? []))
    cursor = page.hasMore ? page.nextCursor : null
  } while (cursor)
  return all
}