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

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

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

if (isNewArchitectureEnabled()) {
    apply plugin: 'com.facebook.react'
}

android {
    namespace "com.turbohaptics"
    compileSdkVersion safeExtGet('compileSdkVersion', 33)

    // Used to override the NDK path/version on internal CI or by allowing
    // users to customize the NDK path/version from their root project.
    if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 33)
        buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++17"
            }
        }
    }

    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += [
                        'src/fabric',
                        // Codegen output directory
                        project.file("${project.buildDir}/generated/source/codegen/java")
                ]
            } else {
                java.srcDirs += ['src/paper']
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/jni/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
    compileOnly "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
}
