buildscript {
  // Simple helper that allows the root project to override versions declared by this library.
  ext.safeExtGet = { prop, fallback ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
  }

  repositories {
    mavenCentral()
    google()
    maven { url "https://jitpack.io" }
  }
}

plugins {
   alias libs.plugins.android.library
   alias libs.plugins.kotlin.android
   alias libs.plugins.download
   id("org.jetbrains.kotlin.plugin.compose") version "2.0.0"
}
apply plugin: 'kotlin-kapt'
apply from: new File(rootDir, "versioning_linking.gradle")

def reactProperties = new Properties()
file("${project(':packages:react-native:ReactAndroid').projectDir}/gradle.properties").withInputStream { reactProperties.load(it) }

// WHEN_VERSIONING_REMOVE_FROM_HERE
//maven repository info
group = 'host.exp.exponent'
version = '45.0.0'
// WHEN_VERSIONING_REMOVE_TO_HERE

def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
apply from: expoModulesCorePlugin
applyKotlinExpoModulesCorePlugin()
useDefaultAndroidSdkVersions()
useExpoPublishing()

repositories {
  mavenCentral()
  maven { url "https://jitpack.io" }
}


apply plugin: 'com.facebook.react'

react {
  /* Autolinking */
  autolinkLibrariesWithApp()
}


android {
  namespace "host.exp.expoview"

  buildFeatures {
    buildConfig = true
  }

  composeOptions {
    kotlinCompilerExtensionVersion = "1.5.14"
  }

  // Used to override the NDK path/version on internal CI or by allowing
  // users to customize the NDK path/version from their root project (e.g. for M1 support)
  if (rootProject.hasProperty("ndkPath")) {
    ndkPath rootProject.ext.ndkPath
  }
  if (rootProject.hasProperty("ndkVersion")) {
    ndkVersion rootProject.ext.ndkVersion
  }

  buildFeatures {
    viewBinding true
    compose true
  }

  defaultConfig {
    versionCode 1
    versionName "1.0"

    /* WHEN_VERSIONING_UNCOMMENT_FROM_HERE
    missingDimensionStrategy('versioning', 'versioned')
    WHEN_VERSIONING_UNCOMMENT_TO_HERE */

    // WHEN_VERSIONING_REMOVE_FROM_HERE
    manifestPlaceholders = [
        'appAuthRedirectScheme': 'host.exp.exponent'
    ]

    buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
    buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
    buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", "true")
    // WHEN_VERSIONING_REMOVE_TO_HERE
  }

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

// WHEN_VERSIONING_REMOVE_FROM_HERE
import org.apache.tools.ant.taskdefs.condition.Os

class GenerateDynamicMacrosPlugin implements Plugin<Project> {
  @Override
  void apply(org.gradle.api.Project target) {
    target.tasks.create("generateDynamicMacros", Exec) {
      def configuration = target.gradle.startParameter.taskNames.any {
        it.toLowerCase().contains("release")
      } ? "release" : "debug"

      workingDir project.file('../../../../bin')

      if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'cmd.exe', '/c', "expotools android-generate-dynamic-macros --configuration ${configuration}"
      } else {
        commandLine "./expotools", "android-generate-dynamic-macros", "--configuration", configuration
      }
    }

    target.tasks.named("preBuild").configure {
      dependsOn("generateDynamicMacros")
    }
  }
}

apply plugin: GenerateDynamicMacrosPlugin

// WHEN_VERSIONING_REMOVE_TO_HERE

