buildscript {
  ext.DocumentScanner = [
    kotlinVersion: "2.0.21",
    minSdkVersion: 24,
    compileSdkVersion: 36
  ]

  ext.getExtOrDefault = { prop ->
    if (rootProject.ext.has(prop)) {
      return rootProject.ext.get(prop)
    }

    return DocumentScanner[prop]
  }

  repositories {
    google()
    mavenCentral()
  }

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


apply plugin: "com.android.library"
// AGP 9 ships built-in Kotlin support and registers the `kotlin` extension
// itself. Applying the Kotlin plugin on top of it fails configuration with
// "Cannot add extension with name 'kotlin'". Only apply it when nothing has
// registered that extension yet.
if (project.extensions.findByName('kotlin') == null) {
  apply plugin: "kotlin-android"
}

apply plugin: "com.facebook.react"

def parseFeatureList = { rawValue ->
  def raw = (rawValue ?: "").toString().toLowerCase()
  def values = raw.split(",")
    .collect { it.trim() }
    .findAll { !it.isEmpty() }
    .toSet()

  if (values.contains("none")) {
    values = values - "none"
  }

  if (values.contains("all")) {
    values += ["barcode", "text", "tables"]
  }

  return values
}

def analysisFeaturesRaw =
  (project.findProperty("DocumentScanner_analysisFeatures")
    ?: "")

def analysisFeatures = parseFeatureList(analysisFeaturesRaw)
def barcodeFeatureEnabled = analysisFeatures.contains("barcode")
def textFeatureEnabled = analysisFeatures.contains("text")
def tablesFeatureEnabled = analysisFeatures.contains("tables")
def textPipelineEnabled = textFeatureEnabled || tablesFeatureEnabled

android {
  namespace "com.preeternal.scanner"

  compileSdkVersion getExtOrDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrDefault("minSdkVersion")
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  sourceSets {
    main {
      if (barcodeFeatureEnabled) {
        java.srcDirs += ["src/barcode/java"]
      } else {
        java.srcDirs += ["src/no-barcode/java"]
      }
      if (textPipelineEnabled) {
        java.srcDirs += ["src/text/java"]
      } else {
        java.srcDirs += ["src/no-text/java"]
      }
    }
  }
}

dependencies {
  implementation "com.facebook.react:react-android"
  implementation "com.google.android.gms:play-services-mlkit-document-scanner:16.0.0"

  if (barcodeFeatureEnabled) {
    implementation "com.google.mlkit:barcode-scanning:17.3.0"
    implementation "androidx.exifinterface:exifinterface:1.4.2"
  }
  if (textPipelineEnabled) {
    implementation "com.google.mlkit:text-recognition:16.0.1"
  }
}
