buildscript {
  ext.kotlinVersion = '1.9.25'

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.12.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
  }
}

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

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

apply plugin: "com.android.library"
apply plugin: "org.jetbrains.kotlin.android"
apply from: "../nitrogen/generated/android/QuickCrypto+autolinking.gradle"

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

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["QuickCrypto_" + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["QuickCrypto_" + name]).toInteger()
}

def sodiumEnabled = hasProperty('sodiumEnabled') ? project.property('sodiumEnabled').toBoolean() : false // Default to false
logger.warn("[QuickCrypto] Has libsodium ${sodiumEnabled ? "enabled" : "disabled"}!")

android {
  namespace "com.margelo.nitro.quickcrypto"
  ndkVersion getExtOrDefault("ndkVersion")
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

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

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

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

  buildFeatures {
    buildConfig true
    prefab true
    // Explicitly disable RenderScript (deprecated since Android 12, removed in AGP 9.0)
    // to suppress 'isRenderscriptDebuggable is obsolete' warning. See #802.
    renderScript = false
  }

packagingOptions {
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/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"
    ]
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
    // AGP version is constrained by RN 0.81 + Gradle 8.14.3 + JDK 17.
    // AGP 9.x requires Gradle 9 and JDK 21 — not viable until RN bumps its toolchain.
    disable "AndroidGradlePluginVersion"
    disable "GradleDependency"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += [
          // React Codegen files
          "${project.buildDir}/generated/source/codegen/java"
        ]
      }
    }
  }
}

repositories {
  mavenCentral()
  google()
}


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

  // Add a dependency on NitroModules
  implementation project(":react-native-nitro-modules")

  // Add a dependency on OpenSSL
  implementation 'io.github.ronickg:openssl:3.6.2-1'

  if (sodiumEnabled) {
    // Add a dependency on libsodium
    implementation 'io.github.ronickg:sodium:1.0.22-1'
  }
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "QuickCrypto"
    codegenJavaPackageName = "com.margelo.nitro.quickcrypto"
  }
}
