// Services
import { getDetail } from '~/services/encounter.service'

// Handlers
import { mapResponseToEncounter } from '~/handlers/encounter-init.handler'

export async function getEncounterData(id: string | number) {
  let data = null
  try {
    const dataRes = await getDetail(id, {
      includes:
        'patient,patient-person,patient-person-addresses,unit,' +
        'Adm_Employee,Adm_Employee,Adm_Employee-Person,' +
        'Responsible_Nurse,Responsible_Nurse-Employee,Responsible_Nurse-Employee-Person,' +
        'Appointment_Doctor,Appointment_Doctor-employee,Appointment_Doctor-employee-person,' +
        'Responsible_Doctor,Responsible_Doctor-employee,Responsible_Doctor-employee-person',
    })
    const dataResBody = dataRes.body ?? null
    const result = dataResBody?.data ?? null

    if (result) {
      const mappedData = mapResponseToEncounter(result)
      if (mappedData) {
        data = mappedData
      } else {
        data = null
      }
    } else {
      data = null
    }
  } catch (error) {
    console.error('Error fetching encounter data:', error)
    data = null
  }
  return data
}
