/**
 * 延迟执行
 * @param {Number} ms 
 * @returns 
 */
export function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
/**
 * 解析查询字符串
 * @param {string} urlOrQueryString 
 * @returns {Object} 查询参数对象
 */
export function parseQueryString(urlOrQueryString) {
    // 如果传入的是完整URL（如 "/path?name=value"），提取查询部分
    let queryString = urlOrQueryString;
    const questionMarkIndex = queryString.indexOf('?');
    if (questionMarkIndex !== -1) {
        queryString = queryString.slice(questionMarkIndex + 1);
    }

    const params = {};
    if (!queryString) return params; // 如果没有查询参数，返回空对象

    const pairs = queryString.split('&');
    for (const pair of pairs) {
        const [key, value] = pair.split('=');
        // 解码 URI 组件，并处理无值情况（如 "key" 而不是 "key=value"）
        params[key] = value ? decodeURIComponent(value) : '';
    }

    return params;
}
/**
 * 提示
 * @param {string} msg 
 */
export function msg(msg) {
    let title = msg || ''
    let icon = 'none'
    let mask = false
    let duration = 1500
    let position = 'center'
    uni.showToast({
        title,
        icon,
        mask,
        duration,
        position,
    });
};
/**
 * 生成GUID
 * @returns {string} 返回格式为'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'的GUID
 */
export function generateGUID() {
    return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function(c) {
        const r = Math.random() * 16 | 0;
        const v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
/**
 * 生成指定长度的随机GUID
 * @param {Number} length 指定GUID的长度
 * @returns {string} 返回指定长度的随机GUID
 */
export function generateGUIDWithLength(length = 32) {
    let guid = '';
    const characters = 'abcdef0123456789';
    
    // 生成指定长度的随机字符
    for (let i = 0; i < length; i++) {
        guid += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    
    // 如果长度大于等于36，则添加破折号使其格式类似标准GUID
    if (length >= 36) {
        return guid.substring(0, 8) + '-' + 
               guid.substring(8, 12) + '-' + 
               guid.substring(12, 16) + '-' + 
               guid.substring(16, 20) + '-' + 
               guid.substring(20, 32) + 
               guid.substring(32);
    }
    
    return guid;
}
/**
 * 生成固定长度的随机字符串
 * @param {Number} length 字符串长度，默认为16
 * @param {Boolean} includeSpecial 是否包含特殊字符，默认为false
 * @returns {string} 返回生成的随机字符串
 */
export function generateRandomString(length = 16, includeSpecial = false) {
    let result = '';
    let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    
    // 如果需要包含特殊字符
    if (includeSpecial) {
        characters += '!@#$%^&*()_+~`|}{[]:;?><,./-=';
    }
    
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    
    return result;
}