def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

// Gradle configures projects alphabetically, so this project sorts before ':app'.
// If the app doesn't apply RN's 'com.facebook.react.rootproject' plugin, the RN gradle
// plugin's jvmToolchain(17) sweep reaches this project after AGP has already finalized
// its Java toolchain, failing with "property 'languageVersion' is final" (issue #1510).
// Configuring ':app' first (as rootproject would) keeps the sweep ahead of finalization.
// When the app already applies that plugin, this block is a harmless no-op (both force ':app' first).
if (findProject(':app') != null) {
  evaluationDependsOn(':app')
}

apply plugin: "com.android.library"
// AGP 9 ships built-in Kotlin support and registers the `kotlin` extension
// itself. Applying the Kotlin plugin on top of it fails configuration with
// "Cannot add extension with name 'kotlin'". Asking for the extension covers
// every way built-in Kotlin can be turned on -- the global default, the
// android.builtInKotlin property, and the per-module com.android.built-in-kotlin
// plugin -- without reading an AGP version number.
if (project.extensions.findByName('kotlin') == null) {
  apply plugin: "kotlin-android"
}

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["AmplitudeReactNative_" + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AmplitudeReactNative_" + name]).toInteger()
}

def supportsNamespace() {
  def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
  def major = parsed[0].toInteger()
  def minor = parsed[1].toInteger()

  // Namespace support was added in 7.3.0
  if (major == 7 && minor >= 3) {
    return true
  }

  return major >= 8
}

android {
  if (supportsNamespace()) {
    namespace "com.amplitude.reactnative"

    sourceSets {
      main {
        manifest.srcFile "src/main/AndroidManifestNew.xml"
      }
    }
  }

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  }
  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  testOptions {
    unitTests {
      // Robolectric needs the merged Android resources/manifest on the test classpath.
      includeAndroidResources = true
      returnDefaultValues = true
    }
  }

}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

  // Unit tests for the framework-free connectivity logic (ConnectivityChecker).
  // Robolectric provides the android.net.* stubs so we can mock ConnectivityManager
  // without a device; see src/test/.../ConnectivityCheckerTest.kt.
  testImplementation "junit:junit:4.13.2"
  testImplementation "org.robolectric:robolectric:4.10.3"
  testImplementation "io.mockk:mockk:1.13.3"
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "AmplitudeReactNative"
    codegenJavaPackageName = "com.amplitude.reactnative"
  }
}
