package com.example.ops_android.data.remote

import com.example.ops_android.data.local.SessionManager
import com.google.gson.Gson
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.RequestBody.Companion.toRequestBody

class BaseUrlInterceptor(
    private val sessionManager: SessionManager
) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        val baseUrl = sessionManager.cachedBaseUrl

        if (baseUrl.isNotEmpty()) {
            val newUrl = request.url.toString().replace("http://localhost", baseUrl)
            request = request.newBuilder().url(newUrl).build()
        }

        return chain.proceed(request)
    }
}
