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

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10" // Kotlin plugin
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android' // Apply Kotlin Android plugin

def includeConnector = project.hasProperty('appsflyer.enable_purchase_connector') ? project.property('appsflyer.enable_purchase_connector').toBoolean() : false // Moved outside of android block

android {
    namespace "com.appsflyer.reactnative"
    compileSdkVersion safeExtGet('compileSdkVersion', 34)
    buildToolsVersion safeExtGet('buildToolsVersion', '34.0.0')

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 34)
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }

        buildConfigField 'boolean', 'INCLUDE_CONNECTOR', includeConnector.toString()
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src/main/kotlin'] // Add Kotlin source directory
            java.srcDirs += includeConnector ? ['src/main/includeConnector'] : ['src/main/excludeConnector']
        }
    }

    lintOptions {
        warning 'InvalidPackage'
    }
    testOptions {
        unitTests.includeAndroidResources = true
        unitTests.returnDefaultValues = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions { // Configure Kotlin-specific options
        jvmTarget = "17"
    }
}

repositories {
    mavenCentral()
    maven { url "https://maven.google.com" }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.10" // Add Kotlin standard library
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
    api "com.appsflyer:af-android-sdk:${safeExtGet('appsflyerVersion', '6.17.3')}"
    implementation "com.android.installreferrer:installreferrer:${safeExtGet('installReferrerVersion', '2.2')}"
    if (includeConnector){
        implementation 'com.appsflyer:purchase-connector:2.1.1'
    }
}