/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */

package net.ccbluex.liquidbounce.integration.interop

import com.google.gson.Gson
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.serialization.gson.GsonConverter
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import java.net.URI

fun Application.installGson(gson: Gson) =
    install(ContentNegotiation) {
        register(ContentType.Application.Json, GsonConverter(gson))
    }

fun Application.installCors() =
    install(CORS) {
        allowOrigins(::isLocalOrigin)
        anyMethod()
        allowHeader(HttpHeaders.Authorization)
        allowHeader(HttpHeaders.ContentLength)
        allowHeader("X-Requested-With")
        allowCredentials = true
        allowNonSimpleContentTypes = true
    }

internal fun isLocalOrigin(origin: String): Boolean = runCatching {
    val uri = URI(origin)
    val supportedScheme = uri.scheme.equals("http", true) || uri.scheme.equals("https", true)
    val localHost = uri.host.equals("localhost", true) || uri.host == "127.0.0.1"

    supportedScheme && localHost
}.getOrDefault(false)
