apply plugin: 'com.android.library'
apply plugin: 'com.facebook.react'

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

def safeFlagGet(envProp, gradleProp) {
    def value = System.getenv(envProp)
    if (value == null && project.hasProperty(gradleProp)) {
        value = project.property(gradleProp)
    }
    if (value == null && rootProject.hasProperty(gradleProp)) {
        value = rootProject.property(gradleProp)
    }
    def normalizedValue = value?.toString()?.trim()
    return normalizedValue != null && (normalizedValue.equalsIgnoreCase('true') || normalizedValue == '1')
}

def oneSignalVersion = '5.9.6'
def oneSignalDisableLocation = safeFlagGet('ONESIGNAL_DISABLE_LOCATION', 'onesignal.disableLocation')

android {
    namespace "com.onesignal.rnonesignalandroid"
    compileSdkVersion safeExtGet('compileSdkVersion', 34)

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
    }
    buildTypes {
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}

dependencies {
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"

    // api is used instead of implementation so the parent :app project can access any of the OneSignal Java
    //   classes if needed. Such as com.onesignal.NotificationExtenderService
    //
    // Exclude OkHttp from OneSignal's transitive deps: the otel module pulls in OkHttp 5.x
    // (via opentelemetry-exporter-sender-okhttp) which is binary-incompatible with React Native's
    // networking stack (okhttp3.internal.Util removed in 5.x). React Native already provides OkHttp 4.x.
    if (oneSignalDisableLocation) {
        api("com.onesignal:core:${oneSignalVersion}") {
            exclude group: 'com.squareup.okhttp3', module: 'okhttp'
        }
        api("com.onesignal:notifications:${oneSignalVersion}") {
            exclude group: 'com.squareup.okhttp3', module: 'okhttp'
        }
        api("com.onesignal:in-app-messages:${oneSignalVersion}") {
            exclude group: 'com.squareup.okhttp3', module: 'okhttp'
        }
    } else {
        api("com.onesignal:OneSignal:${oneSignalVersion}") {
            exclude group: 'com.squareup.okhttp3', module: 'okhttp'
        }
    }

    testImplementation 'junit:junit:4.12'
}
