// Resolve the host app's react-native package.json (app/android -> app -> node_modules).
def reactNativeMinorVersion() {
    def dir = rootProject.projectDir
    while (dir != null) {
        def pkg = new File(dir, "node_modules/react-native/package.json")
        if (pkg.exists()) {
            def m = (new groovy.json.JsonSlurper().parse(pkg).version =~ /^\d+\.(\d+)/)
            return m ? m[0][1].toInteger() : 0
        }
        dir = dir.parentFile
    }
    return 0
}

// RN 0.82+ removed the legacy architecture and ignores newArchEnabled, so the module
// must follow the app or its codegen output goes missing. nsureForceLegacyArch is a
// compile-check hook for src/oldarch, not a consumer switch.
def isNewArchEnabled() {
    if (rootProject.hasProperty("nsureForceLegacyArch")) return false
    if (reactNativeMinorVersion() >= 82) return true
    return !(rootProject.hasProperty("newArchEnabled") && rootProject.newArchEnabled == "false")
}

apply plugin: 'com.android.library'
if (isNewArchEnabled()) {
    apply plugin: 'com.facebook.react'
}

android {
    namespace 'com.nsure.reactnative'
    compileSdkVersion 34

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 34
    }

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

repositories {
    google()
    mavenCentral()
    maven { url "https://jitpack.io" }
    maven { url 'https://maven.fpregistry.io/releases' }
}

// Apps resolve this module's deps with their own repositories, and RN's default
// set lacks Fingerprint's registry — inject it so autolinked builds resolve
// com.fingerprint.android:sdk without manual app-level setup.
rootProject.allprojects {
    repositories {
        maven { url 'https://maven.fpregistry.io/releases' }
    }
}

dependencies {
    implementation 'com.facebook.react:react-native:+'
    implementation 'com.github.nsure-ai:android-sdk:1.4.1'
    // The SDK's published POM declares no dependencies, so its runtime deps
    // must be declared here or consumers hit NoClassDefFoundError.
    implementation 'com.android.volley:volley:1.2.1'
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation 'com.fingerprint.android:sdk:4.0.0'
}
