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

repositories {
    mavenLocal()
    google()
    mavenCentral()
}

kotlin {
    jvm()
//    js(IR) {
//        browser()
//        nodejs()
//    }
    android {}

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib')

                project.parent.subprojects.forEach {
                    if (
                        it != project
                        && it.hasProperty("kotlin")
                        && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
//                        && it.kotlin.sourceSets.any { it.name.contains("jsMain") }
                        && it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
                        && it.kotlin.sourceSets.any { it.name.contains("androidMain") }
                    ) {
                        api it
                    }
                }
            }
        }
//        jsMain {
//            dependencies {
//                implementation kotlin('stdlib')

//                project.parent.subprojects.forEach {
//                    if (
//                        it != project
//                        && it.hasProperty("kotlin")
//                        && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
//                        && it.kotlin.sourceSets.any { it.name.contains("jsMain") }
//                    ) {
//                        api it
//                    }
//                }
//            }
//        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib')

                project.parent.subprojects.forEach {
                    if (
                        it != project
                        && it.hasProperty("kotlin")
                        && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
                        && it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
                    ) {
                        api it
                    }
                }
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')

                project.parent.subprojects.forEach {
                    if (
                        it != project
                        && it.hasProperty("kotlin")
                        && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
                        && it.kotlin.sourceSets.any { it.name.contains("androidMain") }
                    ) {
                        api it
                    }
                }
            }
        }
    }
}

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 }
}

tasks.dokkaHtml {
    dokkaSourceSets {
        configureEach {
            skipDeprecated.set(true)

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

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

//        named("jsMain") {
//            sourceRoots.setFrom(findSourcesWithName("jsMain", "commonMain"))
//        }

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

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

apply from: "$defaultAndroidSettingsPresetPath"