import type { MeResponse } from '../api/profileApi'

const capitalize = (s: string | null) =>
  s ? s[0].toUpperCase() + s.slice(1).toLowerCase() : ''

// True for any non-individual account (legal entity). Organization data then
// lives in the nested `legal_entity` object.
export function isLegalEntity(data: MeResponse): boolean {
  return data.account_type !== 'individual'
}

// "Фамилия Имя Отчество" with each part capitalized; empty parts are dropped.
export function getFullName(data: MeResponse): string {
  return [data.last_name, data.first_name, data.middle_name]
    .filter(Boolean)
    .map(capitalize)
    .join(' ')
}

// Name shown in the profile header: organization name for legal accounts,
// full person name otherwise.
export function getDisplayName(data: MeResponse): string {
  return isLegalEntity(data) ? (data.legal_entity?.name ?? '') : getFullName(data)
}
