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

def isNewArchitectureEnabled() {
    return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

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

if (isNewArchitectureEnabled()) {
    react {
        jsRootDir = file("../src/")
        libraryName = "WEInboxReact"
        codegenJavaPackageName = "com.webengage.inbox.react"
    }
}

android {
    compileSdk 35
    buildToolsVersion '35.0.0'
    namespace "com.webengage.we_notificationinbox_rn"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 35
        manifestPlaceholders = [we_rn_ni_version: readVersion()]
        buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
    }

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

    lint {
        abortOnError false
    }
}

repositories {
    mavenCentral()
    google()
}

String readVersion() {
    def version = new Properties()
    def stream
    try {
        stream = new FileInputStream(new File(projectDir, 'version.properties'))
        version.load(stream)
    } catch (FileNotFoundException ignore) {
    } finally {
        if (stream != null) stream.close()
    }
    // safety defaults in case file is missing
    if(!version['major']) version['major'] = "1"
    if(!version['minor']) version['minor'] = "0"
    if(!version['patch']) version['patch'] = "0"
    return "${version['major']}.${version['minor']}.${version['patch']}"
}

dependencies {
    compileOnly 'com.facebook.react:react-native:+'
    api 'com.webengage:android-sdk:[4.4,)'
    api 'com.webengage:we-notification-inbox:[1,)'
    implementation 'androidx.core:core-ktx:1.9.0'
    compileOnly 'androidx.annotation:annotation:1.2.0'
    
    if (isNewArchitectureEnabled()) {
        implementation 'com.facebook.react:react-android'
        implementation 'com.facebook.react:hermes-android'
    }
}
