buildscript {

    /*
     * These property group is used to build kotlinx.coroutines against Kotlin compiler snapshot.
     * How does it work:
     * When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version,
     * atomicfu_version is overwritten by TeamCity environment (AFU is built with snapshot and published to mavenLocal
     * as previous step or the snapshot build).
     * Additionally, mavenLocal and Sonatype snapshots are added to repository list and stress tests are disabled.
     * DO NOT change the name of these properties without adapting kotlinx.train build chain.
     */
    def prop = rootProject.properties['build_snapshot_train']
    ext.build_snapshot_train = prop != null && prop != ""
    if (build_snapshot_train) {
        ext.kotlin_version = rootProject.properties['kotlin_snapshot_version']
        if (kotlin_version == null) {
            throw new IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with snapshot compiler")
        }
    }
    ext.native_targets_enabled = rootProject.properties['disable_native_targets'] == null

    // Determine if any project dependency is using a snapshot version
    ext.using_snapshot_version = build_snapshot_train
    rootProject.properties.each { key, value ->
        if (key.endsWith("_version") && value instanceof String && value.endsWith("-SNAPSHOT")) {
            println("NOTE: USING SNAPSHOT VERSION: $key=$value")
            ext.using_snapshot_version = true
        }
    }

    if (using_snapshot_version) {
        repositories {
            mavenLocal()
            maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
        }
    }

}

plugins {
    id "org.jetbrains.kotlin.jvm" version "$kotlin_version"
}

repositories {
    if (build_snapshot_train) {
        maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
    }
    mavenLocal()
    mavenCentral()
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testImplementation "org.ow2.asm:asm:$asm_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

sourceSets {
    // An assortment of tests for behavior of the core coroutines module on JVM
    jvmCoreTest {
        kotlin
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath

        dependencies {
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation 'com.google.guava:guava:31.1-jre'
        }
    }
    // Checks correctness of Maven publication (JAR resources) and absence of atomicfu symbols
    mavenTest {
        kotlin
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath

        dependencies {
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
        }
    }
    // Checks that kotlinx-coroutines-debug can be used as -javaagent parameter
    debugAgentTest {
        kotlin
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath

        dependencies {
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version"
        }
    }

    // Checks that kotlinx-coroutines-debug agent can self-attach dynamically to JVM as a standalone dependency
    debugDynamicAgentTest {
        kotlin
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath

        dependencies {
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version"
        }
    }

    // Checks that kotlinx-coroutines-core can be used as -javaagent parameter
    coreAgentTest {
        kotlin
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath

        dependencies {
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
        }
    }
}

compileDebugAgentTestKotlin {
    kotlinOptions {
        freeCompilerArgs += ["-Xallow-kotlin-package"]
        jvmTarget = "1.8"
    }
}

task jvmCoreTest(type: Test) {
    environment "version", coroutines_version
    def sourceSet = sourceSets.jvmCoreTest
    testClassesDirs = sourceSet.output.classesDirs
    classpath = sourceSet.runtimeClasspath
}

task mavenTest(type: Test) {
    environment "version", coroutines_version
    def sourceSet = sourceSets.mavenTest
    testClassesDirs = sourceSet.output.classesDirs
    classpath = sourceSet.runtimeClasspath
}

task debugAgentTest(type: Test) {
    def sourceSet = sourceSets.debugAgentTest
    def coroutinesDebugJar = sourceSet.runtimeClasspath.filter {it.name == "kotlinx-coroutines-debug-${coroutines_version}.jar" }.singleFile
    jvmArgs ('-javaagent:' + coroutinesDebugJar)
    testClassesDirs = sourceSet.output.classesDirs
    classpath = sourceSet.runtimeClasspath
    systemProperties project.properties.subMap(["overwrite.probes"])
}

task debugDynamicAgentTest(type: Test) {
    def sourceSet = sourceSets.debugDynamicAgentTest
    testClassesDirs = sourceSet.output.classesDirs
    classpath = sourceSet.runtimeClasspath
}

task coreAgentTest(type: Test) {
    def sourceSet = sourceSets.coreAgentTest
    def coroutinesCoreJar = sourceSet.runtimeClasspath.filter {it.name == "kotlinx-coroutines-core-jvm-${coroutines_version}.jar" }.singleFile
    jvmArgs ('-javaagent:' + coroutinesCoreJar)
    testClassesDirs = sourceSet.output.classesDirs
    classpath = sourceSet.runtimeClasspath
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "17"
}

check {
    dependsOn([jvmCoreTest, debugDynamicAgentTest, mavenTest, debugAgentTest, coreAgentTest, ":jpmsTest:check", 'smokeTest:build', "java8Test:check"])
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "17"
    }
}

kotlin.jvmToolchain(17)

// Drop this when node js version become stable
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
    it.args.add("--ignore-engines")
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile.class).configureEach {
    jvmTargetValidationMode = org.jetbrains.kotlin.gradle.dsl.jvm.JvmTargetValidationMode.WARNING
}
