import com.android.Version

apply plugin: "com.android.library"
apply plugin: "kotlin-android"


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

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

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

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

    return major >= 8
}

buildscript {
    // Simple helper that allows the root project to override versions declared by this library.
    ext.safeExtGet = { prop, fallback -> rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback }

    // Ensures backward compatibility
    ext.getKotlinVersion = {
        if (ext.has("kotlinVersion")) {
            ext.kotlinVersion()
        } else {
            ext.safeExtGet("kotlinVersion", "1.9.25")
        }
    }

    repositories {
        mavenCentral()
        google()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
    }
}


android {
    if (supportsNamespace()) {
        sourceSets {
            main {
                manifest.srcFile "src/main/AndroidManifestNew.xml" // no package on it
            }
        }
        namespace "com.scandit.datacapture.reactnative.core"
    }

    compileSdkVersion safeExtGet("compileSdkVersion", 33)

    sourceSets {
        main {
            if (supportsNamespace()) {
                manifest.srcFile "src/main/AndroidManifestNew.xml" // no package on it
            }
            // Architecture-specific source sets for both modules and view managers
            if (this.isNewArchitectureEnabled()) {
                kotlin.srcDirs += ['src/main/newArch/kotlin']
                java.srcDirs += ['generated/java']
            } else {
                kotlin.srcDirs += ['src/main/oldArch/kotlin']
            }
        }
    }

    defaultConfig {
        minSdkVersion safeExtGet("minSdkVersion", 24)
        targetSdkVersion safeExtGet("targetSdkVersion", 33)
        versionCode 1
        versionName "1.0"
    }

    lintOptions {
        abortOnError false
    }
}

if (isNewArchitectureEnabled()) {
    react {
        codegenJavaPackageName = "com.scandit.datacapture.reactnative.core"
    }
}

if (file( "${rootProject.projectDir}/build-test.gradle").exists()) {
    apply from: "${rootProject.projectDir}/build-test.gradle"
}

dependencies {
    def sdk_version = "8.3.0"

    println("Version of the native sdk used in this build: ${safeExtGet('global_sdk_version', sdk_version)}")
    if (findProject(':scandit-datacapture-frameworks-core') != null) {
        api project(":scandit-datacapture-frameworks-core")
    } else {
        api "com.scandit.datacapture.frameworks:core:${safeExtGet('global_sdk_version', sdk_version)}"
    }

    // For < 0.71, this will be from the local maven repo
    // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
}
