apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'

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

buildscript {
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
  if (expoModulesCorePlugin.exists()) {
    apply from: expoModulesCorePlugin
    applyKotlinExpoModulesCorePlugin()
  }

  // 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
  }

  // Ensures backward compatibility
  ext.getKotlinVersion = {
    if (ext.has("kotlinVersion")) {
      ext.kotlinVersion()
    } else {
      ext.safeExtGet("kotlinVersion", "1.8.10")
    }
  }

  repositories {
    mavenCentral()
  }

  dependencies {
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
  }
}

afterEvaluate {
  publishing {
    publications {
      release(MavenPublication) {
        from components.release
      }
    }
    repositories {
      maven {
        url = mavenLocal().url
      }
    }
  }
}

android {
  compileSdkVersion safeExtGet("compileSdkVersion", 33)

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11.majorVersion
  }

  namespace "expo.modules.devmenu"
  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", 21)
    targetSdkVersion safeExtGet("targetSdkVersion", 33)
    versionCode 10
    versionName '3.2.1'
  }
  lintOptions {
    abortOnError false
  }

  buildTypes {
    releaseWithDevMenu {
      initWith release
      matchingFallbacks = ['release', 'debug']
    }
  }

  sourceSets {
    debug {
      java {
        srcDirs += ['../vendored/react-native-gesture-handler/android/']
        srcDirs += ['../vendored/react-native-safe-area-context/android/']

        def expoPackageVersion = getExpoPackageVersion()
        if (expoPackageVersion >= versionToNumber(45, 0, 0)) {
          srcDirs += "src/expo-45"
        } else {
          srcDirs += "src/expo-44"
        }
      }
    }

    releaseWithDevMenu {
      setRoot 'src/debug'
    }
  }
  publishing {
    singleVariant("release") {
      withSourcesJar()
    }
  }
}

task copyAssets(type: Copy) {
  from('../assets') {
    exclude "*.ios.*"
  }
  into 'src/main/assets'
}

project.afterEvaluate {
  packageDebugAssets.dependsOn copyAssets
  packageReleaseWithDevMenuAssets.dependsOn copyAssets
}

repositories {
  // ref: https://www.baeldung.com/maven-local-repository
  mavenLocal()
  maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    url "$rootDir/../node_modules/react-native/android"
  }
  maven {
    // Android JSC is installed from npm
    url "$rootDir/../node_modules/jsc-android/dist"
  }
}

dependencies {
  implementation project(':expo-modules-core')
  implementation project(":expo-manifests")

  implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
  implementation 'com.google.android.material:material:1.2.1'

  implementation project(":expo-dev-menu-interface")

  implementation 'com.squareup.okhttp3:okhttp:3.14.9'
  implementation 'com.google.code.gson:gson:2.8.6'

  //noinspection GradleDynamicVersion
  implementation 'com.facebook.react:react-native:+'
  implementation "androidx.transition:transition:1.1.0" // use by react-native-reanimated

  // Fixes
  // Cannot access 'androidx....' which is a supertype of 'expo.modules.devmenu.DevMenuActivity'.
  // Check your module classpath for missing or conflicting dependencies
  api "androidx.appcompat:appcompat:1.1.0"
  api "androidx.lifecycle:lifecycle-extensions:2.2.0"

  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'

  // Needed by gesture handler
  implementation "androidx.core:core-ktx:1.6.0"

  api "androidx.browser:browser:1.2.0"

  testImplementation "com.google.truth:truth:1.1.2"
  testImplementation "org.robolectric:robolectric:4.10"
  testImplementation 'com.squareup.okhttp3:mockwebserver:4.3.1'
  testImplementation 'androidx.test:core:1.4.0'
}

def versionToNumber(major, minor, patch) {
  return patch * 100 + minor * 10000 + major * 1000000
}

def getNodeModulesPackageVersion(packageName, overridePropName) {
  def nodeModulesVersion = ["node", "-e", "console.log(require('$packageName/package.json').version);"].execute(null, projectDir).text.trim()
  def version = safeExtGet(overridePropName, nodeModulesVersion)

  def coreVersion = version.split("-")[0]
  def (major, minor, patch) = coreVersion.tokenize('.').collect { it.toInteger() }

  return versionToNumber(
      major,
      minor,
      patch
  )
}

def getExpoPackageVersion() {
  return getNodeModulesPackageVersion("expo", "expoPackageVersion")
}
