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

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

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

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

// Read version from package.json
def getVersionFromPackageJson() {
    def packageJsonFile = file("$projectDir/../package.json")
    def packageJson = new groovy.json.JsonSlurper().parseText(packageJsonFile.text)
    return packageJson.version
}

android {
    def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
    // Check AGP version for backward compatibility reasons (for RN < 0.71)
    if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
        namespace "com.eko"
    }

    compileSdkVersion safeExtGet('compileSdkVersion', 36)

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 24)
        targetSdkVersion safeExtGet('targetSdkVersion', 36)
        versionCode 1
        versionName '1.0'

        // Support for 16KB memory page sizes (Android 15+)
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }

        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
        buildConfigField "String", "LIBRARY_VERSION", "\"${getVersionFromPackageJson()}\""
    }

    buildFeatures {
        buildConfig true
    }

    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch/java']
            } else {
                java.srcDirs += ['src/oldarch/java']
            }
        }
    }

    // Configure packaging options for 16KB page size support
    packagingOptions {
        jniLibs {
            useLegacyPackaging = false
        }
    }
}

repositories {
    mavenCentral()
    google()
}

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

    implementation 'com.google.code.gson:gson:2.12.1'
}
