import com.moengage.gradle.android.library.plugin.configs.BuildConfigType
import groovy.json.JsonSlurper

ext {
    kotlinVersion = "2.3.20"
}

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "com.moengage.android.hybrid.module.config.plugin:com.moengage.android.hybrid.module.config.plugin.gradle.plugin:2.0.2"
    }
}

apply plugin: 'com.moengage.android.hybrid.module.config.plugin'

// In AGP 9+ built-in Kotlin's default is true
def builtInKotlin = providers.gradleProperty("android.builtInKotlin")
        .map { it.toBoolean() }
        .orElse(true)
        .get()
if (!builtInKotlin) {
    apply plugin: 'kotlin-android'
}

hybridModuleConfig.configurePlugin {
    kotlinOptions {
        enableJvmTarget = false
        compilerArgs = ['-Xjvm-default=all']
    }
    buildFeature {
        buildConfigField(
            BuildConfigType.STRING,
            "MOENGAGE_REACT_EXPO_LIB_VERSION",
            "\"${getExpoPluginVersion().toString()}\""
        )
    }
}

android {
    namespace "expo.modules.moengage"
    compileSdk rootProject.ext.compileSdkVersion
}

dependencies {
    implementation(project(':expo-modules-core'))
    implementation(project(':react-native-moengage'))
    compileOnly("host.exp.exponent:expo.modules.notifications:+")
    compileOnly('com.google.firebase:firebase-messaging:25.1.2')
    compileOnly('com.facebook.react:react-native')
    compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

def getExpoPluginVersion() {
    String libraryVersionName = "UNKNOWN"
    File packageJson = new File(project.projectDir.parentFile, "package.json")

    if (packageJson.exists()) {
        def parsedJson = new JsonSlurper().parseText(packageJson.text)
        libraryVersionName = parsedJson.version
    }

    return libraryVersionName
}