package com.isidroid.c23.ext

import android.content.Context
import android.net.Uri
import com.isidroid.utils.getMimeType
import com.isidroid.utils.isMimeTypeImage
import com.isidroid.utils.isMimeTypePdf
import com.isidroid.utils.saveAsBitmapToFile
import com.isidroid.utils.savePdfAsBitmapFiles
import java.io.File

fun List<Uri>.transformToBitmapFiles(context: Context): List<File> {
    val uris = this
    val result = mutableListOf<File>()

    uris.forEach { uri ->
        val mime = uri.getMimeType(context)
        val isImage = mime.isMimeTypeImage
        val isPdf = mime.isMimeTypePdf

        when {
            isImage -> uri.saveAsBitmapToFile(context)?.also { result.add(it) }
            isPdf -> uri.savePdfAsBitmapFiles(context)?.also { result.addAll(it) }
        }
    }
    return result
}