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

android {
    namespace = "com.dong.maxhap.nestedlib3"
    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)
                // 跨库依赖 - 指向下一个库
                implementation(project(":nestedlib4"))
            }
        }
    }
}

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")
}
