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

  ext.getExtOrDefault = { prop ->
    if (rootProject.ext.has(prop)) {
      return rootProject.ext.get(prop)
    }
    return MediaToolkit[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"
apply plugin: "kotlin-android"
apply plugin: "com.facebook.react"

// Apply nitrogen-generated autolinking (adds generated Kotlin sources & C++ sources)
apply from: '../nitrogen/generated/android/MediaToolkit+autolinking.gradle'
// Remove safe-area-context codegen files generated by the React Native Gradle Plugin.
// The RN plugin scans all node_modules and generates Java stubs for every RN native module,
// including react-native-safe-area-context. These stubs conflict with those generated by
// safe-area-context itself in the example app, causing duplicate DEX errors.
// Solution: exclude ALL safe-area generated files from this library's compilation.
afterEvaluate {
  android.libraryVariants.all { variant ->
    variant.javaCompileProvider.configure {
      exclude '**/RNCSafeAreaProviderManagerDelegate.java'
      exclude '**/RNCSafeAreaViewManagerDelegate.java'
      exclude '**/RNCSafeAreaProviderManager.java'
      exclude '**/RNCSafeAreaViewManager.java'
      exclude '**/RNCSafeAreaProviderManagerInterface.java'
      exclude '**/RNCSafeAreaViewManagerInterface.java'
    }
  }
}

// Exclude react-android from the library's RUNTIME classpath only.
// react-android is pulled in transitively by react-native-nitro-modules. Its AAR contains
// pre-built safe-area view manager classes that conflict with react-native-safe-area-context.
// Using configurations.all would also exclude it from compileOnly (breaking Kotlin compile).
// We need it for compilation (compileOnly declared above) but NOT at runtime.
configurations.each { config ->
  if (config.name.contains("Runtime") || config.name.contains("runtime")) {
    config.exclude group: "com.facebook.react", module: "react-android"
  }
}

android {
  namespace "com.mediatoolkit"

  compileSdkVersion getExtOrDefault("compileSdkVersion")

  // Must match the NDK version used by react-native-nitro-modules (gradle.properties: ndkVersion=27.1.12297006)
  // Mismatched NDK versions cause undefined HybridObject symbols at link time.
  ndkVersion "27.1.12297006"

  defaultConfig {
    minSdkVersion    getExtOrDefault("minSdkVersion")
    targetSdkVersion getExtOrDefault("targetSdkVersion")
    externalNativeBuild {
      cmake {
        cppFlags "-std=c++20 -frtti -fexceptions"
        // Follow the reactNativeArchitectures Gradle property so that:
        //   - `yarn expo run:android` on a physical device only builds arm64-v8a
        //   - `yarn expo run:android --device` on an emulator only builds x86_64
        //   - a standalone library build defaults to both
        // This is the same pattern used by react-native-vision-camera.
        def architectures = findProperty("reactNativeArchitectures")?.split(",")?.collect { it.trim() } ?: ["arm64-v8a", "x86_64"]
        abiFilters(*architectures)
        arguments "-DANDROID_STL=c++_shared"
      }
    }
  }

  buildFeatures {
    buildConfig true
    prefab true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lint {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

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

}

dependencies {
  // react-android is compileOnly: needed by Kotlin to compile (ReactPackage, etc.)
  // but NOT included in the library's runtime DEX output.
  // The host app always provides react-android at runtime.
  // Note: in monorepo/composite build, the host app handles deduplication.
  compileOnly "com.facebook.react:react-android"

  // NitroModules — local node_modules project (not on Maven Central).
  // Exclude react-android from its transitive deps: react-native-nitro-modules pulls in
  // react-android transitively, which causes duplicate DEX errors (RNCSafeAreaProviderManagerDelegate)
  // when the host app also has react-native-safe-area-context.
  implementation(project(":react-native-nitro-modules")) {
    exclude group: "com.facebook.react", module: "react-android"
  }

  // Jetpack Media3 Transformer — for video trim, crop, compress
  // 1.4.0+ required for Presentation.createForCrop API
  implementation "androidx.media3:media3-transformer:1.5.1"
  implementation "androidx.media3:media3-effect:1.5.1"
  implementation "androidx.media3:media3-common:1.5.1"

  // ExifInterface — for EXIF rotation on images
  implementation "androidx.exifinterface:exifinterface:1.3.7"

  // Coroutines for background processing
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
}
