buildscript {
    ext.kotlin_version = '1.8.21'

    repositories {
         google()
         mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

def isNewArchitectureEnabled() {
    // React Native 0.80+ uses this standard detection approach
    return project.hasProperty("newArchEnabled") && project.newArchEnabled.toBoolean()
}

def supportsNamespace() {
    def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
    def major = parsed[0].toInteger()
    def minor = parsed[1].toInteger()

    // Namespace support was added in 7.3.0
    if (major == 7 && minor >= 3) {
        return true
    }

    return major >= 8
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
if (isNewArchitectureEnabled()) {
    apply plugin: 'com.facebook.react'
}

android {
    compileSdkVersion 32
    buildToolsVersion '30.0.3'

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 32
        versionCode 2
        versionName "2.0"
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    }
    lintOptions {
        abortOnError false
    }
    if (supportsNamespace()) {
        namespace 'com.rokt.reactnativesdk'
        buildFeatures {
            buildConfig = true
        }
        sourceSets {
            main {
                manifest.srcFile "src/main/AndroidManifestNew.xml"
            }
        }
    }
    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch/java']
            } else {
                java.srcDirs += ['src/oldarch/java']
            }
        }
    }
}

repositories {

        mavenLocal()

        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }

        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        mavenCentral()
        maven {
            name = "Central Portal Snapshots"
            url = uri("https://central.sonatype.com/repository/maven-snapshots/")
            mavenContent {
                snapshotsOnly()
            }
        }
}

dependencies {
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1"
    implementation ('com.rokt:roktsdk:6.0.1')
}
