 import java.nio.file.Paths

buildscript {
  // The Android Gradle plugin is only required when opening the android folder stand-alone.
  // This avoids unnecessary downloads and potential conflicts when the library is included as a
  // module dependency in an application project.
  if (project == rootProject) {
    repositories {
      google()
      mavenCentral()
    }

    dependencies {
      classpath("com.android.tools.build:gradle:8.8.2")
      classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21")
    }
  }
}

plugins {
  id "com.android.library"
  id "com.facebook.react"
  id "org.jetbrains.kotlin.android"
}

// https://github.com/facebook/react-native/blob/a70354df12ef71aec08583cca4f1fed5fb77d874/ReactAndroid/build.gradle#L168-L201
def findNodeModulePath(baseDir, packageName) {
  def basePath = baseDir.toPath().normalize()
  // Node's module resolution algorithm searches up to the root directory,
  // after which the base path will be null
  while (basePath) {
    def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
    if (candidatePath.toFile().exists()) {
      return candidatePath.toString()
    }
    basePath = basePath.getParent()
  }
  return null
}

def reactNativeDir = findNodeModulePath(projectDir, "react-native") ?: "$rootDir/../../node_modules/react-native"
def codegenDir = findNodeModulePath(projectDir, "@react-native/codegen") ?: "$rootDir/../../node_modules/@react-native/codegen"
def notifeeDir = findNodeModulePath(projectDir, "@psync/notifee") ?: "$rootDir/../node_modules/@psync/notifee"
println ":${project.name} @psync/notifee found at $notifeeDir"

def notifeeLibsDir = new File("$notifeeDir/android/libs")
def hasNotifeeCore = notifeeLibsDir.exists() && notifeeLibsDir.isDirectory()

if (project.hasProperty('reactNativeProjects')) {
  reactNativeProjects.each { dependent ->
    project.evaluationDependsOn(dependent)
  }
} else {
  project.evaluationDependsOn(':app')
}

project.ext {
  set('react-native', [
    versions: [
      android: [
        minSdk    : 28,
        targetSdk : 35,
        compileSdk: 36,
      ],
    ],
  ])
}

android {
  namespace = "io.invertase.notifee"
  compileSdk = 36

  defaultConfig {
    minSdk = 28
    targetSdk = 35
    multiDexEnabled = true
  }

  lint {
    disable += 'GradleCompatible'
    abortOnError = false
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

  sourceSets {
    main {
      kotlin.srcDirs += 'src/main/kotlin'
    }
  }

  buildTypes {
    release {
      consumerProguardFiles 'proguard-rules.pro'
    }
  }
}

repositories {
  google()
  mavenCentral()
}

react {
  reactNativeDir = file(reactNativeDir)
  codegenDir = file(codegenDir)
}

dependencies {
  if (findProject(':notifee_core')) {
    implementation findProject(':notifee_core')
  } else if (hasNotifeeCore) {
    implementation(group: 'app.notifee', name:'core', version: '202108261754')
  }

  implementation("com.google.guava:guava:33.3.1-android") // https://github.com/google/guava
  implementation 'androidx.concurrent:concurrent-futures:1.1.0' // https://developer.android.com/jetpack/androidx/releases/concurrent
  implementation 'androidx.work:work-runtime:2.8.0' // https://developer.android.com/jetpack/androidx/releases/work
  implementation 'org.greenrobot:eventbus:3.3.1' // https://github.com/greenrobot/EventBus/releases
  implementation 'androidx.lifecycle:lifecycle-process:2.3.1'

  //noinspection GradleDynamicVersion
  implementation 'com.facebook.react:react-android:+'
}

rootProject.allprojects {
  repositories {
    if (hasNotifeeCore) {
      maven {
        url = "$notifeeDir/android/libs"
      }
    }
  }
}