dependencies {
  api fileTree(dir: 'libs', include: ['*.jar'])
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
  implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
  implementation 'androidx.lifecycle:lifecycle-process:2.8.7'
  implementation 'androidx.compose.foundation:foundation-android:1.7.6'

  implementation 'io.coil-kt.coil3:coil-compose:3.2.0'
  implementation 'io.coil-kt.coil3:coil-network-okhttp:3.2.0'
  implementation 'androidx.compose.material3:material3:1.4.0-alpha10'
  implementation 'androidx.core:core-splashscreen:1.0.1'


  testImplementation 'junit:junit:4.13.2'

  // WHEN_VERSIONING_REPLACE_WITH_DEPENDENCIES

  // WHEN_VERSIONING_REMOVE_FROM_HERE

  api 'org.webkit:android-jsc:r250231' // needs to be before react-native

  api project(':packages:react-native:ReactAndroid')

  implementation(project(':packages:react-native:ReactAndroid:hermes-engine')) {
    exclude(group:'com.facebook.fbjni', module: 'fbjni')
  }

  // Expo modules
  api project(':expo')

  // Versioned react native
  // THIS COMMENT IS USED BY android-build-aar.sh DO NOT MODIFY
  // WHEN_VERSIONING_REMOVE_TO_HERE

  // React native dependencies
  api(libs.androidx.appcompat.resources)
  api(libs.androidx.appcompat)
  api(libs.androidx.autofill)
  api(libs.androidx.swiperefreshlayout)

  api(libs.fbjni)
  api(libs.fresco)
  api(libs.fresco.imagepipeline.okhttp3)
  api(libs.fresco.ui.common)
  api(libs.infer.annotation)
  api(libs.soloader)
  api(libs.yoga.proguard.annotations)

  api(libs.jsr305)
  api(libs.okhttp3.urlconnection)
  api(libs.okhttp3)
  api(libs.okio)
  api(libs.javax.inject)

  // Our dependencies
  compileOnly 'org.glassfish:javax.annotation:3.1.1'
  api 'de.greenrobot:eventbus:2.4.0'
  api 'com.google.firebase:firebase-crashlytics:19.3.0'
  api 'com.google.firebase:firebase-crashlytics-ndk:19.3.0'
  api "androidx.room:room-runtime:2.1.0"
  implementation 'com.google.android.material:material:1.3.0'

  api(expoLibs.fresco.animated.gif)
  api(expoLibs.fresco.animated.webp)
  api(expoLibs.fresco.webpsupport)

  api 'com.squareup.picasso:picasso:2.5.2'
  api 'com.google.android.gms:play-services-analytics:17.0.0'
  api 'com.google.android.gms:play-services-maps:18.0.1'
  api 'com.google.android.gms:play-services-auth:17.0.0'
  api 'com.google.android.gms:play-services-location:20.0.0'
  api 'com.google.android.gms:play-services-fitness:17.0.0'
  api 'com.google.android.gms:play-services-wallet:17.0.0' //may need 10.+
  debugApi 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'
  // debugApi 'com.squareup.leakcanary:leakcanary-android:1.4-beta1'
  releaseApi 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'
  api 'commons-io:commons-io:2.6'
  api 'me.leolin:ShortcutBadger:1.1.4@aar'
  implementation "com.vanniktech:android-image-cropper:4.6.0"
  api 'commons-codec:commons-codec:1.10'
  api 'net.openid:appauth:0.7.1'
  api 'com.airbnb.android:lottie:6.5.2'
  compileOnly 'io.branch.sdk.android:library:4.1.0'
  api "androidx.exifinterface:exifinterface:1.3.3"
  api "androidx.legacy:legacy-support-v4:1.0.0"
  api "androidx.browser:browser:1.0.0"
  implementation "androidx.tracing:tracing-ktx:1.1.0"

  api 'com.google.firebase:firebase-core:21.1.0'
  api 'com.google.firebase:firebase-messaging:22.0.0'
  api 'com.google.maps.android:android-maps-utils:3.4.0'
  // Remember to update DetachAppTemplate build.gradle if you add any excludes or transitive = false here!

  // expo-file-system
  api 'com.squareup.okhttp3:okhttp:3.10.0'
  api 'com.squareup.okhttp3:okhttp-urlconnection:3.10.0'

  // expo-av
  // See explanation in expo-av/build.gradle
  api 'com.google.android.exoplayer:extension-okhttp:2.18.1'

  // expo-application
  api 'com.android.installreferrer:installreferrer:1.0'

  // expo-blur
  implementation 'com.github.Dimezis:BlurView:version-2.0.3'

  // expo-camera
  def camerax_version = "1.4.0-alpha02"
  implementation "androidx.camera:camera-core:${camerax_version}"
  implementation "androidx.camera:camera-camera2:${camerax_version}"
  implementation "androidx.camera:camera-lifecycle:${camerax_version}"
  implementation "androidx.camera:camera-video:${camerax_version}"
  implementation "androidx.camera:camera-view:${camerax_version}"
  implementation "androidx.camera:camera-extensions:${camerax_version}"
  implementation "com.google.mlkit:barcode-scanning:17.2.0"
  implementation 'androidx.camera:camera-mlkit-vision:1.4.0-alpha02'

  // expo-store-review
  implementation "com.google.android.play:review:2.0.1"
  implementation "com.google.android.play:review-ktx:2.0.0"

  //dbflow
  kapt "com.github.Raizlabs.DBFlow:dbflow-processor:${safeExtGet('dbFlowVersion', '4.2.4')}"
  implementation "com.github.Raizlabs.DBFlow:dbflow-core:${safeExtGet('dbFlowVersion', '4.2.4')}"
  implementation "com.github.Raizlabs.DBFlow:dbflow:${safeExtGet('dbFlowVersion', '4.2.4')}"

  implementation 'com.cronutils:cron-utils:4.1.3'

  // @react-native-community/viewpager
  api 'com.github.troZee:ViewPager2:v1.0.6'

  // react-native-webview
  implementation "androidx.webkit:webkit:1.4.0"

  // expo-updates
  implementation 'org.bouncycastle:bcutil-jdk15to18:1.70'

  // react-native-maps
  implementation "androidx.work:work-runtime:2.7.1"
}

project.afterEvaluate {
  // Workaround for AGP 7 local lint aar task with errors from a library project owning other aars.
  // In our case, it's the expoview (library project) owning hermes aar
  // ```
  // Error while evaluating property 'hasLocalAarDeps' of task ':expoview:bundleVersionedReleaseLocalLintAar'
  //   > Direct local .aar file dependencies are not supported when building an AAR.
  //     The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR.
  //     Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error).
  //     The following direct local .aar file dependencies of the :expoview project caused this error:
  //     /home/runner/work/expo/expo/node_modules/hermes-engine/android/hermes-release.aar
  // ```
  // For the workaround, we just pretend we don't have local aar dependencies.
  tasks.findByName("bundleReleaseLocalLintAar")?.localAarDeps?.setFrom([])
}

useVendoredModulesForExpoView('unversioned')
