buildscript {
  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.13.2"
  }
}

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/ReactNativeList+autolinking.gradle'
apply from: "./fix-prefab.gradle"

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

    react {
            libraryName = "ReactNativeList"
            codegenJavaPackageName = "com.hannojg.reactnativelist"
            jsRootDir = file("../src")
        }
}

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

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

def getAppExtOrDefault(name, fallback) {
  def appProject = rootProject.allprojects.find { candidate ->
    return candidate.plugins.hasPlugin("com.android.application")
  }
  if (appProject == null) {
    return fallback
  }

  def extraProperties = appProject.extensions.extraProperties
  if (!extraProperties.has(name)) {
    return fallback
  }

  return extraProperties.get(name)
}

def resolveReactNativeDirectory() {
  def configuredDirectory = getAppExtOrDefault("REACT_NATIVE_NODE_MODULES_DIR", null)
  if (configuredDirectory != null) {
    return file(configuredDirectory)
  }

  // Resolve from the consuming app so this also works with monorepos and non-hoisted dependencies.
  def resolution = providers.exec {
    workingDir(rootDir)
    commandLine("node", "--print", "require.resolve('react-native/package.json')")
  }
  def output = resolution.standardOutput.asText.get()
  def packagePath = output.trim()
  def packageFile = file(packagePath)
  if (!packageFile.exists()) {
    throw new GradleException(
      "[ReactNativeList] Unable to resolve react-native. Set REACT_NATIVE_NODE_MODULES_DIR in app/build.gradle."
    )
  }

  return packageFile.parentFile
}

def reactNativeDirectory = resolveReactNativeDirectory()
def reactNativePath = reactNativeDirectory.absolutePath
def reactNativeCMakePath = reactNativePath.replace("\\", "/")

android {
  namespace "com.hannojg.reactnativelist"
  ndkVersion getExtOrDefault("ndkVersion")
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    consumerProguardFiles "consumer-rules.pro"
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

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

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

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

  packagingOptions {
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/libc++_shared.so",
            "**/libNitroModules.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 {
    buildConfig true
    prefab true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

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

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

}

repositories {
  mavenCentral()
  google()
}


dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"

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

  // RecyclerView
  implementation "androidx.recyclerview:recyclerview:1.4.0"
}
