export const Debounce = (fn, wait) => {
    let delay = wait|| 500
    let timer
    return function () {
        let args = arguments;
        if (timer) {
            clearTimeout(timer)
        }
       
        let callNow = !timer
        timer = setTimeout(() => {
            timer = null
        }, delay)
        if (callNow) fn.apply(this, args)
    }
}

/**
 * 处理头像/图片 URL
 * @param {String} url 图片路径
 * @param {String} baseUrl 基础路径
 * @param {String} defaultUrl 默认图片路径
 */
export const getHeadImgUrl = (url, baseUrl, defaultUrl = "/static/common/images/avatar_default2.png") => {
    if (!url) return defaultUrl;
    if (url.startsWith('http') || url.startsWith('https') || url.startsWith('blob:')) return url;

    // 统一处理反斜杠：将所有的 \ 替换为 /
    let cleanUrl = url.replace(/\\/g, '/');

    // 处理 baseUrl: 替换反斜杠并去除末尾斜杠
    let cleanBaseUrl = '';
    if (baseUrl) {
        cleanBaseUrl = baseUrl.replace(/\\/g, '/').replace(/\/$/, '');
    }
    
    // 情况三：headSculptureUrl = "/UploadImages/tx.jpg"
    if (cleanUrl.startsWith('/')) {
        return cleanBaseUrl + cleanUrl;
    }
    
    // 情况二：headSculptureUrl = "UploadImages/tx.jpg"
    if (cleanUrl.includes('/')) {
        return cleanBaseUrl + '/' + cleanUrl;
    }
    
    // 情况一：headSculptureUrl = "tx.jpg"
    return cleanBaseUrl + '/UploadImages/' + cleanUrl;
}
