package cd.casic.plugin.register.config;

import org.pf4j.util.StringUtils;

public class ConfigFileUtils {
    /**
     * 拼接插件独立配置文件的文件名和环境后缀
     * @param fileName 插件独立配置文件文件名
     * @param suffix 配置文件环境后缀，PluginConfiguration中定义
     * @return 拼接后的文件名
     */
    public static String joinConfigFileName(String fileName, String suffix){
        if(StringUtils.isNullOrEmpty(fileName)){
            return null;
        }
        String fileNamePrefix;
        String fileNamePrefixSuffix;

        if(fileName.lastIndexOf(".") == -1) {
            fileNamePrefix = fileName;
            fileNamePrefixSuffix = "";
        } else {
            int index = fileName.lastIndexOf(".");
            fileNamePrefix = fileName.substring(0, index);
            fileNamePrefixSuffix = fileName.substring(index);
        }
        if(suffix == null){
            suffix = "";
        }
        if(StringUtils.isNotNullOrEmpty(suffix) && !suffix.startsWith("-")){
            suffix = "-" + suffix;
        }
        return fileNamePrefix + suffix + fileNamePrefixSuffix;
    }
}
