/*
 * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlinx.serialization.runners

import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.js.test.fir.AbstractFirJsTest
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.runners.codegen.AbstractFirLightTreeBlackBoxCodegenTest
import org.jetbrains.kotlin.test.runners.codegen.AbstractIrBlackBoxCodegenTest
import org.jetbrains.kotlinx.serialization.configureForKotlinxSerialization
import org.jetbrains.kotlin.js.test.ir.AbstractJsIrTest;
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives
import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives.LANGUAGE

open class AbstractSerializationIrBoxTest : AbstractIrBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization()
    }
}

open class AbstractSerializationJdk11IrBoxTest : AbstractIrBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(useJdk11 = true)
    }
}

open class AbstractSerializationWithoutRuntimeIrBoxTest : AbstractIrBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(noLibraries = true)
    }
}

open class AbstractSerializationFirLightTreeBlackBoxTest : AbstractFirLightTreeBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization()

        with(builder) {
            // Necessary because AbstractSerializationFirPsiDiagnosticTest, that runs on the same test data, configures it.
            // Otherwise, the dump would be automatically deleted.
            forTestsMatching("*/firMembers/*") {
                defaultDirectives {
                    +FirDiagnosticsDirectives.FIR_DUMP
                }
            }
        }
    }
}

open class AbstractSerializationJdk11FirLightTreeBoxTest : AbstractFirLightTreeBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(useJdk11 = true)
    }
}

open class AbstractSerializationWithoutRuntimeFirLightTreeBoxTest : AbstractFirLightTreeBlackBoxCodegenTest() {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(noLibraries = true)
    }
}

open class AbstractSerializationIrJsBoxTest : AbstractJsIrTest(
    pathToTestDir = "plugins/kotlinx-serialization/testData/boxIr/",
    testGroupOutputDirPrefix = "codegen/serializationBoxIr/"
) {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(target = TargetBackend.JS_IR)
    }
}

open class AbstractSerializationFirJsBoxTest(
    testGroupOutputDirPrefix: String = "codegen/serializationBoxFir/"
) : AbstractFirJsTest(
    pathToTestDir = "plugins/kotlinx-serialization/testData/boxIr/",
    testGroupOutputDirPrefix = testGroupOutputDirPrefix,
) {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        builder.configureForKotlinxSerialization(target = TargetBackend.JS_IR)
    }
}

open class AbstractSerializationFirJsBoxWithInlinedFunInKlibTest : AbstractSerializationFirJsBoxTest(
    testGroupOutputDirPrefix = "codegen/serializationBoxFirWithInlinedFunInKlib/"
) {
    override fun configure(builder: TestConfigurationBuilder) {
        super.configure(builder)
        with(builder) {
            defaultDirectives {
                LANGUAGE with "+${LanguageFeature.IrInlinerBeforeKlibSerialization.name}"
            }
        }
    }
}
