/**
 * 校验枚举工具
 * 
 */

import { ERRORENUM } from "../config/enum/errorEnum";
import { BizError } from "./bizError";


/**
 * 校验value是否符合传入的枚举
 * @param name 被掉用名称 用于输出异常日志
 * @param key 目标字段  用于输出异常日志
 * @param enumConf 目标枚举
 * @param value 目标值
 * 无返回 有异常直接报错
 */
export function eccEnumValue(name:string, key:string, enumConf, value:any) {
    let eccSuccess = true;
    if ( typeof value == 'number' ) {
        if (!enumConf[value] ) eccSuccess = false;
    } else if (Array.isArray(value)) {
        value.forEach(item => {
            if ( !enumConf[item] ) eccSuccess = false;
        });
    }
    
    if (!eccSuccess) throw new BizError(ERRORENUM.参数校验失败, `${name} 下的 ${key} 字段值为 ${value} 不满足枚举范围`);
}


/**
 * 将枚举值转换成对应的枚举名(key)
 * @param enumConf 目标枚举
 * @param value 目标值
 * @returns string  返回字符串 如果传入多个枚举值，就拼接字符串
 */
export function changeEnumValue(enumConf, value:any) {
    if (!value) return '';
    if ( typeof value == 'number' ) {
        let str = enumConf[value];
        /**特化处理 */
        if(/_dou/.test(str)) str = str.replace(/_dou/gm, "，");
        if(/_zyh/.test(str)) str = str.replace(/_zyh/gm, "“");
        if(/_yyh/.test(str)) str = str.replace(/_yyh/gm, "”");
        if(/_dun/.test(str)) str = str.replace(/_dun/gm, "、");
        if(/_ju/.test(str)) str = str.replace(/_ju/gm, "。");

        return str
    } else if (typeof value == 'string') { 
        try {//兼容数据库  '[1,2,3]'
            value = JSON.parse(value);
        }catch(err) {
            return enumConf[parseInt(value)];
        } 
    }
    let str = "";
    value.forEach((item, index) => {
        let subStr = enumConf[item];
         /**特化处理 */
        if(/_dou/.test(subStr)) subStr = subStr.replace(/_dou/gm, "，");
        if(/_zyh/.test(subStr)) subStr = subStr.replace(/_zyh/gm, "“");
        if(/_yyh/.test(subStr)) subStr = subStr.replace(/_yyh/gm, "”");
        if(/_dun/.test(subStr)) subStr = subStr.replace(/_dun/gm, "、");
        if(/_ju/.test(subStr)) subStr = subStr.replace(/_ju/gm, "。");
        str += subStr;
        if (index == value.length-1) str+="";
        else str += ","
    });

    return str;
}


export function changeEnumManyValue(enumConf, value:any) {
    let list:string[] = [];
    value.forEach((item, index) => {
        let subStr = enumConf[item];
        /** 特化处理 中文引号在枚举中不适用*/
        if (subStr == "_投资__孵化_类型") {
            subStr = subStr.replace("__","+");
            subStr = subStr.replace("_","“");
            subStr = subStr.replace("_","”");
        }
        if (subStr == "经营成本过高_场地成本或人员成本_" || subStr == "办公空间拓展_无合适办公空间_") {
            subStr = subStr.replace("_","(");
            subStr = subStr.replace("_",")");
        }
        if (subStr == "迁出孵化器_仍在张江" || subStr == "迁出张江_仍在浦东" || subStr == "迁出浦东_仍在上海") {
            subStr = subStr.replace("_",",");
        }
        if (subStr == "科技金融_风险投资_" || subStr == "科技金融_其他_" || subStr == "技术专家_法律专家_") {
            subStr = subStr.replace("_","(");
            subStr = subStr.replace("_",")");
        }
        list.push(subStr);
    });

    return list;
}