plugins {
    id "org.jetbrains.kotlin.multiplatform"
    id "org.jetbrains.kotlin.plugin.serialization"
    id "org.jetbrains.dokka"
}

project.description = "Full collection of all built-in tgbotapi tools"

apply from: "$mppProjectWithSerialization"

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                api project(":tgbotapi.core")
                api project(":tgbotapi.api")
                api project(":tgbotapi.utils")
                api project(":tgbotapi.behaviour_builder")
                api project(":tgbotapi.behaviour_builder.fsm")
                api project(":tgbotapi")
            }
        }
        jsMain {
            dependencies {
                api project(":tgbotapi.webapps")
            }
        }
    }
}

private List<SourceDirectorySet> findSourcesWithName(String... approximateNames) {
    return parent.subprojects
            .findAll { it != project && it.hasProperty("kotlin") }
            .collectMany { it.kotlin.sourceSets }
            .findAll { sourceSet ->
                approximateNames.any { nameToFilter ->
                    sourceSet.name.contains(nameToFilter)
                }
            }.collect { it.kotlin }
}

Object callback = {
    switch (true) {
        case project.hasProperty("DOKKA_PATH"):
            outputDirectory = project.property("DOKKA_PATH").toString()
            break
        case System.getenv("DOKKA_PATH") != null:
            outputDirectory = System.getenv("DOKKA_PATH")
            break
    }

    dokkaSourceSets {
        configureEach {
            skipDeprecated.set(true)

            sourceLink {
                localDirectory.set(file("../"))
                remoteUrl.set(new URL("https://github.com/InsanusMokrassar/ktgbotapi/tree/master"))
                remoteLineSuffix.set("#L")
            }
        }

        named("commonMain") {
            sourceRoots.setFrom(findSourcesWithName("commonMain"))
        }

        named("jsMain") {
            sourceRoots.setFrom(findSourcesWithName("jsMain"))
        }

        named("jvmMain") {
            sourceRoots.setFrom(findSourcesWithName("jvmMain"))
        }
    }
}

tasks.dokkaGfm(callback)
tasks.dokkaHtml(callback)
