plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.compose")
    id("org.jetbrains.kotlin.plugin.compose")
}

android {
    namespace = "com.dong.maxhap.nestedlib5"
    compileSdk = 34
    
    defaultConfig {
        minSdk = 24
    }
}

kotlin {
    androidTarget()
    iosX64()
    iosArm64()
    iosSimulatorArm64()
    ohosArm64()
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material)
                implementation(compose.ui)
                // 最后一个库，无需跨库依赖
            }
        }
    }
}

val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
val cmpVersion = versionCatalog.findVersion("composeMultiplatform")
    .orElseThrow { IllegalStateException("Missing version composeMultiplatform in version catalog") }
    .requiredVersion
val skikoOverrideByCmpVersion = mapOf(
    "1.9.2-ez-001" to "0.9.22.2-ez-001",
)
val skikoOhosOverride = skikoOverrideByCmpVersion[cmpVersion]

configurations.all {
    val configName = name
    if (configName.contains("ohos", ignoreCase = true)) {
        incoming.afterResolve {
            val resolvedSkiko = resolutionResult.allComponents
                .mapNotNull { it.moduleVersion }
                .firstOrNull { it.group == "org.jetbrains.skiko" && it.name == "skiko" }
            logger.lifecycle(
                "Resolved skiko for $configName: " +
                        (resolvedSkiko?.let { "${it.group}:${it.name}:${it.version}" } ?: "not found")
            )
        }
    }
    resolutionStrategy {
        eachDependency {
            if (requested.group == "org.jetbrains.skiko" && requested.name == "skiko") {
                if (configName.contains("ohos", ignoreCase = true)) {
                    skikoOhosOverride?.let { useVersion(it) }
                }
            }
            if (requested.group == "androidx.collection" && requested.name == "collection") {
                useVersion(libs.versions.androidx.collection.get())
            }
        }
    }
    exclude(group = "androidx.collection", module = "collection-jvm")
}
