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

buildscript {
    ext.isNewArchitectureEnabled = { _ ->
        project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
    }
    // buildscript dependencies are only required when working with the module in isolation (e.g.
    // during development and for testing). This avoids unnecessary downloads and potential
    // conflicts when the library is included as a module dependency in an application project.
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.2.1'
            if (isNewArchitectureEnabled()) {
                classpath 'com.facebook.react:react-native-gradle-plugin'
            }
        }
    }
}

apply plugin: 'com.android.library'

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

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 31).toInteger()
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 31)
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    }
    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch/java']
            } else {
                java.srcDirs += ['src/oldarch/java']
            }
        }
    }
    lintOptions {
        abortOnError false
    }
}

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

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
}
