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

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

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

def _ext = rootProject.ext

def _reactNativeVersion = _ext.has('reactNative') ? _ext.reactNative : '+'
def _compileSdkVersion = _ext.has('compileSdkVersion') ? _ext.compileSdkVersion : 30
def _buildToolsVersion = _ext.has('buildToolsVersion') ? _ext.buildToolsVersion : '30.0.2'
def _minSdkVersion = _ext.has('minSdkVersion') ? _ext.minSdkVersion : 21
def _targetSdkVersion = _ext.has('targetSdkVersion') ? _ext.targetSdkVersion : 30
def _junitVersion = _ext.has('junitVersion') ? _ext.junitVersion : '4.13.2'
def _mockitoVersion = _ext.has('mockitoVersion') ? _ext.mockitoVersion : '3.2.0'
def _androidTestRunnerVersion = _ext.has('androidTestRunnerVersion') ? _ext.androidTestRunnerVersion : '1.1.0'

buildscript {
    // buildscript is evaluated before any other task is executed, so this must be defined here
    ext._kotlinVersion = rootProject.ext.has("kotlinVersion") ?  rootProject.ext.get('kotlinVersion') : '1.8.10'

    repositories {
        mavenCentral()
    }

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

android {
    compileSdkVersion _compileSdkVersion
    buildToolsVersion _buildToolsVersion

    // Conditional for compatibility with AGP <4.2.
    if (project.android.hasProperty("namespace")) {
        namespace = "com.shopify.reactnative.flash_list"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        debug.java.srcDirs += 'src/debug/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'

        if (!isNewArchitectureEnabled()) {
            main.java.srcDirs += ['src/paper/java']
        }
    }

    defaultConfig {
        minSdkVersion _minSdkVersion
        targetSdkVersion _targetSdkVersion
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    lintOptions {
        abortOnError false
    }

    testOptions {
      unitTests.returnDefaultValues = true
    }
}

dependencies {
    compileOnly "com.facebook.react:react-native:${_reactNativeVersion}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${_kotlinVersion}"
    testImplementation "junit:junit:${_junitVersion}"
    testImplementation "org.mockito.kotlin:mockito-kotlin:${_mockitoVersion}"
    testImplementation "org.mockito:mockito-inline:${_mockitoVersion}"
    testImplementation 'com.google.code.gson:gson:2.8.9'
    androidTestImplementation("androidx.test:runner:${_androidTestRunnerVersion}")
    androidTestImplementation("androidx.test:rules:${_androidTestRunnerVersion}")
}
