buildscript {
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath("com.android.tools.build:gradle:8.1.4")
            classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0")
        }
    }
}

def moduleLibsDir = file("$projectDir/src/main/libs")
rootProject.allprojects {
    repositories {
        flatDir {
            dirs moduleLibsDir
        }
    }
}

if (project == rootProject) {
    allprojects {
        repositories {
            google()
            mavenCentral()
            maven { url 'https://www.jitpack.io' }
        }
    }
}

// Only apply library and React Native plugins if this script is NOT running as the root project.
// This prevents conflicts when running ./gradlew installDebug for the standalone test app!
if (project != rootProject) {
    apply plugin: 'com.android.library'
    apply plugin: 'org.jetbrains.kotlin.android'
    apply plugin: 'com.facebook.react'

    react {
        libraryName = "RNTurboImagePickerSpec"
        root = file("../../")
        jsRootDir = file("../../src")
    }

    android {
        namespace "com.rnturboimagepicker.react"
        compileSdkVersion 34
        
        defaultConfig {
            minSdkVersion 24
            targetSdkVersion 34
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17
        }
        kotlinOptions {
            jvmTarget = "17"
        }
        
        buildFeatures {
            buildConfig false
        }
        
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }

        sourceSets {
            main {
                java {
                    def newArchEnabled = project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
                    if (newArchEnabled) {
                        srcDirs = [
                            "src/newarch/java",
                        ]
                    } else {
                        srcDirs = [
                            "src/oldarch/java",
                        ]
                    }
                }
            }
        }
    }

    dependencies {
        implementation files('src/main/libs/rnturboimagepicker-release.aar')
        compileOnly "com.facebook.react:react-android:+"
        
        implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.0"
        implementation "androidx.core:core-ktx:1.12.0"
        implementation "androidx.appcompat:appcompat:1.6.1"
        implementation "androidx.activity:activity-ktx:1.8.2"
        implementation "androidx.fragment:fragment-ktx:1.6.2"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
        implementation "com.google.android.material:material:1.11.0"
        implementation "androidx.recyclerview:recyclerview:1.3.2"
        implementation "androidx.cardview:cardview:1.0.0"
        implementation "androidx.coordinatorlayout:coordinatorlayout:1.2.0"
        implementation "com.github.bumptech.glide:glide:4.16.0"
        implementation "androidx.exifinterface:exifinterface:1.3.7"
        
        // CameraX dependencies
        def camerax_version = "1.3.1"
        implementation "androidx.camera:camera-core:${camerax_version}"
        implementation "androidx.camera:camera-camera2:${camerax_version}"
        implementation "androidx.camera:camera-lifecycle:${camerax_version}"
        implementation "androidx.camera:camera-view:${camerax_version}"
        implementation "androidx.camera:camera-extensions:${camerax_version}"
    }
}
