import { m } from '$lib/paraglide/messages.js';

export const PERSON_TYPES = ['PERSON', 'INSTITUTION', 'GROUP', 'UNKNOWN'] as const;
export type PersonType = (typeof PERSON_TYPES)[number];

export type PersonFormData = {
	personType?: string | null;
	title?: string | null;
	firstName?: string | null;
	lastName: string;
	alias?: string | null;
	birthDate?: string | null;
	birthDatePrecision?: string | null;
	deathDate?: string | null;
	deathDatePrecision?: string | null;
	generation?: number | null;
	notes?: string | null;
};

export function normalizePersonType(raw: string | undefined | null): PersonType {
	return raw === 'SKIP' ? 'UNKNOWN' : ((raw ?? 'PERSON') as PersonType);
}

export type PersonValidationKey =
	| 'validation_last_name_required'
	| 'validation_first_name_required';

export function resolveValidationMessage(key: PersonValidationKey): string {
	return key === 'validation_last_name_required'
		? m.validation_last_name_required()
		: m.validation_first_name_required();
}

export function validatePersonFields(
	personType: string,
	firstName: string | undefined | null,
	lastName: string | undefined | null
): PersonValidationKey | null {
	if (!lastName) return 'validation_last_name_required';
	if (personType === 'PERSON' && !firstName) return 'validation_first_name_required';
	return null;
}
