export type AddressLabel = 'HOME' | 'WORK' | 'OTHER';

export const ADDRESS_LABELS: Record<AddressLabel, { title: string; shortTitle: string }> = {
  HOME: { title: 'Дом', shortTitle: 'Дом' },
  WORK: { title: 'Работа', shortTitle: 'Работа' },
  OTHER: { title: 'Другой адрес', shortTitle: 'Другие' }
};

export function formatAddressLine(address: {
  fullAddress?: string | null;
  city?: string;
  street?: string;
  house?: string;
  apartment?: string | null;
}) {
  if (address.fullAddress?.trim()) return address.fullAddress.trim();
  const parts = [`г. ${address.city}`, `${address.street}, ${address.house}`];
  if (address.apartment?.trim()) parts.push(`кв. ${address.apartment.trim()}`);
  return parts.filter(Boolean).join(', ');
}

export function getPrimaryAddress(addresses: { label: string }[], label: AddressLabel) {
  return addresses.find((item) => item.label === label) ?? null;
}

export function getOtherAddresses<T extends { label: string }>(addresses: T[]) {
  return addresses.filter((item) => item.label === 'OTHER');
}
