import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.jvm.tasks.Jar

plugins {
    id("via.base-conventions")
    id("com.github.johnrengelman.shadow")
}

tasks {
    named<Jar>("jar") {
        archiveClassifier.set("unshaded")
        from(project.rootProject.file("LICENSE"))
    }
    val shadowJar = named<ShadowJar>("shadowJar") {
        archiveClassifier.set("")
        configureRelocations()
        configureExcludes()
    }
    named("build") {
        dependsOn(shadowJar)
    }
}

fun ShadowJar.configureRelocations() {
    relocate("com.google.gson", "com.viaversion.viaversion.libs.gson")
    relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt")
    relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil")
    relocate("space.vectrix.flare", "com.viaversion.viaversion.libs.flare")
    relocate("net.lenni0451.mcstructs", "com.viaversion.viaversion.libs.mcstructs")
}

fun ShadowJar.configureExcludes() {
    // FastUtil - we only want object and int maps
    // Object types
    exclude("it/unimi/dsi/fastutil/*/*Reference*")
    exclude("it/unimi/dsi/fastutil/*/*Boolean*")
    exclude("it/unimi/dsi/fastutil/*/*Byte*")
    exclude("it/unimi/dsi/fastutil/*/*Short*")
    exclude("it/unimi/dsi/fastutil/*/*Float*")
    exclude("it/unimi/dsi/fastutil/*/*Double*")
    exclude("it/unimi/dsi/fastutil/*/*Long*")
    exclude("it/unimi/dsi/fastutil/*/*Char*")
    // Map types
    exclude("it/unimi/dsi/fastutil/*/*Custom*")
    exclude("it/unimi/dsi/fastutil/*/*Tree*")
    exclude("it/unimi/dsi/fastutil/*/*Heap*")
    exclude("it/unimi/dsi/fastutil/*/*Queue*")
    // Crossing fingers
    exclude("it/unimi/dsi/fastutil/*/*Big*")
    exclude("it/unimi/dsi/fastutil/*/*Synchronized*")
    exclude("it/unimi/dsi/fastutil/*/*Unmodifiable*")
    // Flare - only need int maps
    exclude("space/vectrix/flare/fastutil/*Double*")
    exclude("space/vectrix/flare/fastutil/*Float*")
    exclude("space/vectrix/flare/fastutil/*Long*")
    exclude("space/vectrix/flare/fastutil/*Short*")
}
