package com.isidroid.sync_service.ext

import androidx.work.ListenableWorker
import androidx.work.WorkInfo
import androidx.work.WorkManager


internal fun WorkManager.isWorkerRunning(workerClass: Class<out ListenableWorker>): Boolean {
    val data = getWorkInfosForUniqueWork(workerClass.name).get()
    return data.any { it.state == WorkInfo.State.RUNNING || it.state == WorkInfo.State.ENQUEUED }
}

internal fun WorkManager.isWorkerPendingOrRunning(workerClass: Class<out ListenableWorker>): Boolean {
    val data = getWorkInfosForUniqueWork(workerClass.name).get()
    return data.any { it.state == WorkInfo.State.RUNNING || it.state == WorkInfo.State.ENQUEUED || it.state == WorkInfo.State.BLOCKED }
}

//internal fun Collection<SyncJob>.updateSyncJobsDataIds(vararg removeDataId: String) = mapNotNull { job ->
//    val newDataList = job.data.split(",").filter { !removeDataId.contains(it) }
//    if (newDataList.isEmpty()) null
//    else {
//        val data = newDataList.joinToString(",")
//        job.copy(state = JobState.PENDING, id = UUID.randomUUID().toString(), data = data)
//    }
//}
