import type { components } from '$lib/generated/api';
import { joinNameOrUnknown } from './personFormat';

type Person = components['schemas']['Person'];

/**
 * Narrow chip/dedup contract for person pickers: exactly what PersonMultiSelect
 * renders. Full `Person` objects (search results) are structurally assignable;
 * view projections without a displayName go through {@link toPersonOption}.
 */
export type PersonOption = Pick<Person, 'id' | 'displayName'>;

/**
 * Maps a name-carrying projection (e.g. GeschichteView.PersonView, which has no
 * server-computed displayName) into the chip contract. Mirrors the server-side
 * fallback in GeschichteService.toView.
 */
export function toPersonOption(p: {
	id: string;
	firstName?: string | null;
	lastName?: string | null;
}): PersonOption {
	return {
		id: p.id,
		displayName: joinNameOrUnknown(p.firstName, p.lastName)
	};
}
