// Generated by alef. Do not edit by hand.
@file:Suppress(
    "ktlint:standard:trailing-comma-on-call-site",
    "ktlint:standard:trailing-comma-on-declaration-site",
    "ktlint:standard:spacing-between-declarations-with-comments",
    "ktlint:standard:spacing-between-declarations-with-annotations",
    "ktlint:standard:when-entry-bracing",
    "ktlint:standard:blank-line-between-when-conditions",
    "ktlint:standard:blank-line-before-declaration",
    "ktlint:standard:chain-method-continuation",
    "ktlint:standard:annotation",
    "ktlint:standard:max-line-length",
    "ktlint:standard:no-semi",
    "ktlint:standard:statement-wrapping",
    "MaxLineLength",
    "TooManyFunctions",
    "FunctionParameterNaming",
    "LongParameterList",
    "CyclomaticComplexMethod",
    "LongMethod",
)

package dev.kreuzberg

/**
 * A single line in a unified-diff hunk.
 *
 * Defined here (rather than only in `crate.diff`) so `RevisionDelta` can
 * reference it unconditionally, without requiring the `diff` Cargo feature.
 * `crate.diff` re-exports this type verbatim.
 */
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = DiffLineDeserializer::class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = DiffLineSerializer::class)
sealed class DiffLine {
    /** Unchanged context line. */
    data class Context(val value: String) : DiffLine()
    /** Line added in the "after" version. */
    data class Added(val value: String) : DiffLine()
    /** Line removed from the "before" version. */
    data class Removed(val value: String) : DiffLine()
}

private class DiffLineDeserializer : com.fasterxml.jackson.databind.deser.std.StdDeserializer<DiffLine>(DiffLine::class.java) {
    @Suppress("LongMethod")
    override fun deserialize(
        parser: com.fasterxml.jackson.core.JsonParser,
        ctx: com.fasterxml.jackson.databind.DeserializationContext,
    ): DiffLine {
        val node = parser.codec.readTree<com.fasterxml.jackson.databind.node.ObjectNode>(parser)
        val tag = node.get("kind")?.asText()
        @Suppress("UNCHECKED_CAST")
        val payload = (node.deepCopy() as com.fasterxml.jackson.databind.node.ObjectNode).apply { remove("kind") }
        return when (tag) {
            "context" -> DiffLine.Context(ctx.readTreeAsValue<String>(payload, String::class.java))
            "added" -> DiffLine.Added(ctx.readTreeAsValue<String>(payload, String::class.java))
            "removed" -> DiffLine.Removed(ctx.readTreeAsValue<String>(payload, String::class.java))
            else -> throw com.fasterxml.jackson.databind.exc.InvalidFormatException(
                parser, "Unknown DiffLine tag", tag, DiffLine::class.java,
            )
        }
    }
}

private class DiffLineSerializer : com.fasterxml.jackson.databind.ser.std.StdSerializer<DiffLine>(DiffLine::class.java) {
    @Suppress("LongMethod")
    override fun serialize(
        value: DiffLine,
        gen: com.fasterxml.jackson.core.JsonGenerator,
        provider: com.fasterxml.jackson.databind.SerializerProvider,
    ) {
        @Suppress("UNCHECKED_CAST")
        val mapper = (gen.codec as? com.fasterxml.jackson.databind.ObjectMapper) ?: com.fasterxml.jackson.databind.ObjectMapper().findAndRegisterModules()
        val node: com.fasterxml.jackson.databind.node.ObjectNode = when (value) {
            is DiffLine.Context -> {
                @Suppress("UNCHECKED_CAST")
                val n = mapper.valueToTree<com.fasterxml.jackson.databind.node.ObjectNode>(value.value) as com.fasterxml.jackson.databind.node.ObjectNode
                n.put("kind", "context")
                n
            }
            is DiffLine.Added -> {
                @Suppress("UNCHECKED_CAST")
                val n = mapper.valueToTree<com.fasterxml.jackson.databind.node.ObjectNode>(value.value) as com.fasterxml.jackson.databind.node.ObjectNode
                n.put("kind", "added")
                n
            }
            is DiffLine.Removed -> {
                @Suppress("UNCHECKED_CAST")
                val n = mapper.valueToTree<com.fasterxml.jackson.databind.node.ObjectNode>(value.value) as com.fasterxml.jackson.databind.node.ObjectNode
                n.put("kind", "removed")
                n
            }
        }
        mapper.writeTree(gen, node)
    }
}
