import type { Book, DeckSettings, MinedCard, ReviewLog, WordUnderstandingRecord, } from '../types' import { createSyncedBook, deleteBook, ensureBookContentHash, listBooks } from './books' import { deleteMinedCard, getDeckSettings, listMinedCards, listReviewLogs, } from './cards' import { bookSyncKey, type RemoteBookRecord } from './remoteSyncRecords' import { BOOKS_STORE, CARDS_STORE, REVIEW_LOGS_STORE, SETTINGS_STORE, withStore, } from './storage' import { applySyncedWordUnderstanding, listWordUnderstandingRecords, } from './wordUnderstanding' export interface LocalSyncSnapshot { books: Book[] cards: MinedCard[] reviews: ReviewLog[] deckSettings: DeckSettings wordLevels: WordUnderstandingRecord[] } export async function getLocalSyncSnapshot(): Promise { const [rawBooks, cards, reviews, deckSettings, wordLevels] = await Promise.all([ listBooks(), listMinedCards(), listReviewLogs(), getDeckSettings(), listWordUnderstandingRecords(), ]) const books: Book[] = [] for (const book of rawBooks) { try { books.push(await ensureBookContentHash(book)) } catch { books.push(book) } } return { books, cards, reviews, deckSettings, wordLevels } } export { applySyncedWordUnderstanding } export async function applySyncedCard(card: MinedCard): Promise { const current = await withStore( CARDS_STORE, 'readonly', (store) => store.get(card.id), ) if (current && current.updatedAt >= card.updatedAt) return false await withStore(CARDS_STORE, 'readwrite', (store) => store.put(card)) return true } export async function applySyncedReview(log: ReviewLog): Promise { const current = await withStore( REVIEW_LOGS_STORE, 'readonly', (store) => store.get(log.id), ) if (current) return false await withStore(REVIEW_LOGS_STORE, 'readwrite', (store) => store.put(log)) return true } export async function applySyncedDeckSettings(settings: DeckSettings): Promise { const current = await getDeckSettings() if (current.updatedAt >= settings.updatedAt) return false await withStore(SETTINGS_STORE, 'readwrite', (store) => store.put(settings)) return true } export async function applySyncedBookRecord(record: RemoteBookRecord): Promise { const books = await listBooks() const match = books.find((book) => ( book.id === record.localBookId || bookSyncKey(book) === record.syncKey )) if (!match) return false const remoteTitleUpdatedAt = record.titleUpdatedAt ?? record.createdAt const localTitleUpdatedAt = match.titleUpdatedAt ?? match.createdAt const useRemoteTitle = remoteTitleUpdatedAt > localTitleUpdatedAt const useRemoteProgress = record.progress.updatedAt > match.progress.updatedAt if (!useRemoteTitle && !useRemoteProgress) return false await withStore(BOOKS_STORE, 'readwrite', (store) => store.put({ ...match, title: useRemoteTitle ? record.title : match.title, titleUpdatedAt: useRemoteTitle ? remoteTitleUpdatedAt : match.titleUpdatedAt, progress: useRemoteProgress ? record.progress : match.progress, updatedAt: Math.max(match.updatedAt, record.updatedAt), })) return true } export async function applySyncedBookContent( record: RemoteBookRecord, content: string, ): Promise { const books = await listBooks() if (books.some((book) => bookSyncKey(book) === record.syncKey)) return false await createSyncedBook(record, content) return true } export async function deleteSyncedCard(id: string): Promise { const existing = await withStore( CARDS_STORE, 'readonly', (store) => store.get(id), ) if (!existing) return false await deleteMinedCard(id, { sync: false }) return true } export async function deleteSyncedBook(syncKey: string): Promise { const books = await listBooks() const match = books.find((book) => bookSyncKey(book) === syncKey) if (!match) return false await deleteBook(match.id, { sync: false }) return true }