buildscript {
    // Only needed for standalone builds - consuming apps provide their own AGP version
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            // AGP 8.1.4 is compatible with Gradle 8.10.2 and requires Java 17
            classpath 'com.android.tools.build:gradle:8.1.4'
            classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.20'
        }
    }
}

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

// Determine if New Architecture is enabled
def isNewArchitectureEnabled() {
    // Check various possible property locations - rootProject for consuming apps
    if (rootProject.hasProperty("newArchEnabled")) {
        return rootProject.property("newArchEnabled").toString() == "true"
    }
    // Fallback to local project property
    if (project.hasProperty("newArchEnabled")) {
        return project.property("newArchEnabled").toString() == "true"
    }
    return false
}

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

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

android {
    namespace "com.reactnativeportalmobilempc"

    packagingOptions {
        exclude 'AndroidManifest.xml'
    }
    compileSdk safeExtGet('PortalMobileMpc_compileSdkVersion', 35)

    defaultConfig {
        minSdk safeExtGet('PortalMobileMpc_minSdkVersion', 24)
        targetSdk safeExtGet('PortalMobileMpc_targetSdkVersion', 35)
        versionCode 1
        versionName "1.0"
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    lint {
        disable 'GradleCompatible'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    // Configure source sets for Old/New Architecture
    sourceSets {
        main {
            java {
                if (isNewArchitectureEnabled()) {
                    srcDirs += 'src/newarch/java'
                } else {
                    srcDirs += 'src/oldarch/java'
                }
            }
        }
    }

    buildFeatures {
        buildConfig = true
    }
}

repositories {
    mavenLocal()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url("$rootDir/../node_modules/react-native/android")
    }
    google()
    mavenCentral()
}

dependencies {
    //noinspection GradleDynamicVersion
    compileOnly fileTree(dir: 'libs', include: ['*.aar'])
    implementation "com.facebook.react:react-native:+"  // From node_modules
}
