export function resolveFileString(str: string) {
  return str ? str.split(';').map(s => resolveSingleFileString(s)) : []
}

export function resolveSingleFileString(str: string, sign = '|') {
  const [url, name] = str.split(sign)
  return {
    name,
    url: url.startsWith('http') ? url : `${window.GLOBAL_CONFIG.oss}/${url}`,
    origin: url,
  }
}

// 获取完整文件地址
export function getFullUrl(url: string) {
  if (!url)
    return ''
  return url.startsWith('http') ? url : `${window.GLOBAL_CONFIG.oss}/${url}`
}

// 获取不完整文件地址
export function getPartUrl(url: string) {
  if (!url)
    return ''
  return url.startsWith('http') ? url.startsWith(window.GLOBAL_CONFIG.oss) ? url.substring(window.GLOBAL_CONFIG.oss.length) : Error('oss 地址错误') : url
}

export function getAvatarUrl(id: string) {
  return `${window.GLOBAL_CONFIG.oss}/user/main/user_avatar?user_id=${id}`
}

export function numberToChinese(num:number) {
    const chineseNums = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    const chineseUnits = ["", "十", "百", "千", "万"];
    let chineseStr = "";
    let unitPos = 0;
    let needZero = false;

    if (num === 0) {
        return chineseNums[0];
    }

    while (num > 0) {
        let digit = num % 10;
        if (digit === 0) {
            if (needZero) {
                chineseStr = chineseNums[0] + chineseStr;
            }
            needZero = true;
        } else {
            chineseStr = chineseNums[digit] + chineseUnits[unitPos] + chineseStr;
            needZero = false;
        }
        unitPos++;
        num = Math.floor(num / 10);
    }

    return chineseStr;
}
