package dp.kotul.solrapiapplication.service

import dp.kotul.solrapimodel.SolrApiSearchParameters
import org.springframework.stereotype.Service
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap

interface SearchParameterService {
    fun processParameters(searchParameters: SolrApiSearchParameters): MultiValueMap<String, String>
}

@Service
class SearchParameterServiceImpl : SearchParameterService {

    override fun processParameters(searchParameters: SolrApiSearchParameters): MultiValueMap<String, String> {
        val parameters = LinkedMultiValueMap<String, String>()

        parameters.add("q", searchParameters.q)
        searchParameters.fq?.let { parameters.add("fq", it) }
        searchParameters.fl?.let { parameters.add("fl", it.joinToString(separator = ", ")) }
        searchParameters.sort?.let { parameters.add("sort", it.joinToString(separator = ", ")) }
        searchParameters.start?.let { parameters.add("start", it.toString()) }
        searchParameters.rows?.let { parameters.add("rows", it.toString()) }

        return parameters
    }
}
