plugins {
    // Declared with apply false so the Kotlin Gradle Plugin classes (used below to fix up
    // file_picker's Kotlin compilation) are resolvable from this script, without applying the
    // plugin to the root project itself. Version matches settings.gradle.kts.
    id("org.jetbrains.kotlin.android") version "2.3.20" apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

val newBuildDir: Directory =
    rootProject.layout.buildDirectory
        .dir("../../build")
        .get()
rootProject.layout.buildDirectory.value(newBuildDir)

subprojects {
    val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name)
    project.layout.buildDirectory.value(newSubprojectBuildDir)
}
subprojects {
    project.evaluationDependsOn(":app")
}

// file_picker's android/build.gradle guards `apply plugin: 'org.jetbrains.kotlin.android'`
// behind a runtime AGP-version check (skipped on AGP 9+, where it assumes AGP's built-in
// Kotlin support is active). Flutter's own auto-apply logic in FlutterPluginUtils only
// scans the file's text for that apply statement -- it matches even though the guard makes
// it a no-op on this project's AGP 9, so Flutter skips applying Kotlin itself too. With
// android.builtInKotlin=false (kept for compatibility with other plugins), no Kotlin plugin
// ever gets applied to file_picker, and its Kotlin sources (including FilePickerPlugin) never
// compile. Apply it explicitly here once AGP's library plugin is in place.
subprojects {
    if (name == "file_picker") {
        pluginManager.withPlugin("com.android.library") {
            pluginManager.apply("org.jetbrains.kotlin.android")
            // file_picker's own jvmTarget = 17 setup is behind the same dead AGP9 guard, so its
            // Kotlin compile tasks would otherwise default to the running JDK's target (21),
            // conflicting with the javac target (17) set by its `compileOptions` block.
            tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
                compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
            }
        }
    }
}

tasks.register<Delete>("clean") {
    delete(rootProject.layout.buildDirectory)
}
