def DEFAULT_COMPILE_SDK_VERSION = 32
def DEFAULT_BUILD_TOOLS_VERSION = '30.0.3'
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 32

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

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

buildscript {

    ext {
        kotlinVersion = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : '1.6.0'
    }
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    // ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
            maven {
                url "https://maven.tealiumiq.com/android/releases/"
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:4.2.2'
        }
    }

    repositories {
        mavenCentral()
        maven {
            url "https://maven.tealiumiq.com/android/releases/"
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${project.ext.kotlinVersion}"
    }
}

version = "2.6.2"
android {
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)

        buildConfigField "String", "TAG", "\"Tealium-React-$version\""

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        abortOnError false
    }

    if (project.android.hasProperty("namespace")) {
        namespace "com.tealium.react"
    }

    def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
    if(agpVersion < 8) {        
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }

        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_11
        }
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    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"
    }

    maven {
        url "https://maven.tealiumiq.com/android/releases/"
    }
    google()
    mavenCentral()
    maven {
        url "https://maven.tealiumiq.com/android/releases/"
    }
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules

    //Tealium
    implementation 'com.tealium:kotlin-core:1.9.1'
    implementation 'com.tealium:kotlin-collect-dispatcher:1.1.3'
    implementation 'com.tealium:kotlin-tagmanagement-dispatcher:1.2.5'
    implementation 'com.tealium:kotlin-remotecommand-dispatcher:1.5.2'
    implementation 'com.tealium:kotlin-lifecycle:1.2.2'
    implementation 'com.tealium:kotlin-visitor-service:1.2.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${project.ext.kotlinVersion}"

    testImplementation 'com.facebook.react:react-native:+'
    testImplementation 'io.mockk:mockk:1.13.3'
    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.robolectric:robolectric:4.11.1"
}
