buildscript {
    ext.safeExtGet = { prop, fallback ->
        rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
    }
    repositories {
        google()
        gradlePluginPortal()
    }
}

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

def getKotlinVersion() {
    return project.plugins.findPlugin("kotlin-android")?.class?.package?.implementationVersion
}

def isKotlin2OrHigher() {
    def kotlinVersion = getKotlinVersion()
    if (kotlinVersion) {
        def mainVersion = kotlinVersion.split('-')[0]
        def versionParts = mainVersion.tokenize('.').collect { it.toInteger() }
        return versionParts[0] >= 2
    }
    return false
}

def getComposeVersion() {
    if (isKotlin2OrHigher()) {
        def mainVersion = getKotlinVersion().split('-')[0]
        return mainVersion
    }
    return gradle.ext.has("kotlinCompilerExtensionVersion") ? gradle.ext.kotlinCompilerExtensionVersion : "1.5.10"
}

apply plugin: 'com.android.library'
apply plugin: 'com.facebook.react'
apply plugin: 'org.jetbrains.kotlin.android'

if (isKotlin2OrHigher()) {
    project.plugins.apply('org.jetbrains.kotlin.plugin.compose')
}

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 34)

    namespace "ly.img.editor.reactnative.module"

    defaultConfig {
        minSdk = 24
        buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
    }

    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch']
            } else {
                java.srcDirs += ['src/oldarch']
            }
        }
    }

    buildFeatures {
        compose true
    }

    if (!isKotlin2OrHigher()) {
        composeOptions {
            kotlinCompilerExtensionVersion = getComposeVersion()
        }
    }
}

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation findProject(":editor") ?: "ly.img:editor:1.53.0"
    implementation platform("androidx.compose:compose-bom:2023.05.01")
    implementation "androidx.activity:activity-compose:1.6.1"
    implementation "androidx.activity:activity:1.7.0"
    implementation "com.google.android.material:material:1.8.0"

    implementation 'com.facebook.react:react-native'
}
