buildscript {
    ext.IS_ROOT = project == rootProject

    repositories {
        mavenCentral()
        google()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:8.10.1")
    }
}

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"
}

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

def resolveReactNativeDirectory() {
    def reactNativeLocation = safeExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
    if (reactNativeLocation != null) {
        return file(reactNativeLocation)
    }

    // Fallback to node resolver for custom directory structures like monorepos.
    def reactNativePackage = file(
            providers.exec {
                workingDir(rootDir)
                commandLine("node", "--print", "require.resolve('react-native/package.json')")
            }.standardOutput.asText.get().trim()
    )

    if (reactNativePackage.exists()) {
        return reactNativePackage.parentFile
    }

    throw new Exception(
            "[react-native-get-location] Unable to resolve react-native location in " +
                    "node_modules. You should add project extension property (in app/build.gradle) " +
                    "`REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
    )
}

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

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

def reactNativeArchitectures() {
    def value = project.getProperties().get("reactNativeArchitectures")
    return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def REACT_NATIVE_DIR = resolveReactNativeDirectory()

def reactProperties = new Properties()
file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.split("\\.")[1].toInteger()

repositories {
    mavenCentral()
}

android {
    compileSdk safeExtGet('compileSdkVersion', 35)

    namespace = "com.github.douglasjunior.reactNativeGetLocation"
    buildFeatures {
        buildConfig = true
        prefab = true
    }

    // Used to override the NDK path/version on internal CI or by allowing
    // users to customize the NDK path/version from their root project (e.g. for M1 support)
    if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 24)
        targetSdkVersion safeExtGet('targetSdkVersion', 35)
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
        buildConfigField "int", "REACT_NATIVE_MINOR_VERSION", REACT_NATIVE_MINOR_VERSION.toString()

        if (isNewArchitectureEnabled()) {
            var appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
            externalNativeBuild {
                cmake {
                    cppFlags "-O2", "-frtti", "-fexceptions", "-Wall", "-Werror", "-std=c++20", "-DANDROID"
                    arguments "-DREACT_NATIVE_DIR=${REACT_NATIVE_DIR}",
                            "-DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}",
                            "-DANDROID_STL=c++_shared",
                            "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
                    abiFilters(*reactNativeArchitectures())
                }
            }
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        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
            exclude "**/libreact_render*.so"
            exclude "**/libreactnative.so"
            exclude "**/libjsi.so"
            exclude "**/libc++_shared.so"
        }

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

dependencies {
    implementation 'com.facebook.react:react-native:+' // from node_modules
}
