import type { Noun } from '#shared/classes.ts'; import { genders, numeri, resolveFirstDeclension } from '#shared/nouns.ts'; import type { NounsData } from '#shared/nouns.ts'; import { getLocale, loadConfig, loadNounsData } from '~~/server/data.ts'; import { buildNoun, getNounEntries } from '~~/server/nouns.ts'; const downgradeToV1 = (noun: Noun, nounsData: NounsData) => { const downgradedNoun: Record = { id: noun.id, sourcesData: noun.sourcesData, categories: noun.categories.join('|'), sources: noun.sources.join(','), }; for (const gender of genders) { for (const numerus of numeri) { const wordSpellings = noun.words[gender]?.[numerus] ?.map((word) => resolveFirstDeclension(word, numerus, nounsData)) ?? []; downgradedNoun[gender + (numerus === 'singular' ? '' : 'Pl')] = wordSpellings.join('|'); } } return downgradedNoun; }; export default defineEventHandler(async (event) => { const locale = getLocale(event); checkIsConfigEnabledOr404(await loadConfig(locale), 'nouns'); const query = getQuery(event); const { isGranted } = await useAuthentication(event); const db = useDatabase(); const nouns = await getNounEntries(db, isGranted, locale); if (query.version !== '2') { const config = await loadConfig(locale); const nounsData = await loadNounsData(locale); return nouns .map((nounRaw) => buildNoun(nounRaw, config, nounsData)) .map((noun) => downgradeToV1(noun, nounsData)); } else { return nouns; } });