/**
 * Freshdesk React Native SDK — Android consumer Gradle snippet.
 *
 * The Freshdesk Android native SDK (com.freshworks.sdk:freshdesk 2.2.x) is
 * compiled with Kotlin 2.1 metadata while React Native 0.75.x builds with
 * Kotlin 1.9.x. Any module that compiles against the Freshdesk SDK therefore
 * needs `-Xskip-metadata-version-check`. The wrapper library sets this on its
 * own module; this snippet extends it to every subproject of a host app.
 *
 * Apply from the host app's ROOT `android/build.gradle` (after the React Native
 * rootproject plugin):
 *
 *   apply from: file("../node_modules/@freshworks/react-native-freshdesk-sdk/android/freshdesk-consumer.gradle")
 */

subprojects { subproject ->
    subproject.tasks.configureEach { task ->
        // Match KotlinCompile tasks by runtime class name. A class literal
        // (org.jetbrains.kotlin.gradle.tasks.KotlinCompile) is NOT resolvable
        // from an `apply from:` script's classloader, so this stays string-based
        // and dispatches `kotlinOptions` dynamically on the task itself.
        if (task.class.name.contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")) {
            task.kotlinOptions {
                freeCompilerArgs += ["-Xskip-metadata-version-check"]
            }
        }
    }

    // RN codegen (New Architecture) emits C++ identifiers like `$event` and
    // `$payload`. Clang warns on `-Wdollar-in-identifier-extension` for every
    // generated line — hundreds of warnings from navigation libraries such as
    // react-native-screens, not from app or Freshdesk SDK code. Suppress for
    // any subproject that uses CMake external native builds.
    subproject.afterEvaluate {
        if (!(subproject.plugins.hasPlugin("com.android.library")
                || subproject.plugins.hasPlugin("com.android.application"))) {
            return
        }
        def androidExt = subproject.extensions.findByName("android")
        if (androidExt == null) {
            return
        }
        def extNative = androidExt.defaultConfig.externalNativeBuild
        if (extNative != null) {
            extNative.cmake {
                cppFlags "-Wno-dollar-in-identifier-extension"
            }
        }
    }
}
