static def isKotlin2OrHigher(kotlinVersion) {
  def version = kotlinVersion.split("\\.")
  def major = version[0] as Integer
  def minor = version[1] as Integer
  return major > 2 || (major == 2 && minor >= 0)
}

static def getComposeCompilerVersion(kotlinVersion) {
  def versionMap = [
    "1.9.25": "1.5.15",
    "1.9.24": "1.5.14",
    "1.9.23": "1.5.13",
    "1.9.22": "1.5.10",
    "1.9.21": "1.5.7",
    "1.9.20": "1.5.5",
    "1.9.10": "1.5.3",
    "1.9.0": "1.5.2",
    "1.8.22": "1.4.8",
    "1.8.21": "1.4.7",
    "1.8.20": "1.4.6",
    "1.8.10": "1.4.4",
    "1.8.0": "1.4.1"
  ]

  if (versionMap.containsKey(kotlinVersion)) {
    return versionMap[kotlinVersion]
  }

  return "1.5.10"
}

buildscript {
  ext.getExtOrDefault = { name ->
    return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['CheckoutReactNativeComponents_' + name]
  }

  repositories {
    google()
    mavenCentral()
  }

  def kotlinVer = getExtOrDefault("kotlinVersion")
  def kotlinVersionParts = kotlinVer.split("\\.")
  def kotlinMajor = kotlinVersionParts[0] as Integer
  def kotlinMinor = kotlinVersionParts[1] as Integer
  def isKotlin2 = kotlinMajor > 2 || (kotlinMajor == 2 && kotlinMinor >= 0)

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

    if (isKotlin2) {
      classpath "org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:${kotlinVer}"
    }
  }
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-parcelize"
apply plugin: "kotlinx-serialization"

def kotlinVersion = getExtOrDefault("kotlinVersion")
if (isKotlin2OrHigher(kotlinVersion)) {
  apply plugin: "org.jetbrains.kotlin.plugin.compose"
}

apply plugin: "com.facebook.react"

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

android {
  namespace "com.checkoutreactnativecomponents"

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

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

  buildFeatures {
    buildConfig true
    compose = true
  }

  if (!isKotlin2OrHigher(kotlinVersion)) {
    composeOptions {
      kotlinCompilerExtensionVersion = getComposeCompilerVersion(kotlinVersion)
    }
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    freeCompilerArgs += "-Xexplicit-api=strict"
  }

  sourceSets {
    main {
      java.srcDirs += [
        "generated/java",
        "generated/jni"
      ]
    }
  }
}

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

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
  implementation "com.facebook.react:react-android"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

  implementation "com.checkout:checkout-android-components-reactnative:beta8"

  // Test dependencies
  testImplementation "org.mockito:mockito-core:5.2.0"
  testImplementation "org.mockito.kotlin:mockito-kotlin:5.0.0"
  testImplementation "org.mockito:mockito-inline:5.2.0"
  testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1"
  testImplementation "junit:junit:4.13.2"
  testImplementation "org.robolectric:robolectric:4.12"
  testImplementation("io.mockk:mockk:1.14.7")
}

react {
  jsRootDir = file("../src/")
  libraryName = "CheckoutReactNativeComponents"
  codegenJavaPackageName = "com.checkoutreactnativecomponents"
}
