buildscript {
  def packageJson = new groovy.json.JsonSlurper().parse(file('../package.json'))
  ext.Jecnaapi = [
    kotlinVersion: "2.0.21",
    minSdkVersion: 24,
    compileSdkVersion: 36,
    jecnaApiVersion: packageJson.jecnaApiVersion
  ]

  ext.getExtOrDefault = { prop ->
    if (rootProject.ext.has(prop)) {
      return rootProject.ext.get(prop)
    }

    return Jecnaapi[prop]
  }

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.7.2"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
  }
}

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

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply from: '../nitrogen/generated/android/jecnaapi+autolinking.gradle'

apply plugin: "com.facebook.react"

android {
  namespace "cz.jzitnik.jecnaapi_react_native"

  compileSdkVersion getExtOrDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrDefault("minSdkVersion")

    externalNativeBuild {
      cmake {
        cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        abiFilters (*reactNativeArchitectures())

        buildTypes {
          debug {
            cppFlags "-O1 -g"
          }
          release {
            cppFlags "-O2"
          }
        }
      }
    }
  }

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

  packaging {
    resources {
      excludes += [
        "META-INF",
        "META-INF/**"
      ]
    }

    jniLibs {
      excludes += [
        "**/libc++_shared.so",
        "**/libfbjni.so",
        "**/libjsi.so",
        "**/libfolly_json.so",
        "**/libfolly_runtime.so",
        "**/libglog.so",
        "**/libhermes.so",
        "**/libhermes-executor-debug.so",
        "**/libhermes_executor.so",
        "**/libreactnative.so",
        "**/libreactnativejni.so",
        "**/libturbomodulejsijni.so",
        "**/libreact_nativemodule_core.so",
        "**/libjscexecutor.so"
      ]
    }
  }

  buildFeatures {
    prefab true
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }
}

dependencies {
  implementation "com.facebook.react:react-android"
  implementation project(":react-native-nitro-modules")
  implementation "io.github.tomhula:jecnaapi-core:${getExtOrDefault('jecnaApiVersion')}"
}
