import type { Config } from '~/components/pub/my-ui/data-table'
import type { Patient } from '~/models/patient'
import { defineAsyncComponent } from 'vue'
import { educationCodes, genderCodes } from '~/lib/constants'
import { calculateAge } from '~/lib/utils'

const action = defineAsyncComponent(() => import('~/components/pub/my-ui/data/dropdown-action-dud.vue'))

export const config: Config = {
  cols: [{}, {}, {}, {}, {}, {}, {}, { width: 5 }],

  headers: [
    [
      { label: 'No. RM' },
      { label: 'Nama' },
      { label: 'No. KTP/SIM/Passpor' },
      { label: 'Tgl Lahir' },
      { label: 'Umur' },
      { label: 'Kelamin' },
      { label: 'Pendidikan' },
      { label: '' },
    ],
  ],

  keys: ['number', 'person.name', 'identity_number', 'birth_date', 'patient_age', 'gender', 'education', 'action'],

  delKeyNames: [
    { key: 'code', label: 'Kode' },
    { key: 'name', label: 'Nama' },
  ],

  parses: {
    patientId: (rec: unknown): unknown => {
      const patient = rec as Patient
      return patient.number
    },
    identity_number: (rec: unknown): unknown => {
      const { person } = rec as Patient

      if (person.nationality == 'WNA') {
        return person.passportNumber
      }

      return person.residentIdentityNumber || '-'
    },
    birth_date: (rec: unknown): unknown => {
      const { person } = rec as Patient

      if (typeof person.birthDate == 'object' && person.birthDate) {
        return (person.birthDate as Date).toLocaleDateString('id-ID')
      } else if (typeof person.birthDate == 'string') {
        return (person.birthDate as string).substring(0, 10)
      }
      return person.birthDate
    },
    patient_age: (rec: unknown): unknown => {
      const { person } = rec as Patient
      return calculateAge(person.birthDate)
    },
    gender: (rec: unknown): unknown => {
      const { person } = rec as Patient

      if (typeof person.gender_code == 'number' && person.gender_code >= 0) {
        return person.gender_code
      } else if (typeof person.gender_code === 'string' && person.gender_code) {
        return genderCodes[person.gender_code] || '-'
      }
      return '-'
    },
    education: (rec: unknown): unknown => {
      const { person } = rec as Patient
      if (typeof person.education_code == 'number' && person.education_code >= 0) {
        return person.education_code
      } else if (typeof person.education_code === 'string' && person.education_code) {
        return educationCodes[person.education_code] || '-'
      }
      return '-'
    },
  },

  components: {
    action(rec, idx) {
      return {
        idx,
        rec: rec as object,
        component: action,
      }
    },
  },

  htmls: {
    patient_address(_rec) {
      return '-'
    },
  },
}
