ext {
  transactVersion = '3.24.1'
}

buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  // Prefer the library's declared Kotlin version to avoid root project mismatches
  def kotlin_version = project.properties["TransactReactNative_kotlinVersion"] ?: (rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : null)

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.7.2"
    // 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 getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["TransactReactNative_" + name]
}

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

android {
  namespace "com.atomicfi.transactreactnative"
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  buildFeatures {
    buildConfig true
  }

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

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = project.properties["TransactReactNative_kotlinVersion"] ?: 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"

  // transact deps end
  implementation "financial.atomic:transact:$transactVersion"
  // Decode the JS data-request response into the SDK's @Serializable TransactDataResponse.
  // (kotlinx-serialization-json is already a transitive runtime dep of the SDK.)
  implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3"
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "TransactReactNative"
    codegenJavaPackageName = "com.atomicfi.transactreactnative"
  }
}
