// android/build.gradle
// based on:
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
//   original location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
//   original location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle
def DEFAULT_BUILD_TOOLS_VERSION = '34.0.0'
def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
apply plugin: 'com.android.library'
// apply plugin: 'maven'
apply plugin: "kotlin-android"
buildscript {
    ext.kotlin_version = '1.8.20'
    // 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 {
            mavenCentral()
            google()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
}
android {
    compileSdkVersion 34
    buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 34
        versionCode 1
        versionName "1.0"
    }
    compileOptions {
        coreLibraryDesugaringEnabled true
    }
    lintOptions {
        abortOnError false
    }
    buildFeatures {
        viewBinding true
    }

    flavorDimensions "store"
    productFlavors {
        amazon {
            dimension "store"
        }

        play {
            dimension "store"
        }
    }
}
repositories {
    mavenCentral()
    // ref: https://www.baeldung.com/maven-local-repository
    mavenLocal()
    maven { url("https://packages.namiml.com/NamiSDK/Amazon/") }
    maven { url("https://packages.namiml.com/NamiSDK/Android/") }
    maven { url 'https://jitpack.io' }
    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()
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    playImplementation "com.namiml:sdk-android:3.2.9"
    amazonImplementation "com.namiml:sdk-amazon:3.2.9"

    implementation "com.facebook.react:react-native:+"  // From node_modules
    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5"

}

configurations {
  customConfig.extendsFrom implementation
}

afterEvaluate { project ->
    // some Gradle build hooks ref:
    // https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
    android.libraryVariants.all { variant ->
        def name = variant.name.capitalize()
        def javaCompileTask = variant.javaCompileProvider.get()
        task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
            from javaCompileTask.destinationDir
        }
    }

    task installArchives(type: Upload) {
        configuration = configurations.archives
    }
}
