import cacheManager from "./cacheManager.js"

/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  // FIXME   拼接参数 
  return result
}

/**
 * @summary 获取请求异常与正常返回
 * @param {Object} promise
 */
export function catchError(promise) {
	return new Promise((resolve,reject) => {
		promise.then(data => {
			resolve([undefined, data.data]) 
		}).catch(err => {
			reject([err]) 
		})
	})
}

export function debounce(func, wait) {
  let timeout;

  return function(...args) {
    // 清除之前的定时器
    clearTimeout(timeout);

    // 设置新的定时器
    timeout = setTimeout(() => {
      func.apply(this, args);
    }, wait);
  };
}

export  function formatDuration(duration = 0) {
      return Math.round(duration / 60);
    }
	
		
export function getStaticUrl(url) {
	let result = '';
	// #ifdef H5
	result = `/mdist/${url}`
	// #endif
	
	// #ifdef APP-PLUS
	result = url
	// #endif
	
	return result;
}
	
export function formatSecondsToCnhms(value, isNoZero) {
    if (!value || value == 0) {
        return '';
    }
    let result = parseInt(value);
    let h = isNoZero ? Math.floor(result / 3600) : Math.floor(result / 3600) < 10
        ? '0' + Math.floor(result / 3600)
        : Math.floor(result / 3600);
    let m = isNoZero ? Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)) < 10
        ? '0' + Math.floor((result / 60 % 60))
        : Math.floor((result / 60 % 60));
    let s = isNoZero ? Math.floor((result % 60)) : Math.floor((result % 60)) < 10
        ? '0' + Math.floor((result % 60))
        : Math.floor((result % 60));
    if (isNoZero) {
        result = '';
        if (h !== 0) {
            result += `${h}时`;
            // 判断如果有时有秒，分钟为0也显示
            if (s !== 0) {
                // 分为零也显示
                if (m === 0) {
                    result += `${m}分`;
                }

            }

        }
        // 分钟不为零显示
        if (m !== 0) {
            result += `${m}分`;
        }
        // 秒不为零显示
        if (s !== 0) {
            result += `${s}秒`;
        }
    } else {
        result = `${h}时${m}分${s}秒`;
    }
    return result;
}


// utils/jsonp.js
export function jsonp(url, params, callbackName = 'jsonp_callback') {
  return new Promise((resolve, reject) => {
    const queryString = new URLSearchParams(params).toString();
    const fullUrl = `${url}?${queryString}&callback=${callbackName}`;

    window[callbackName] = (data) => {
      resolve(data);
      delete window[callbackName];
      if (script.parentNode) document.body.removeChild(script);
    };

    const script = document.createElement('script');
    script.src = fullUrl;
    script.onerror = (error) => reject(error);
    document.body.appendChild(script);
  });
}
export function jsonp2(url, params, callbackName = 'jsonp_callback') {
  return new Promise((resolve, reject) => {
      // 创建一个全局回调函数
      const callbackName = `jsonp_callback_${Date.now()}`;
      window[callbackName] = function (data) {
        resolve(data); // 解析数据
        delete window[callbackName]; // 清理全局回调函数
        document.body.removeChild(script); // 移除 script 标签
      };
  
      // 创建 script 标签
      const script = document.createElement('script');
      script.src = `${url}&callback=${callbackName}`;
      script.onerror = () => {
        reject(new Error('JSONP 请求失败'));
        delete window[callbackName]; // 清理全局回调函数
        document.body.removeChild(script); // 移除 script 标签
      };
  
      // 将 script 标签添加到文档中
      document.body.appendChild(script);
    });
}
