import { staticUrl } from '@/config.js';
/**
 * @desc    价格过滤器
 * @param   {[Number|String]}   arg      原始参数
 * @param   {[Number]}          num      小数点位
 * @param   {[String]}          symbol   符号
 * @param   {[Boolean]}         spilt    是否进行逗号分隔
 * @return  {[String]}
 */
export const priceFormat = (arg, num = 0, symbol, spilt = false) => {
    arg = Number(arg).toFixed(num);
    if (spilt == true) {
        if (arg.indexOf('.') == -1) {
            arg = arg.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        } else {
            let parts = arg.split('.');
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            arg = parts.join('.');
        }
    }

    if (symbol) {
        arg = symbol + arg;
    }
    return arg;
};
/**
 * @desc    格式化日期时间
 * @param   {[Date|String]}    date
 * @param   {[String]}    format时间日期格式
 * @return  {[String]}
 */
export const formatTime = (time, format = 'yyyy-MM-dd hh:mm:ss') => {
    if (arguments.length === 0) {
        return null;
    }
    let date;
    if (typeof time === 'object') {
        date = time;
    } else {
        if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
            time = parseInt(time);
        }
        if (typeof time === 'number' && time.toString().length === 10) {
            time = time * 1000;
        }
        date = new Date(time);
    }
    let dateObj = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        'S+': date.getMilliseconds(),
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in dateObj) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? dateObj[k] : ('00' + dateObj[k]).substr(('' + dateObj[k]).length));
        }
    }
    return format;
};

/**
 * 相对路径过滤器(主要用于后端接口返回的静态资源)
 * @param {String} url
 * @param {String} type
 */
export const staticPath = (url) => {
    if (url && !/(http|https):\/\/([\w.]+\/?)\S*/.test(url)) {
        return staticUrl + url;
    }
    return url || '';
};

export const isArray = (obj) => {
    return Object.prototype.toString.call(obj) === '[object Array]';
};

// 在线聊天成员
export const chatUnique = (arr) => {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        let flag = false;
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i].fromUser === arr[j].fromUser) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

// 在线聊天成员
export const chatUnique2 = (arr) => {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].flag === true) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

// 获取用户等级

export const optionalRoles = async id => {
    let rolesList = []
    await queryOptionalRoles(id)
        .then(res => {
            rolesList = res;
        })
        .catch(err => err);
    return rolesList
}



/**
 * @desc    数组转树形
 * @param   {[Array]}
 */
export const toTree = data => {
    let result = [];
    if (!Array.isArray(data)) {
        return result;
    }
    data.forEach(item => {
        delete item.appmenu;
    });
    let map = {};
    data.forEach(item => {
        map[item.id] = item;
    });
    data.forEach(item => {
        let parent = map[item.parentId];
        if (parent) {
            (parent.appmenu || (parent.appmenu = [])).push(item);
        } else {
            result.push(item);
        }
    });
    return result;
};
