import { differenceInDays, differenceInMonths, differenceInYears, parseISO } from 'date-fns' import { type Base, genBase } from './_base' import type { PersonAddress } from './person-address' import type { PersonContact } from './person-contact' import type { PersonRelative } from './person-relative' import type { Ethnic } from './ethnic' import type { Language } from './language' import type { Regency } from './regency' export interface Person extends Base { name: string // alias?: string frontTitle?: string | null endTitle?: string | null birthDate?: string birthRegency_code?: string | null gender_code?: string | null residentIdentityNumber?: string | null passportNumber?: string | null drivingLicenseNumber?: string | null religion_code?: string | null education_code?: string | null occupation_code?: string | null occupation_name?: string | null ethnic_code?: string | null language_code?: string | null nationality?: string | null communicationIssueStatus?: boolean | string disability?: string | null residentIdentityFileUrl?: string | null passportFileUrl?: string | null drivingLicenseFileUrl?: string | null familyIdentityFileUrl?: string | null maritalStatus_code?: string // preload data for detail patient birthRegency?: Regency | null addresses?: PersonAddress[] | null contacts?: PersonContact[] | null relatives?: PersonRelative[] | null ethnic?: Ethnic | null language?: Language | null } export function genPerson(): Person { return { ...genBase(), frontTitle: '', name: '', endTitle: '', } } export function parseName(person: Person): string { if (!person) return '' const fullName = [person.frontTitle, person.name, person.endTitle].filter(Boolean).join(' ').trim() return fullName } export function calculateAge(birthDate: string | Date | undefined): string { if (!birthDate) { return 'Masukkan tanggal lahir' } try { let dateObj: Date if (typeof birthDate === 'string') { dateObj = parseISO(birthDate) } else { dateObj = birthDate } const today = new Date() // Calculate years, months, and days const totalYears = differenceInYears(today, dateObj) // Calculate remaining months after years const yearsPassed = new Date(dateObj) yearsPassed.setFullYear(yearsPassed.getFullYear() + totalYears) const remainingMonths = differenceInMonths(today, yearsPassed) // Calculate remaining days after years and months const monthsPassed = new Date(yearsPassed) monthsPassed.setMonth(monthsPassed.getMonth() + remainingMonths) const remainingDays = differenceInDays(today, monthsPassed) // Format the result const parts = [] if (totalYears > 0) parts.push(`${totalYears} Tahun`) if (remainingMonths > 0) parts.push(`${remainingMonths} Bulan`) if (remainingDays > 0) parts.push(`${remainingDays} Hari`) return parts.length > 0 ? parts.join(' ') : '0 Hari' } catch { return 'Masukkan tanggal lahir' } }