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

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.4.0"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

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

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

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

def supportsNamespace() {
  def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
  def major = parsed[0].toInteger()
  def minor = parsed[1].toInteger()

  // Namespace support was added in 7.3.0
  return (major == 7 && minor >= 3) || major >= 8
}

android {
  if (supportsNamespace()) {
    namespace "com.reactnativetink"

    sourceSets {
      main {
        manifest.srcFile "src/main/AndroidManifestNew.xml"
      }
    }
  }

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  buildFeatures {
    compose true
    buildConfig true
  }

  composeOptions {
    kotlinCompilerExtensionVersion '1.5.14'
  }

  testOptions {
    unitTests.returnDefaultValues = true
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17

  }
  kotlinOptions {
    jvmTarget = '17'
  }
}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")
def compose_ui_version = getExtOrDefault("composeUIVersion")
def compose_material_version = getExtOrDefault("composeMaterialVersion")
def compose_activity_version = getExtOrDefault("composeActivityVersion")

dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"

  implementation 'androidx.appcompat:appcompat:1.6.1'
  implementation 'com.google.android.material:material:1.8.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  implementation 'com.google.code.gson:gson:2.8.5'

  constraints {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
  }

  // Jetpack Compose
  implementation "androidx.compose.ui:ui:$compose_ui_version"
  implementation "androidx.compose.material:material:$compose_material_version"
  implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
  implementation "androidx.activity:activity-compose:$compose_activity_version"

  // Tink
  implementation 'com.tink:link:3.0.0'

  // Money manager
//  implementation "com.tink.moneymanager:moneymanager-ui:2.1.2"

  implementation("com.squareup.okhttp3:okhttp:4.9.1")
  implementation("com.squareup.okhttp3:logging-interceptor:4.9.1")

  // Test
  testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
  testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

  testImplementation "org.mockito:mockito-core:5.1.1"
  testImplementation("org.mockito:mockito-inline:3.12.4")
  testImplementation "org.mockito.kotlin:mockito-kotlin:4.1.0"

  testImplementation("org.robolectric:robolectric:4.9.2")

  testImplementation "junit:junit:4.13.2"
  testImplementation 'androidx.test.ext:junit-ktx:1.1.5'
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
  kotlinOptions {
    jvmTarget = '17'
  }
}
