package com.njcn.gather.aireport.reportapproval.handler;

import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class ReportApprovalHandlerRegistry {

    private final Map<String, ReportApprovalHandler> handlerMap = new LinkedHashMap<String, ReportApprovalHandler>();

    public ReportApprovalHandlerRegistry(List<ReportApprovalHandler> handlers) {
        if (handlers == null || handlers.isEmpty()) {
            return;
        }
        for (ReportApprovalHandler handler : handlers) {
            if (handler != null && StrUtil.isNotBlank(handler.getReportType())) {
                handlerMap.put(handler.getReportType(), handler);
            }
        }
    }

    public ReportApprovalHandler requireHandler(String reportType) {
        ReportApprovalHandler handler = handlerMap.get(reportType);
        if (handler == null) {
            throw new BusinessException(CommonResponseEnum.FAIL, "不支持的报告类型");
        }
        return handler;
    }
}
