package eu.friedcutlet.yokan.services.library import eu.friedcutlet.yokan.database.DbChapter import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbSmartUpdateRules import eu.friedcutlet.yokan.database.DbStatus import eu.friedcutlet.yokan.domain.effectiveChapterUpdateDays /** Pure admission rule shared by manual library refreshes and all scheduler entry points. */ internal fun DbSmartUpdateRules.permits(manga: DbManga, chapters: List, now: Long): Boolean { if (skipCompleted && manga.status == DbStatus.Completed) return false if (onlyCaughtUp && chapters.any { !it.isRead }) return false if (onlyStarted && chapters.isNotEmpty() && chapters.none { chapter -> chapter.isRead || (chapter.lastPageRead.toDoubleOrNull()?.let { it.isFinite() && it > 0 } == true) }) return false if (!respectExpectedReleasePeriod) return true val days = effectiveChapterUpdateDays(manga.smartUpdateDays, if (manga.smartUpdateDays == null) chapters.map { it.dateUpload } else emptyList(), now) if (days <= 0) return true return now - (manga.lastUpdate ?: 0L) >= days.toLong() * 86_400_000L }