buildscript {
  ext {
    boolish = { value ->
      return value.toString().toBoolean()
    }
    getKspVersion = {
      if (rootProject.hasProperty("kspVersion")) {
        return rootProject["kspVersion"]
      }

      // We can remove this path once we update the test environment
      // to use the same version of Kotlin as the `expo/expo` repo.
      def kotlinVersion = rootProject["kotlinVersion"]
      
      if (kotlinVersion == "2.1.20") {
        return "2.1.20-2.0.1"
      } else if (kotlinVersion == "2.0.21") {
        return "2.0.21-1.0.28"
      } else if (kotlinVersion == "1.9.25") {
        return "1.9.25-1.0.20"
      }

      return "1.9.24-1.0.20"
    }
  }

  repositories {
    mavenCentral()
  }

  dependencies {
    classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:${getKspVersion()}"
  }
}

apply plugin: 'com.android.library'
apply plugin: 'expo-module-gradle-plugin'
apply plugin: 'com.google.devtools.ksp'

expoModule {
  canBePublished false
}

group = 'host.exp.exponent'
version = '55.0.18'

// Utility method to derive boolean values from the environment or from Java properties,
// and return them as strings to be used in BuildConfig fields
def getBoolStringFromPropOrEnv(String name, Boolean defaultValue) {
  def env_value = System.getenv(name)
  def prop_value = findProperty(name)
  def value = defaultValue.toString()
  // If present, property value in gradle.properties overrides default
  if (prop_value != null) {
    value = boolish(prop_value).toString()
    println("expo-updates: Value of ${name} was overridden by property, new value = ${value}")
  }
  // If present, env value overrides default and gradle.properties
  if (env_value != null) {
    value = boolish(env_value).toString()
    println("expo-updates: Value of ${name} was overridden by environment, new value = ${value}")
  }
  return value
}

// If true, app will use bundled manifest and updates will be enabled, even for
// debug builds. (default false)
def exUpdatesNativeDebug = getBoolStringFromPropOrEnv("EX_UPDATES_NATIVE_DEBUG", false)

// If true, app is using custom code to initialize expo-updates, so default initialization code
// will be disabled.
def exUpdatesCustomInit = getBoolStringFromPropOrEnv("EX_UPDATES_CUSTOM_INIT", false)

// If true, code will run that delays app loading until updates is initialized, to prevent ANR issues.
// (default true)
def exUpdatesAndroidDelayLoadApp = getBoolStringFromPropOrEnv("EX_UPDATES_ANDROID_DELAY_LOAD_APP", true)

// If true, updates will copy embedded assets to file system when startup. (default false)
def exUpdatesCopyEmbeddedAssets = getBoolStringFromPropOrEnv("EX_UPDATES_COPY_EMBEDDED_ASSETS", false)

def useDevClient = findProject(":expo-dev-client") != null

android {
  buildFeatures {
    buildConfig true
    prefab true
  }

  namespace "expo.modules.updates"
  defaultConfig {
    versionCode 31
    versionName '55.0.18'
    consumerProguardFiles("proguard-rules.pro")
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    buildConfigField("boolean", "EX_UPDATES_NATIVE_DEBUG", exUpdatesNativeDebug)
    buildConfigField("boolean", "EX_UPDATES_CUSTOM_INIT", exUpdatesCustomInit)
    buildConfigField("boolean", "EX_UPDATES_ANDROID_DELAY_LOAD_APP", exUpdatesAndroidDelayLoadApp)
    buildConfigField("boolean", "EX_UPDATES_COPY_EMBEDDED_ASSETS", exUpdatesCopyEmbeddedAssets)
    buildConfigField("boolean", "USE_DEV_CLIENT", useDevClient.toString())

    externalNativeBuild {
      cmake {
        arguments "-DANDROID_STL=c++_shared",
          "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
      }
    }
  }
  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }
  testOptions {
    unitTests.includeAndroidResources = true

    packagingOptions {
      pickFirst "**/libfbjni.so"
      pickFirst "**/libc++_shared.so"

      resources.excludes.add("META-INF/LICENSE.md")
      resources.excludes.add("META-INF/LICENSE-notice.md")
    }
  }
  packagingOptions {
    excludes += [
      "**/libc++_shared.so",
      "**/libfbjni.so",
    ]
  }
  sourceSets {
    main.assets.srcDirs += files("$projectDir/src/main/certificates".toString())
    androidTest.assets.srcDirs += files("$projectDir/src/androidTest/schemas".toString())
    androidTest.assets.srcDirs += files("$projectDir/src/androidTest/certificates".toString())
  }
}

ksp {
  arg("room.generateKotlin", "true")
  // uncomment below to export the database schema when making changes
  // arg("room.schemaLocation", "$projectDir/src/androidTest/schemas".toString())
}

dependencies {
  implementation project(':expo-structured-headers')
  implementation project(':expo-updates-interface')
  implementation project(':expo-manifests')
  implementation project(':expo-json-utils')
  implementation project(':expo-eas-client')
  implementation "com.facebook.react:react-android"

  def room_version = "2.6.1"
  def mockk_version = "1.13.11"

  implementation "androidx.room:room-runtime:$room_version"
  implementation "androidx.room:room-ktx:$room_version"
  ksp "androidx.room:room-compiler:$room_version"

  implementation("com.squareup.okhttp3:okhttp:4.9.2")
  implementation("com.squareup.okhttp3:okhttp-urlconnection:4.9.2")
  implementation("com.squareup.okhttp3:okhttp-brotli:4.9.2")
  implementation("org.bouncycastle:bcutil-jdk15to18:1.81")

  compileOnly 'com.facebook.fbjni:fbjni:0.3.0'

  testImplementation 'junit:junit:4.13.2'
  testImplementation 'androidx.test:core:1.7.0'
  testImplementation 'com.google.truth:truth:1.4.5'
  testImplementation "io.mockk:mockk:$mockk_version"
  testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVersion}"
  testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2'
  testImplementation "com.squareup.okhttp3:mockwebserver:4.9.2"
  testImplementation 'org.robolectric:robolectric:4.16'

  androidTestImplementation 'com.squareup.okio:okio:2.9.0'
  androidTestImplementation 'androidx.test:runner:1.7.0'
  androidTestImplementation 'androidx.test:core:1.7.0'
  androidTestImplementation 'androidx.test:rules:1.7.0'
  androidTestImplementation "io.mockk:mockk-android:$mockk_version"
  androidTestImplementation "androidx.room:room-testing:$room_version"
  androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2'
  androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:${kotlinVersion}"

  implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
}
