import groovy.json.JsonSlurper

buildscript {
  ext.getExtOrDefault = {name ->
    return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['LoaderKit_' + name]
  }

  repositories {
    google()
    mavenCentral()
  }

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

// React Native 0.82 and newer may not define newArchEnabled at all; only an explicit false is rejected.
if (rootProject.findProperty("newArchEnabled")?.toString() == "false") {
  throw new GradleException(
    "react-native-loader-kit 5 requires the New Architecture. " +
      "For the old architecture, install react-native-loader-kit@v4-lts."
  )
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "com.facebook.react"

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

// The core engine version is the one the JavaScript package depends on, so JS, iOS and Android
// always render the same spec revision.
def loaderKitCoreVersion = {
  def pkg = new JsonSlurper().parse(file("../package.json"))
  def version = pkg.dependencies?.get("@loader-kit/spec")
  if (!(version ==~ /\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?/)) {
    throw new GradleException(
      "react-native-loader-kit: package.json must depend on an exact @loader-kit/spec version, found '${version}'."
    )
  }
  return version
}()

android {
  namespace "com.loaderkit"

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lint {
    disable "GradleCompatible"
  }

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

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
  compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
}

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation "com.facebook.react:react-android"
  implementation "io.github.maitrungduc1410:loaderkit-core:${loaderKitCoreVersion}"
}

react {
  jsRootDir = file("../src/")
  libraryName = "LoaderKitView"
  codegenJavaPackageName = "com.loaderkit"
}
