package tech.easyflow.admin.controller.common;

import jakarta.servlet.http.HttpServletRequest;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.dict.Dict;
import tech.easyflow.common.dict.DictItem;
import tech.easyflow.common.dict.DictLoader;
import tech.easyflow.common.dict.DictManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/dict/")
public class DictController {

    @Resource
    DictManager dictManager;

    @GetMapping("/items/{code}")
    public Result<List<DictItem>> items(@PathVariable("code") String code, String keyword, HttpServletRequest request) {
        DictLoader loader = dictManager.getLoader(code);
        if (loader == null) {
            return Result.ok(Collections.emptyList());
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        Dict dict = loader.load(keyword, parameterMap);
        if (dict == null) {
            return Result.ok(Collections.emptyList());
        }
        return Result.ok(dict.getItems());
    }

}
