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

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

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

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

def shouldImportBrazeLocationLibrary() {
 return project.hasProperty("importBrazeLocationLibrary") && project.importBrazeLocationLibrary == "true"
}  

android {
  compileSdk safeExtGet('compileSdkVersion', 33)
  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
  if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
    namespace "com.braze.reactbridge"
    buildFeatures {
      buildConfig true
    }
  }

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 16)
    targetSdkVersion safeExtGet('targetSdkVersion', 33)
    buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
    versionCode 1
    versionName '1.0'
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += ['src/newarch']
        res.srcDirs += ['src/newarch/res']
      } else {
        java.srcDirs += ['src/oldarch']
        res.srcDirs += ['src/oldarch/res']
      }
    }
  }

  kotlinOptions {
    freeCompilerArgs = ['-Xjvm-default=all']
  }
}

dependencies {
  api 'com.facebook.react:react-native:+'

  def wrapper_braze_sdk_version = "36.0.0"
  api "com.braze:android-sdk-ui:${wrapper_braze_sdk_version}"
  
  if (shouldImportBrazeLocationLibrary()) {
      api "com.braze:android-sdk-location:${wrapper_braze_sdk_version}"
  }
}
