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:7.0.3")
    }
  }
}

plugins {
  id "io.invertase.gradle.build" version "1.5"
}

// 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 notifeeDir = findNodeModulePath(projectDir, "@notifee/react-native") ?: "$rootDir/../node_modules/@notifee/react-native"
println ":${project.name} @notifee/react-native found at $notifeeDir"

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

project.ext {
  set('react-native', [
    versions: [
      android: [
        minSdk    : 20,
        targetSdk : 33,
        compileSdk: 34,
        // optional as gradle.buildTools comes with one by default
        // overriding here though to match the version RN uses
        // buildTools: "29.0.2"
      ],
    ],
  ])
}

android {
  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
  // Check AGP version for backward compatibility w/react-native versions still on gradle plugin 6
  if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
    namespace = "io.invertase.notifee"
  }

  defaultConfig {
    multiDexEnabled true
  }
  lintOptions {
    disable 'GradleCompatible'
    abortOnError false
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  buildTypes {
    release {
      consumerProguardFiles 'proguard-rules.pro'
    }
  }
  compileSdkVersion = 34
}

repositories {
  google()
  mavenCentral()
}

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

  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'
}

ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")
rootProject.allprojects {
  repositories {
    maven {
      url "$notifeeDir/android/libs"
    }
  }
}
