package com.aiosman.ravenow.data

import com.aiosman.ravenow.data.api.ApiClient
import com.aiosman.ravenow.data.api.DictItem


interface DictService {
    /**
     * 获取字典项
     */
    suspend fun getDictByKey(key: String): DictItem

    /**
     * 获取字典列表
     */
    suspend fun getDistList(keys: List<String>): List<DictItem>

    /**
     * 获取外部字典项
     */
    suspend fun getOutsideDictByKey(key: String): DictItem

    /**
     * 获取外部字典列表
     */
    suspend fun getOutsideDistList(keys: List<String>): List<DictItem>
}

class DictServiceImpl : DictService {
    override suspend fun getDictByKey(key: String): DictItem {
        val resp = ApiClient.api.getDict(key)
        return resp.body()?.data ?: throw Exception("failed to get dict")
    }

    override suspend fun getDistList(keys: List<String>): List<DictItem> {
        val resp = ApiClient.api.getDicts(keys.joinToString(","))
        return resp.body()?.list ?: throw Exception("failed to get dict list")
    }
    override suspend fun getOutsideDictByKey(key: String): DictItem {
        val resp = ApiClient.api.getOutsideDict(key)
        return resp.body()?.data ?: throw Exception("failed to get outside dict")
    }

    override suspend fun getOutsideDistList(keys: List<String>): List<DictItem> {
        val resp = ApiClient.api.getOutsideDicts(keys.joinToString(","))
        return resp.body()?.list ?: throw Exception("failed to get outside dict list")
    }
}