import { apiJson, apiPost, apiFetch } from './client'; import type { GlossaryTerm, AutoExtractResponse, DeletedResponse } from './types'; export async function listGlossary(projectId: string): Promise { return apiJson(`/glossary/${encodeURIComponent(projectId)}`); } export async function addGlossaryTerm(projectId: string, term: Partial): Promise { return apiPost(`/glossary/${encodeURIComponent(projectId)}`, term); } export async function updateGlossaryTerm( projectId: string, termId: number, patch: Partial, ): Promise { const r = await apiFetch(`/glossary/${encodeURIComponent(projectId)}/${termId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(patch), }); return r.json() as Promise; } export async function deleteGlossaryTerm(projectId: string, termId: number): Promise { const r = await apiFetch(`/glossary/${encodeURIComponent(projectId)}/${termId}`, { method: 'DELETE', }); return r.json() as Promise; } export async function clearGlossary(projectId: string, onlyAuto: boolean = false): Promise { const qs = onlyAuto ? '?only_auto=true' : ''; const r = await apiFetch(`/glossary/${encodeURIComponent(projectId)}${qs}`, { method: 'DELETE', }); return r.json() as Promise; } export interface AutoExtractArgs { sourceLang: string; targetLang: string; segments: { id: string; text: string }[]; maxTerms?: number; } export async function autoExtractGlossary( projectId: string, { sourceLang, targetLang, segments, maxTerms = 40 }: AutoExtractArgs, ): Promise { return apiPost(`/glossary/${encodeURIComponent(projectId)}/auto-extract`, { source_lang: sourceLang, target_lang: targetLang, segments, max_terms: maxTerms, }); }