buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault.
  //
  // Two independent selections, because two different builds evaluate this file:
  //
  //   - Standalone, with this folder as its own root project. Nothing else supplies plugins
  //     and the wrapper here is Gradle 9.3.1, so it needs a Kotlin plugin that can drive
  //     that. React Native's own @react-native/gradle-plugin runs exactly this pairing --
  //     it pins kotlin 2.1.20 and ships a gradle-9.3.1 wrapper -- so we match it.
  //
  //   - Inside a host app. Plugins load parent-first, so a host declaring its own Kotlin
  //     plugin wins and neither value below applies. Only hosts declaring none reach the
  //     fallback, and those are React Native 0.72 and older, whose templates ship Gradle
  //     7.5.1 and set no ext.kotlinVersion. That value must stay low enough for 7.5.1,
  //     which rules out 2.1.20 (it needs Gradle >= 7.6.3).
  //
  // Collapsing these into one property is what broke old consumers: the version the
  // standalone build needs is far above the version an RN 0.70 host can accept.
  def kotlin_version
  if (project == rootProject) {
    // Standalone build.
    kotlin_version = project.properties["KlaviyoReactNativeSdk_standaloneKotlinVersion"]
  } else if (rootProject.ext.has("kotlinVersion")) {
    // Host app that declares a Kotlin version.
    kotlin_version = rootProject.ext.get("kotlinVersion")
  } else {
    // Host app that declares none -- React Native 0.72 and older.
    kotlin_version = project.properties["KlaviyoReactNativeSdk_kotlinVersion"]
  }

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    // The Android Gradle Plugin is only required when this folder is opened as its own root
    // project. Every React Native app template declares AGP in its own root build.gradle, and
    // Gradle loads plugins parent-first, so inside a host app that declaration wins and this
    // entry is never used. Declaring it unconditionally only resolves a version the host discards.
    if (project == rootProject) {
      classpath "com.android.tools.build:gradle:8.10.1"
    }

    // The Kotlin plugin, by contrast, must stay unconditional. React Native templates only began
    // declaring kotlin-gradle-plugin at 0.73, and a subproject's buildscript classpath is not
    // shared with its siblings, so on 0.72 and older this entry is the only thing that supplies
    // `kotlin-android` to this module. A host that declares its own Kotlin plugin still wins,
    // parent-first; kotlin_version above only takes effect when the host declares none.
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

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

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

apply plugin: "com.android.library"
// AGP 9 ships built-in Kotlin support and registers the `kotlin` extension
// itself. Applying the Kotlin plugin on top of it fails configuration with
// "Cannot add extension with name 'kotlin'". Only apply it when nothing has
// registered that extension yet.
if (project.extensions.findByName('kotlin') == null) {
  apply plugin: "kotlin-android"
}

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

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["KlaviyoReactNativeSdk_" + 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.klaviyoreactnativesdk"

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

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    consumerProguardFiles "consumer-rules.pro"
  }

  buildFeatures {
    buildConfig true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

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

def klaviyoAndroidSdkVersion = getExtOrDefault("klaviyoAndroidSdkVersion")
def localProperties = new Properties()
if (rootProject.file("local.properties").canRead()) {
  localProperties.load(new FileInputStream(rootProject.file("local.properties")))
}
def reactNativeAndroidVersion = localProperties['reactNativeAndroidVersion'] ?: ""

// Whether to include the full location module (geofencing + location permissions).
// Set klaviyoIncludeLocation=false in your gradle.properties to use lightweight location-core instead.
def includeLocation = rootProject.hasProperty('klaviyoIncludeLocation')
    ? rootProject.property('klaviyoIncludeLocation').toBoolean()
    : true

// Whether to include the full forms module (in-app forms rendering).
// Set klaviyoIncludeForms=false in your gradle.properties to use lightweight forms-core instead.
def includeForms = rootProject.hasProperty('klaviyoIncludeForms')
    ? rootProject.property('klaviyoIncludeForms').toBoolean()
    : true

dependencies {
  if (reactNativeAndroidVersion) {
    // For local development of the SDK code, specify the react-android version to use
    // So that the SDK can be built and .kt files are linted against a real version of react-native
    implementation "com.facebook.react:react-android:$reactNativeAndroidVersion"
  } else {
    // Production build / once embedded in a react-native app,
    // the react-native version gets loaded in from the application 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:+"
  }

  // Klaviyo Android SDK
  api "com.github.klaviyo.klaviyo-android-sdk:analytics:$klaviyoAndroidSdkVersion"
  api "com.github.klaviyo.klaviyo-android-sdk:push-fcm:$klaviyoAndroidSdkVersion"
  implementation "com.github.klaviyo.klaviyo-android-sdk:core:$klaviyoAndroidSdkVersion"

  // Location: full module (includes -core transitively), or just -core for lightweight
  if (includeLocation) {
    api "com.github.klaviyo.klaviyo-android-sdk:location:$klaviyoAndroidSdkVersion"
  } else {
    implementation "com.github.klaviyo.klaviyo-android-sdk:location-core:$klaviyoAndroidSdkVersion"
  }

  // Forms: full module (includes -core transitively), or just -core for lightweight
  if (includeForms) {
    api "com.github.klaviyo.klaviyo-android-sdk:forms:$klaviyoAndroidSdkVersion"
  } else {
    implementation "com.github.klaviyo.klaviyo-android-sdk:forms-core:$klaviyoAndroidSdkVersion"
  }
}

