buildscript {
  ext.safeExtGet = {prop, fallback ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
  }
  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:7.0.4'
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${safeExtGet('kotlinVersion', '1.6.10')}"
  }
}

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
  compileSdkVersion safeExtGet('compileSdkVersion', 31)

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 21)
    targetSdkVersion safeExtGet('targetSdkVersion', 31)
    versionCode 1
    versionName "1.0"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    if (isNewArchitectureEnabled()) {
      var appProject = rootProject.allprojects.find {it.plugins.hasPlugin('com.android.application')}
      externalNativeBuild {
        ndkBuild {
          arguments "APP_PLATFORM=android-21",
            "APP_STL=c++_shared",
            "NDK_TOOLCHAIN_VERSION=clang",
            "GENERATED_SRC_DIR=${appProject.buildDir}/generated/source",
            "PROJECT_BUILD_DIR=${appProject.buildDir}",
            "REACT_ANDROID_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid",
            "REACT_ANDROID_BUILD_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid/build"
          cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
          cppFlags "-std=c++17"
          targets "rnaps_modules"
        }
      }
    }
  }
  if (isNewArchitectureEnabled()) {
    externalNativeBuild {
      ndkBuild {
        path "src/main/jni/Android.mk"
      }
    }
  }
  packagingOptions {
    // For some reason gradle only complains about the duplicated version of libreact_render libraries
    // while there are more libraries copied in intermediates folder of the lib build directory, we exclude
    // only the ones that make the build fail (ideally we should only include librnaps_modules but we
    // are only allowed to specify exclude patterns)
    exclude "**/libreact_render*.so"
  }

  buildTypes {
    release {
      minifyEnabled true
      proguardFile 'proguard-rules.pro'
    }
  }
}

repositories {
  maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    // Matches the RN Hello World template
    // https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/local-cli/templates/HelloWorld/android/build.gradle#L21
    url "$projectDir/../node_modules/react-native/android"
  }
  mavenCentral()
  google()
}

dependencies {
  if (isNewArchitectureEnabled()) {
    implementation project(":ReactAndroid")
  } else {
    implementation 'com.facebook.react:react-native:+'
  }
  implementation ("com.amazon.android:aps-sdk:9.4.2")
}

if (isNewArchitectureEnabled()) {
  react {
    libraryName = "rnaps"
    codegenJavaPackageName = "com.adversport.rnaps"
    root = rootProject.file("..")
    jsRootDir = file("../src/turbomodules")
    reactNativeDir = rootProject.file("../node_modules/react-native/")
    codegenDir = rootProject.file("../node_modules/react-native-codegen/")
  }
}

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

def isNewArchitectureEnabled() {
  // To opt-in for the New Architecture, you can either:
  // - Set `newArchEnabled` to true inside the `gradle.properties` file
  // - Invoke gradle with `-newArchEnabled=true`
  // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}
