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

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

apply plugin: 'com.android.library'

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

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 28)
    buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 16)
        targetSdkVersion safeExtGet('targetSdkVersion', 28)
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ["src/turbo"]
            } else {
                java.srcDirs += ["src/legacy"]
            }
        }
    }

    lintOptions {
        abortOnError false
    }
}

repositories {
    google()
    mavenCentral()
    mavenLocal()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }

    maven { url 'https://maven.fullstory.com' }
}

def rootScriptClassPathFiles = rootProject.buildscript.getScriptClassPath().getAsFiles()
String searchString = 'gradle-plugin-local-'
String fsDetectedVersion = null
for (File file : rootScriptClassPathFiles) {
    int lastIndex = file.name.lastIndexOf(searchString)
    if (lastIndex < 0) {
        continue
    }

    if (!file.name.endsWith('.jar')) {
        continue
    }

    int endIndex = file.name.length() - 4
    int startIndex = lastIndex + searchString.length()
    fsDetectedVersion = file.name.substring(startIndex, endIndex)
    break
}

if (fsDetectedVersion == null) {
    throw new GradleException('Unable to determine FullStory version, please verify your root build file is properly configured')
}

def versionRegex = /(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/
def match = fsDetectedVersion =~ versionRegex
if (match.matches()) {
    def major = match.group('major') as Integer
    def minor = match.group('minor') as Integer
    if (major < 1 || (major == 1 && minor < 14)) {
        throw new GradleException("FullStory SDK version 1.14.0 or later is required, but version $fsDetectedVersion was detected. Please update to a newer version of FullStory.")
    }
} else {
    logger.warn('Unable to determine FullStory version, please verify your root build file is properly configured')
}

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 "com.fullstory:instrumentation-full:$fsDetectedVersion@aar"
}
