import groovy.json.JsonSlurper

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

def getVersionFromJsPackage(project) {
  def packageJson = getPackageJson(project)
  def version = packageJson.version

  def isSnapshot = System.getenv("SNAPSHOT") ?: false
  if (isSnapshot) {
      version = version + "-SNAPSHOT"
  }
  return version
}

def getPackageJson(project) {
  def packageJson = new File(project.projectDir, '../package.json').getText('UTF-8')
  def jsonSlurper = new JsonSlurper()
  return jsonSlurper.parseText(packageJson)
}

def _reactNativeVersion = safeExtGet("reactNative", "+")
def _backpackAndroidVersion = safeExtGet("backpackVersion", "42.2.2")
def _compileSdkVersion = safeExtGet("compileSdkVersion", 33)
def _targetSdkVersion = safeExtGet("targetSdkVersion", 33)
def _minSdkVersion = safeExtGet("minSdkVersion", 26)
def _internalBuild = safeExtGet("internalBuild", false)

apply plugin: "com.android.library"

apply plugin: 'kotlin-android'

if (_internalBuild) {
  // This should come after the kotlin plugins
  apply plugin: 'com.kezong.fat-aar'

  apply from: './gradle-maven-push.gradle'
}

description='Bridge code for Backpack React Native'

group='net.skyscanner.backpack'

version=getVersionFromJsPackage(project)

android {
  compileSdkVersion _compileSdkVersion

  defaultConfig {
    minSdkVersion _minSdkVersion
    targetSdkVersion _targetSdkVersion

    versionName getVersionFromJsPackage(project)

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }
}

dependencies {
  compileOnly "com.facebook.react:react-native:${_reactNativeVersion}"
  implementation "net.skyscanner.backpack:backpack-android:${_backpackAndroidVersion}"
  implementation "androidx.constraintlayout:constraintlayout:2.1.3"
}
