import type { AutomationScheduleDto, MetadataProviderId, MetadataSourceConfigDto, MetadataSourcesConfigDto, UpdateMetadataSourcesConfigDto } from "@readabook/shared"; export const providerLabels: Record = { local: "Fichier local", openlibrary: "OpenLibrary", googlebooks: "Google Books", bnf: "BnF" }; const weekdays = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"]; export function normalizeMetadataSources(config: MetadataSourcesConfigDto): MetadataSourcesConfigDto { const sorted = [...config.sources].sort((left, right) => left.priority - right.priority); const local = sorted.find((source) => source.provider === "local") ?? { provider: "local", enabled: true, priority: 0, hasApiKey: false }; const external = sorted.filter((source) => source.provider !== "local"); return { isbnPriorityEnabled: config.isbnPriorityEnabled, sources: [ { ...local, enabled: true, priority: 0 }, ...external.map((source, index) => ({ ...source, priority: index + 1 })) ] }; } export function metadataSourcesPayload(config: MetadataSourcesConfigDto): UpdateMetadataSourcesConfigDto { const sources: NonNullable = []; config.sources.forEach((source) => { if (source.provider === "local") return; sources.push({ provider: source.provider, enabled: source.enabled, priority: sources.length + 1 }); }); return { isbnPriorityEnabled: config.isbnPriorityEnabled, sources }; } export function moveSource(sources: MetadataSourceConfigDto[], provider: MetadataProviderId, direction: -1 | 1): MetadataSourceConfigDto[] { const external = sources.filter((source) => source.provider !== "local"); const index = external.findIndex((source) => source.provider === provider); const nextIndex = index + direction; if (index < 0 || nextIndex < 0 || nextIndex >= external.length) return sources; const nextExternal = [...external]; [nextExternal[index], nextExternal[nextIndex]] = [nextExternal[nextIndex], nextExternal[index]]; const local = sources.find((source) => source.provider === "local") ?? { provider: "local", enabled: true, priority: 0, hasApiKey: false }; return [local, ...nextExternal].map((source, priority) => ({ ...source, priority: source.provider === "local" ? 0 : priority })); } export function scheduleSummary(schedule: AutomationScheduleDto, subject: string): string { if (schedule.frequency === "disabled") return `${subject} desactive.`; if (schedule.frequency === "daily") return `${subject} tous les jours a ${schedule.time}.`; return `${subject} chaque ${weekdays[schedule.dayOfWeek]} a ${schedule.time}.`; } export const scheduleDays = weekdays.map((label, value) => ({ label, value }));