package com.erp.waybill.express;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快递适配器工厂
 */
@Component
public class ExpressAdapterFactory {

    private final Map<String, ExpressAdapter> adapters = new HashMap<>();

    public ExpressAdapterFactory(List<ExpressAdapter> adapterList) {
        for (ExpressAdapter adapter : adapterList) {
            adapters.put(adapter.getCode().toUpperCase(), adapter);
        }
    }

    /**
     * 根据快递编码获取适配器
     */
    public ExpressAdapter getAdapter(String expressCode) {
        ExpressAdapter adapter = adapters.get(expressCode.toUpperCase());
        if (adapter == null) {
            throw new IllegalArgumentException("不支持的快递编码: " + expressCode);
        }
        return adapter;
    }

    /**
     * 检查是否支持该快递
     */
    public boolean isSupported(String expressCode) {
        return adapters.containsKey(expressCode.toUpperCase());
    }

    /**
     * 获取所有支持的快递编码
     */
    public List<String> getSupportedCodes() {
        return List.copyOf(adapters.keySet());
    }
}
