buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  def kotlinVersion = rootProject.ext.has("kotlinVersion")
    ? rootProject.ext.get("kotlinVersion")
    : project.properties["StripeSdk_kotlinVersion"]

  def kotlinMajor = kotlinVersion.tokenize("\\.")[0].toInteger()

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.2.2"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    classpath "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
    // only use this old compose-compiler plugin when Kotlin >= 2.0
    if (kotlinMajor >= 2) {
      classpath "org.jetbrains.kotlin:compose-compiler-gradle-plugin:$kotlinVersion"
    }
  }
}

if (project == rootProject) {
  apply from: "spotless.gradle"
  return
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["StripeSdk_" + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["StripeSdk_" + name]).toInteger()
}

def isNewArchitectureEnabled() {
  // To opt-in for the New Architecture, you can either:
  // - Set `newArchEnabled` to true inside the `gradle.properties` file
  // - Invoke gradle with `-newArchEnabled=true`
  // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

def isOnrampIncluded() {
  return rootProject.ext.has("StripeSdk_includeOnramp") &&
    rootProject.ext.StripeSdk_includeOnramp.toString().toLowerCase() == "true"
}

def reactNativeArchitectures() {
  def value = project.getProperties().get("reactNativeArchitectures")
  return value ? value.split(",") : [
    "armeabi-v7a",
    "x86",
    "x86_64",
    "arm64-v8a"
  ]
}

def kotlinVersion = getExtOrDefault("kotlinVersion")
def kotlinMajor = kotlinVersion.tokenize("\\.")[0].toInteger()

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-parcelize"
// Only apply the compose plugin if we have the old compose-compiler on the classpath
if (kotlinMajor >= 2) {
  apply plugin: "org.jetbrains.kotlin.plugin.compose"
}

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}

android {
  namespace "com.reactnativestripesdk"
  buildFeatures {
    buildConfig true
  }

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

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

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
    consumerProguardFiles "proguard-rules.txt"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    buildConfigField "boolean", "IS_ONRAMP_INCLUDED", isOnrampIncluded().toString()
    buildConfigField "String", "STRIPE_ANDROID_SDK_VERSION", "\"${getExtOrDefault("stripeVersion")}\""

    ndk {
      abiFilters(*reactNativeArchitectures())
    }
  }

  lintOptions {
    disable "GradleCompatible"
    textReport true
    textOutput "stdout"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  kotlinOptions {
    allWarningsAsErrors = System.getenv("RN_STRIPE_WARNINGS_AS_ERRORS") == "true"
  }

  buildFeatures {
    viewBinding true
    compose true
  }

  sourceSets.main {
    java {
      if (!isNewArchitectureEnabled()) {
        srcDirs += ["src/oldarch/java"]
      }
      if (isOnrampIncluded()) {
        srcDirs += ["src/onramp/java"]
      }
    }
  }

  // When running tests on bitrise we need to make sure that the test artifacts are inside
  // the root project directory (in that case example/android), or it won't be picked up.
  testOptions {
    unitTests.all { Test test ->
      def outputRoot = rootProject.layout.buildDirectory.get().asFile
      test.reports.junitXml.outputLocation.set(new File(outputRoot, "test-results/${test.name}"))
      test.reports.html.outputLocation.set(new File(outputRoot, "reports/tests/${test.name}"))
    }
  }
}

repositories {
  mavenCentral()
  google()
}

def stripeVersion = getExtOrDefault("stripeVersion")

dependencies {
  // noinspection GradleDynamicVersion
  api "com.facebook.react:react-native:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
  implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
  implementation("com.stripe:stripe-android:$stripeVersion")
  implementation("com.stripe:financial-connections:$stripeVersion")
  implementation("com.stripe:payment-method-messaging:$stripeVersion")
  
  if (isOnrampIncluded()) {
    implementation("com.stripe:crypto-onramp:$stripeVersion")
  }
  
  implementation "com.google.android.material:material:1.3.0"
  implementation "androidx.appcompat:appcompat:1.4.1"
  implementation "androidx.legacy:legacy-support-v4:1.0.0"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"
  implementation "androidx.browser:browser:1.8.0"

  // play-services-wallet is already included in stripe-android
  compileOnly "com.google.android.gms:play-services-wallet:19.3.0"

  // Users need to declare this dependency on their own, otherwise all methods are a no-op
  compileOnly "com.stripe:stripe-android-issuing-push-provisioning:1.1.0"

  testImplementation "junit:junit:4.13"
  testImplementation "org.mockito:mockito-core:3.+"
  testImplementation "org.robolectric:robolectric:4.10"
  testImplementation "androidx.test:core:1.4.0"
  testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0"

  implementation "androidx.compose.ui:ui:1.7.8"
  implementation "androidx.compose.foundation:foundation-layout:1.7.8"
}

def lintRulesJar = file("$projectDir/lint-rules/build/libs/lint-rules-1.0.0.jar")
if (lintRulesJar.exists()) {
  dependencies {
    lintChecks files(lintRulesJar)
  }
}

