def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def getCapacitorCompileSdkVersion() {
  return  Integer.parseInt(
    rootProject.project(':capacitor-android').android.compileSdkVersion
      .replaceAll(/\D+/, ''))
}

def GetCapacitorVersion() {
  def compileVersion =  getCapacitorCompileSdkVersion()
  if (compileVersion >= 35) {
    return 7
  }
  if (compileVersion == 34) {
    return 6
  }
  else {
    throw new GradleException("The version of Capacitor is not supported by Sentry, CODE: " + compileVersion)
  }
}

def capVersion = GetCapacitorVersion()

def defaultSdkVersion = getCapacitorCompileSdkVersion()
def defaultMinAndroidVersion = 23
def targetJavaVersion = JavaVersion.VERSION_21
if (capVersion >= 7) {
  println "Sentry Capacitor: Applying defaults for Capacitor 7 or higher"
  ext {
    androidxAppCompatVersion = safeExtGet('androidxAppCompatVersion', '1.7.0')
    androidxEspressoCoreVersion = safeExtGet('androidxEspressoCoreVersion', '3.6.1')
  }
}
else {
  println "Sentry Capacitor: Applying defaults for Capacitor 6"
  targetJavaVersion = JavaVersion.VERSION_17
  defaultMinAndroidVersion = 22
  ext {
    androidxAppCompatVersion = safeExtGet('androidxAppCompatVersion', '1.6.1')
    androidxEspressoCoreVersion = safeExtGet('androidxEspressoCoreVersion', '3.5.1')
  }
}

apply plugin: 'com.android.library'

android {
  namespace "io.sentry.capacitor"
  compileSdkVersion safeExtGet('compileSdkVersion', defaultSdkVersion)
  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', defaultMinAndroidVersion)
    targetSdkVersion safeExtGet('targetSdkVersion', defaultSdkVersion)
    versionCode 1
    versionName "1.0"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  lintOptions {
    abortOnError false
  }
  compileOptions {
    targetCompatibility targetJavaVersion
    sourceCompatibility targetJavaVersion
  }
}

repositories {
  google()
  mavenCentral()
}

dependencies {
    implementation project(':capacitor-android')
    implementation 'io.sentry:sentry-android:8.23.0'
}
