import { apiRequest } from '../lib/api' import { getActiveBusinessId } from '../lib/businessContext' import type { EntityTranslation, EntityTranslationsResponse, TranslationEntityType, TranslationFields, TranslationLocale, } from '../types/translation' function businessPath(entityType: TranslationEntityType, entityId: string, suffix = '') { const businessId = getActiveBusinessId() if (!businessId) { throw new Error('No active business selected. Please sign in again.') } const resource = entityType === 'product' ? 'products' : 'categories' return `/businesses/${businessId}/${resource}/${entityId}/translations${suffix}` } export async function listEntityTranslations( entityType: TranslationEntityType, entityId: string, signal?: AbortSignal, ) { return apiRequest(businessPath(entityType, entityId), { auth: true, signal, }) } export async function translateEntityWithAi( entityType: TranslationEntityType, entityId: string, locale: TranslationLocale, ) { return apiRequest<{ message: string; translation: EntityTranslation }>( businessPath(entityType, entityId, `/${locale}/ai`), { method: 'POST', auth: true, body: {}, }, ) } export async function saveEntityTranslation( entityType: TranslationEntityType, entityId: string, locale: TranslationLocale, fields: TranslationFields, ) { return apiRequest<{ message: string; translation: EntityTranslation }>( businessPath(entityType, entityId, `/${locale}`), { method: 'PUT', auth: true, body: { fields }, }, ) } export async function translateAllProductCategories(locale: TranslationLocale) { const businessId = getActiveBusinessId() if (!businessId) { throw new Error('No active business selected. Please sign in again.') } return apiRequest<{ message: string locale: TranslationLocale translated: number skipped: number failed: number }>(`/businesses/${businessId}/categories/translations/ai-all`, { method: 'POST', auth: true, body: { locale }, }) } export async function translateAllProducts(locale: TranslationLocale) { const businessId = getActiveBusinessId() if (!businessId) { throw new Error('No active business selected. Please sign in again.') } return apiRequest<{ message: string locale: TranslationLocale translated: number skipped: number failed: number }>(`/businesses/${businessId}/products/translations/ai-all`, { method: 'POST', auth: true, body: { locale }, }) }