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

    /**
     * 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"
        }
    }

    // TODO: Bump `minSdkVersion` to 24 in 4.0
    def getDefaultMinSdkVersion = { reactNativeVersion ->
        if (reactNativeVersion >= v(0, 76, 0)) {
            return 24
        } else {
            return 23
        }
    }

    reactNativeVersion = getPackageVersionNumber("react-native", rootDir)

    compileSdkVersion = rootProject.findProperty("react.compileSdkVersion") ?: 34
    minSdkVersion = rootProject.findProperty("react.minSdkVersion")
                        ?: getDefaultMinSdkVersion(reactNativeVersion)
    targetSdkVersion = rootProject.findProperty("react.targetSdkVersion") ?: 33

    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)

    // 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/build/releases/past-releases/agp-7-3-0-release-notes
        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.7.0",
        androidCamera               : "androidx.camera:camera-camera2:1.4.0-rc01",
        androidCameraMlKitVision    : "androidx.camera:camera-mlkit-vision:1.4.0-rc01",
        androidCoreKotlinExtensions : "androidx.core:core-ktx:1.13.1",
        androidRecyclerView         : "androidx.recyclerview:recyclerview:1.3.2",
        materialComponents          : "com.google.android.material:material:1.12.0",
        mlKitBarcodeScanning        : "com.google.mlkit:barcode-scanning:17.3.0",
    ]

    // Separate block so bots can parse this file properly
    if (kotlinVersionNumber < v(1, 8, 0)) {
        libraries.androidAppCompat = ["androidx.appcompat", "appcompat", "1.6.1"].join(":")
        libraries.androidCamera = ["androidx.camera", "camera-camera2", "1.2.0-beta02"].join(":")
        libraries.androidCameraMlKitVision = ["androidx.camera", "camera-mlkit-vision", "1.2.0-beta02"].join(":")
        libraries.androidCoreKotlinExtensions = ["androidx.core", "core-ktx", "1.9.0"].join(":")
    }

    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.6.0"
        }

        return dependencies
    }
}
