const isObject = (obj) => {
    return Object.prototype.toString.call(obj) === '[object Object]';
};
const isString = (obj) => {
    return Object.prototype.toString.call(obj) === '[object String]';
};
const isNumber = (num) => {
    return Object.prototype.toString.call(num) === '[object Number]';
};
const isArray = (arr) => {
    return Object.prototype.toString.call(arr) === '[object Array]';
};
const isFunction = (obj) => {
    return Object.prototype.toString.call(obj) === '[object Function]';
}
const hasMoney = (configItems) => {
    if (!configItems) return false;
    let _items = configItems.split('|');
    return !!_items.find((item) => item === '金额');
};
const isSelect = (configItems) => {
    if (!configItems) return false;
    let _items = configItems.split('|');
    return !!_items.find((item) => item === 'renderAsSelect');
};
// options=name1[.value1],name2[.value2],name3[.value3];
const returnOptions = (configItems) => {
    if (!configItems) return false;
    let _items = configItems.split('|');
    let _options = _items.find((item) => item.includes('options='));
    if (!_options) return false;
    return _options.replace('options=', '').split(',');
};
const returnRows = (configItems) => {
    if (!configItems) return 3;
    let _items = configItems.split('|');
    let _options = _items.find((item) => item.includes('rows='));
    if (!_options) return 3;
    let _rows = Number(_options.replace('rows=', ''));
    return !isNaN(_rows) && _rows >= 1 ? _rows : 3;
};
const returnCols = (configItems) => {
    if (!configItems) return 1;
    let _items = configItems.split('|');
    let _options = _items.find((item) => item.includes('cols='));
    if (!_options) return 1;
    let _cols = Number(_options.replace('cols=', ''));
    return !isNaN(_cols) && _cols <= 3 && _cols >= 0 ? _cols : 1;
};
/**
 * @description: 用来在summary中处理数值的配置
 * @params: 参数描述
 * @date: 2022/8/10
 * @author: ChenLong
 */
const returnHandledNumber = (configItems, num, isSummary) => {
    // 精度、前缀、后缀、倍率
    // $_d|_d%|_d*0.0001|金额|0.00
    if (isNaN(num)) return '-';
    if (!configItems) return num;
    num = Number(num);
    let _items = configItems.split('|');
    /*  let prefix = '';
      let suffix = '';*/
    let template = '_d';
    let precision = 0;
    let rate = 1;
    _items.forEach((item) => {
        let _arr = [];
        if (item.match(/_d[^\*]/)) {
            // 后缀
            template = item;
        } else if (item.match(/^_d\*/)) {
            // 倍率
            let _rate = item.replace(/_d\*/, '');
            rate = _rate && isSummary ? Number(_rate) : 1; // 总结栏计算需要计算倍率,其他不用
        } else if (item.match(/^0\./)) {
            // 精度
            precision = item.replace('0.', '').length;
        }
    });
    // 可能存在NaN的问题
    let final = _items.includes('金额')
        ? Number((num * rate).toFixed(precision)).toLocaleString()
        : Number((num * rate).toFixed(precision)).toLocaleString();
    return template.replace(/_d/, isString(final) ? final : '-');
};
/**
 * @description: 返回configItems内配置的默认值、默认模式等等
 * @params: 参数描述
 * @date: 2022/8/12
 * @author: ChenLong
 * @params:
 *  configItems 报表字段的配置 例如 defaultValue=智慧水务
 *
 *     defaultDateModel=customer|defaultDateValue=2022-01-01,2022-12-31;
 *           keysArray
 *
 *     所需要返回的值的key的集合,比如你需要获取configItems中的’defaultValue‘,那么keysArray=['defaultValue'];
 * @returns:
 *  defaultValue 通用参数 默认值
 *          defaultDateModel 时间参数 默认模式
 *          defaultDateValue 时间参数 默认时间
 */
const returnDefaultValueOrConfigs = (configItems = '', keysArray = []) => {
    let _map = {};
    let _configItemsArray = configItems.split('|');
    keysArray.forEach((key) => {
        _map[key] = _configItemsArray.find((item) => item.includes(`${key}=`))?.replace(`${key}=`, '');
    });
    return _map;
};

function downloadFunc(url, name, target = '_self') {
    const a = document.createElement('a');
    a.href = url;
    a.target = target;
    a.download = name;
    a.click();
    a.remove();
}

/**
 * @description: 校验文件的名称是否包含特殊字符
 * @params: {Object: File} file file对象 { special:Boolean } 是否去除/的匹配
 * @date: 2021/12/8
 * @author: ChenLong
 * @returns {Object} {type: error | success ,content: 提示...}
 */
function filenameVerification(file, special) {
    // 文件名含有特殊字符  提示不能上传   {+,:/?#[]@!$&\\*+;=}
    // 规则对象(flag)
    var flag = !special
        ? new RegExp("[`~!@#$^&*=|{}':;',\\[\\]/?~!@#¥&*——|{}【】‘;:”“'。,、?]")
        : new RegExp("[`~!@#$^&*=|{}':;',[\\]?~!@#¥&*——|{}【】‘;:”“'。,、?]");
    if (flag.test(file.name)) {
        return {
            type: 'error',
            content: `文件名格式错误,请检查文件名是否含有特殊字符${"~!@#$^&*=|{}':;',\\[\\]/?~!@#¥&*——|{}【】‘;:”“'。,、?"}`,
        };
    }
    return {
        type: 'success',
        content: `上传成功!`,
    };
}

export {
    isObject,
    isString,
    isNumber,
    hasMoney,
    isArray,
    isFunction,
    returnHandledNumber,
    returnDefaultValueOrConfigs,
    downloadFunc,
    filenameVerification,
    isSelect,
    returnOptions,
    returnRows,
    returnCols,
};