buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["CourierReactNative_kotlinVersion"]

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.1.0"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

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

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }

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

def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

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

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["CourierReactNative_" + 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.courierreactnative"

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

  compileSdkVersion safeExtGet("compileSdkVersion", getExtOrIntegerDefault("compileSdkVersion"))

  buildFeatures {
    buildConfig true
  }

  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", getExtOrIntegerDefault("minSdkVersion"))
    targetSdkVersion safeExtGet("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
  }

}

repositories {
  mavenCentral()
  google()
  maven { url 'https://jitpack.io' }
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {

  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-android:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

  // For converting to json
  implementation 'com.google.code.gson:gson:2.11.0'

  // Courier Core SDK
  api 'com.github.trycourier:courier-android:6.0.0'
  api 'androidx.recyclerview:recyclerview:1.3.2'

  // Firebase Messaging (needed to resolve RemoteMessage from Courier SDK APIs)
  compileOnly 'com.google.firebase:firebase-messaging:23.4.1'

}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "CourierReactNativeView"
    codegenJavaPackageName = "com.courierreactnative"
  }
}
