import { CollectionItem } from "@/models/CollectionModels"; import getLogger from "@/utils/getLogger"; import { collectionStorage } from "./collectionStorage"; import getCollectionsFromCloud from "./getCollectionsFromCloud"; import getCollectionsFromLocal from "./getCollectionsFromLocal"; import saveCollectionsToCloud from "./saveCollectionsToCloud"; import saveCollectionsToLocal from "./saveCollectionsToLocal"; const logger = getLogger("resolveConflict"); export default function resolveConflict(acceptSource: "local" | "sync"): Promise { if (acceptSource === "local") return replaceCloudWithLocal(); return replaceLocalWithCloud(); } async function replaceCloudWithLocal(): Promise { const collections: CollectionItem[] = await getCollectionsFromLocal(); const lastUpdated: number = await collectionStorage.localLastUpdated.getValue(); await saveCollectionsToCloud(collections, lastUpdated); } async function replaceLocalWithCloud(): Promise { try { const collections: CollectionItem[] = await getCollectionsFromCloud(); const lastUpdated: number = await collectionStorage.syncLastUpdated.getValue(); await saveCollectionsToLocal(collections, lastUpdated); } catch (ex) { logger("Failed to get cloud storage"); console.error(ex); } }