/**
 * Find the latest version of KSP that supports the specified Kotlin version.
 */
static String getKspVersion(String kotlinVersion) {
    def availableVersions = [
        // Run `scripts/update-ksp-versions.mjs` to update this list
        // update-ksp-versions start
        "1.9.22-1.0.17",
        "1.9.21-1.0.16",
        "1.9.20-1.0.14",
        "1.9.10-1.0.13",
        "1.9.0-1.0.13",
        "1.8.22-1.0.11",
        "1.8.21-1.0.11",
        "1.8.20-1.0.11",
        "1.8.10-1.0.9",
        "1.8.0-1.0.9",
        "1.7.22-1.0.8",
        "1.7.21-1.0.8",
        "1.7.20-1.0.8",
        "1.7.10-1.0.6",
        "1.7.0-1.0.6",
        "1.6.21-1.0.6",
        "1.6.20-1.0.5",
        "1.6.10-1.0.4",
        "1.6.0-1.0.2",
        "1.5.31-1.0.1",
        "1.5.30-1.0.0",
        // update-ksp-versions end
    ]
    def version = availableVersions.find { it.startsWith(kotlinVersion) }
    return version ?: availableVersions[0]
}

ext {
    apply(from: "${buildscript.sourceFile.getParent()}/test-app-util.gradle")

    compileSdkVersion = 34
    minSdkVersion = 23
    targetSdkVersion = 33

    /**
     * Returns the recommended Gradle plugin version for the specified React Native
     * version.
     */
    def getDefaultGradlePluginVersion = { reactNativeVersion ->
        // Gradle plugin version can be found in the template:
        // https://github.com/facebook/react-native/blob/main/packages/react-native/template/android/build.gradle
        if (reactNativeVersion == 0 || reactNativeVersion >= v(0, 73, 0)) {
            return ""
        } else if (reactNativeVersion >= v(0, 71, 0)) {
            return "7.3.1"
        } else {
            return "7.2.2"
        }
    }

    reactNativeVersion = getPackageVersionNumber("react-native", rootDir)
    autodetectReactNativeVersion = reactNativeVersion == 0 || reactNativeVersion >= v(0, 71, 0)
    enableNewArchitecture = isNewArchitectureEnabled(project)
    enableBridgeless = isBridgelessEnabled(project, enableNewArchitecture)
    usePrefabs = reactNativeVersion == 0 || reactNativeVersion >= v(0, 71, 0)

    androidPluginVersion = getDefaultGradlePluginVersion(reactNativeVersion)
    kotlinVersion = rootProject.findProperty("KOTLIN_VERSION") ?: getKotlinVersion(rootDir)
    kspVersion = getKspVersion(kotlinVersion)

    // We need only set `ndkVersion` when building react-native from source.
    if (rootProject.hasProperty("ANDROID_NDK_VERSION")) {
        ndkVersion = rootProject.properties["ANDROID_NDK_VERSION"]
    } else if (reactNativeVersion >= v(0, 74, 0)) {
        // https://github.com/facebook/react-native/commit/9ce7b564131c5b2075489c09ff05325ddc28014a
        ndkVersion = "26.1.10909125"
    } else if (System.properties["os.arch"] == "aarch64" && androidPluginVersion == "7.2.2") {
        // NDK r23c has been patched to support Apple M1 and is default in AGP
        // 7.3.0. Prior to 0.71, we still need to set `ndkVersion` because we'll
        // be using AGP 7.2.2 (see `androidPluginVersion` above).
        // Note that even though newer 23.x versions exist, we'll stick to AGP's
        // default. See also
        // https://developer.android.com/studio/releases/gradle-plugin#compatibility-7-3-0
        ndkVersion = "23.1.7779620"
    }

    def kotlinVersionNumber = toVersionNumber(kotlinVersion)

    /**
     * Dependabot requires a `dependencies.gradle` to evaluate Java
     * dependencies. It is also very particular about the formatting and cannot
     * evaluate string literals.
     *
     * Hint: When making conditional changes, check whether the correct version
     * is set with `./gradlew app:dependencies`.
     *
     * See https://github.com/dependabot/feedback/issues/345.
     */
    libraries = [
        androidAppCompat            : "androidx.appcompat:appcompat:1.6.1",
        androidCamera               : kotlinVersionNumber >= v(1, 8, 0)
                                          ? "androidx.camera:camera-camera2:1.3.0-beta02"
                                          : "androidx.camera:camera-camera2:1.2.0-beta02",
        androidCameraMlKitVision    : kotlinVersionNumber >= v(1, 8, 0)
                                          ? "androidx.camera:camera-mlkit-vision:1.3.0-beta02"
                                          : "androidx.camera:camera-mlkit-vision:1.2.0-beta02",
        androidCoreKotlinExtensions : kotlinVersionNumber >= v(1, 8, 0)
                                          ? "androidx.core:core-ktx:1.10.1"
                                          : "androidx.core:core-ktx:1.9.0",
        androidEspressoCore         : "androidx.test.espresso:espresso-core:3.5.1",
        androidJUnit                : "androidx.test.ext:junit:1.1.5",
        androidJUnitKotlinExtensions: "androidx.test.ext:junit-ktx:1.1.5",
        androidRecyclerView         : "androidx.recyclerview:recyclerview:1.3.2",
        androidSwipeRefreshLayout   : "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
        junit                       : "junit:junit:4.13.2",
        materialComponents          : "com.google.android.material:material:1.11.0",
        mlKitBarcodeScanning        : "com.google.mlkit:barcode-scanning:17.2.0",
        mockitoInline               : "org.mockito:mockito-inline:5.2.0",
        moshiKotlin                 : kotlinVersionNumber >= v(1, 8, 0)
                                          ? "com.squareup.moshi:moshi-kotlin:1.15.0"
                                          : "com.squareup.moshi:moshi-kotlin:1.14.0",
        moshiKotlinCodegen          : kotlinVersionNumber >= v(1, 8, 0)
                                          ? "com.squareup.moshi:moshi-kotlin-codegen:1.15.0"
                                          : "com.squareup.moshi:moshi-kotlin-codegen:1.14.0",
    ]

    getReactNativeDependencies = {
        // Hint: Use `./gradlew buildEnvironment` to check whether these are
        // correctly set.
        def dependencies = [
          androidPluginVersion == ""
              ? "com.android.tools.build:gradle"
              : "com.android.tools.build:gradle:${androidPluginVersion}",
          "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}",
        ]

        if (autodetectReactNativeVersion || enableNewArchitecture) {
            dependencies << "com.facebook.react:react-native-gradle-plugin"
            dependencies << "de.undercouch:gradle-download-task:5.5.0"
        }

        return dependencies
    }
}
