apply plugin: "com.android.library"
apply plugin: "org.jetbrains.kotlin.android"

def reactNativeArchitectures() {
  def value = rootProject.getProperties().get("reactNativeArchitectures")
  return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def getExtOrDefault(name, defaultValue) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
}

android {
  namespace "com.margelo.nitro.buffer"
  ndkVersion getExtOrDefault("ndkVersion", "27.1.12297006")
  compileSdkVersion getExtOrDefault("compileSdkVersion", 34)

  defaultConfig {
    minSdkVersion getExtOrDefault("minSdkVersion", 21)
    targetSdkVersion getExtOrDefault("targetSdkVersion", 34)

    externalNativeBuild {
      cmake {
        cppFlags "-fexceptions -frtti -std=c++20"
        arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        abiFilters (*reactNativeArchitectures())
      }
    }
  }

  buildFeatures {
    buildConfig true
    prefab true
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

  sourceSets {
    main {
      jniLibs.srcDirs += ['libs']
    }
  }

  packagingOptions {
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/libc++_shared.so",
            "**/libfbjni.so",
            "**/libjsi.so",
            "**/libreactnative.so",
            "**/libreact_nativemodule_core.so",
            "**/libturbomodulejsijni.so",
            "**/libhermes.so",
            "**/libhermes_executor.so"
    ]
  }
}

dependencies {
  // Use project reference for Nitro Modules to ensure autolinking works correctly in this repo
  implementation project(":react-native-nitro-modules")
  
  // Use react-android instead of react-native for better compatibility with 0.71+
  implementation "com.facebook.react:react-android"
}

// Apply the Nitrogen-generated Gradle file
apply from: "../nitrogen/generated/android/NitroBuffer+autolinking.gradle"
