import type { Chapter, ChapterViewModel, ImageAsset } from './types'; function countByStatus(images: ImageAsset[], status: string): number { return images.filter((img) => img.status === status).length; } export function toChapterViewModel(chapter: Chapter): ChapterViewModel { const images = chapter.images ?? []; const completedCount = countByStatus(images, 'completed'); return { id: chapter.id, title: chapter.title, category: chapter.category, orderIndex: chapter.order_index, isEmpty: chapter.status === 'empty' || !chapter.content, isNew: chapter.is_new, hasImages: images.length > 0, allImagesReady: images.length > 0 && completedCount === images.length, pendingImageCount: countByStatus(images, 'pending') + countByStatus(images, 'processing'), failedImageCount: countByStatus(images, 'failed'), sections: chapter.sections ?? [], coverImageUrl: chapter.cover_image?.url ?? null, updatedAt: chapter.updated_at, }; } export function toChapterViewModels(chapters: Chapter[]): ChapterViewModel[] { return chapters.map(toChapterViewModel); }