package eu.friedcutlet.yokan.database.sync import eu.friedcutlet.yokan.database.DbSyncPreference /** * The preference after the hub reported [epoch], plus the epoch whose older outbox operations * must be dropped (`null` keeps them). The first negotiation to epoch 1 is not a rollback. */ internal fun DbSyncPreference.hubResync(epoch: Int): Pair { val initialNegotiation = hubEpoch == 0 && !hubBootstrapComplete && epoch == 1 val rollback = !initialNegotiation && (hubEpoch > 0 || hubBootstrapComplete || epoch > 1) val dropBefore = epoch.takeIf { rollback && it > hubEpoch } val updated = copy( hubHead = 0, hubEpoch = maxOf(hubEpoch, epoch), hubRecoveryPending = hubRecoveryPending || dropBefore != null, ) return updated to dropBefore }