// android/build.gradle
//
// Modernized for React Native 0.85:
//
//   - Applies the `com.facebook.react` plugin so RN's codegen runs against
//     our TurboModule spec in `../src/` and generates the Java abstract base
//     class our Kotlin module inherits from plus the C++ JNI glue the New
//     Architecture autolinking links against. Codegen is configured by the
//     `react { ... }` block below. `jsRootDir` MUST point at the isolated
//     `src/` dir (not the package root) so codegen scans only our spec and
//     not the sibling `example/` app's node_modules.
//   - Applies the Kotlin plugin so `UserLeapModule.kt` compiles.
//   - Sets `namespace` (required by Android Gradle Plugin 8+).
//   - Targets Java 17 / Kotlin (matches RN 0.85's host-app build).
//
// Versions for compileSdk / buildTools / NDK / Java are pulled from the
// host application's rootProject.ext via safeExtGet, so consumers can
// override them centrally without editing this file.

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

buildscript {
    // Standalone-only block: dependencies here only apply when the
    // wrapper is opened directly in Android Studio. Consuming apps
    // provide their own AGP / Kotlin classpath via their root build.
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:8.7.2"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.20"
        }
    }
}

apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'com.facebook.react'

android {
    namespace "com.userleap.reactnative"
    compileSdkVersion safeExtGet('compileSdkVersion', 36)
    buildToolsVersion safeExtGet('buildToolsVersion', '35.0.0')

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 24)
        targetSdkVersion safeExtGet('targetSdkVersion', 36)
    }

    lintOptions {
        abortOnError false
    }
}

react {
    jsRootDir = file("../src/")
    libraryName = "RNUserLeapBindingsSpec"
    codegenJavaPackageName = "com.userleap.reactnative"
}

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

dependencies {
    implementation 'com.facebook.react:react-native:+'
    implementation "androidx.webkit:webkit:1.8.0"

    // Use conditional logic for local vs Maven
    if (findProject(':userleap-android-sdk') != null) {
        implementation project(':userleap-android-sdk')
    } else {
        implementation ("com.userleap:userleap-android-sdk:2.27.0") // update this version on android updates
    }
}
