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 >= 36) {
    return 8
  }
  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 = 24
def targetJavaVersion = JavaVersion.VERSION_21
if ( capVersion >= 8) {
  println "Sentry Capacitor: Applying defaults for Capacitor 8 or higher"
  targetJavaVersion = JavaVersion.VERSION_21
}
else if ( capVersion >= 6) {
  println "Sentry Capacitor: Applying defaults for Capacitor 6 or 7"
  targetJavaVersion = JavaVersion.VERSION_17
}
else {
  throw new GradleException("The version of Capacitor is not supported by Sentry, CODE: " + capVersion)
}
if (capVersion == 8) {
  println "Sentry Capacitor: Applying defaults for Capacitor 8 or higher"
  ext {
    androidxAppCompatVersion = safeExtGet('androidxAppCompatVersion', '1.7.1')
    androidxEspressoCoreVersion = safeExtGet('androidxEspressoCoreVersion', '3.6.1')
  }
}
else if (capVersion == 7) {
  println "Sentry Capacitor: Applying defaults for Capacitor 7"
  ext {
    androidxAppCompatVersion = safeExtGet('androidxAppCompatVersion', '1.7.0')
    androidxEspressoCoreVersion = safeExtGet('androidxEspressoCoreVersion', '3.7.0')
  }
}
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"
  }
  lintOptions {
    abortOnError false
  }
  compileOptions {
    targetCompatibility targetJavaVersion
    sourceCompatibility targetJavaVersion
  }
}

repositories {
  google()
  mavenCentral()
}

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