import { type Base, genBase } from './_base'
import type { AddressLocationTypeCode } from '~/lib/constants'
import type { PostalRegion } from './postal-region'
import { toTitleCase } from '~/lib/utils'
export interface PersonAddress extends Base {
  person_id: number
  locationType_code: AddressLocationTypeCode
  address: string
  rt?: string
  rw?: string
  postalRegion_code?: string
  village_code: string

  // preload
  postalRegion?: PostalRegion | null
  locationType?: AddressLocationTypeCode
}

export function genPersonAddress(): PersonAddress {
  return {
    ...genBase(),
    person_id: 0,
    locationType_code: '',
    address: '',
    village_code: '',
  }
}

export function formatAddress(builder?: PersonAddress) {
  if (!builder) return ''

  const village = builder?.postalRegion?.village
  const district = village?.district
  const regency = district?.regency
  const province = regency?.province

  const parts = [
    builder?.address,
    village?.name,
    district?.name,
    toTitleCase(regency?.name || ''),
    toTitleCase(province?.name || ''),
    builder?.postalRegion_code,
  ].filter(Boolean)

  return parts.join(', ')
}
