import { USER_INFO, MAP_POINT_TYPE } from "@/utils/const";
import { httpApi } from "@/common/js/baseUrl";
export function debounce(func, wait, immediate) {
  let timeout;
  return function () {
    let context = this,
      args = arguments;
    if (timeout) clearTimeout(timeout);
    if (immediate) {
      var callNow = !timeout;
      timeout = setTimeout(function () {
        timeout = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args);
      }, wait);
    }
  };
}

export function storageUserInfo(tempUserInfo) {
  let userInfo = uni.getStorageSync(USER_INFO) || {};
  userInfo = {
    ...userInfo,
    ...tempUserInfo,
  };
  uni.setStorageSync(USER_INFO, userInfo);
}

export function maskPhoneCall(phoneNumber) {
  uni.getSystemInfo({
    success: function (res) {
      if (res.platform == "ios") {
        uni.makePhoneCall({
          phoneNumber: phoneNumber,
          success: function () {
            console.log("拨打电话成功");
          },
          fail: function () {},
        });
      } else if (res.platform == "android" || res.platform == "devtools") {
        uni.showModal({
          title: "提示",
          content: `确定拨打${phoneNumber}电话吗？`,
          success(res) {
            if (res.confirm) {
              uni.makePhoneCall({
                phoneNumber: phoneNumber,
                success: function () {
                  console.log("拨打电话成功");
                },
                fail: function () {},
              });
            } else if (res.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      }
    },
    fail: function () {
      uni.showToast({ title: "拨打电话失败", icon: "fail" });
    },
  });
}
/**
 * 将数组array分成长度为subArrLength的小数组并返回新数组
 *  */
export function splitArr(array, subArrLength) {
  let index = 0;
  let newArray = [];
  while (index < array.length) {
    newArray.push(array.slice(index, (index += subArrLength)));
  }
  return newArray;
}

/**
 * 根据不同类型获取打点所需图片
 *  */
export function getPointResByLabel(label) {
  return MAP_POINT_TYPE.find((item) => item.label === label);
}

/* 
 富文本图片地址转换
*/
export function replaceSrc(str) {
  const imgReg = /<[img|video].*?(?:>|\/>)/gi;
  const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/gi;
  return str.replace(imgReg, (img) => {
    // 在这里对图片路径进行加工
    return img.replace(srcReg, (srcMatch, src) => {
      return `src="${httpApi}/file/download?${src.split("?")[1]}"`;
    });
  });
}

/*
 判断h5所处平台  
*/
export function getPlatformEnv() {
  const userAgent = window.navigator.userAgent.toLowerCase();
  // 浙里办APP
  const bIsDtDreamApp = userAgent.indexOf("dtdreamweb") > -1;
  // 浙里办支付宝小程序
  const bIsAlipayMini =
  userAgent.indexOf("miniprogram") > -1 && userAgent.indexOf("alipay") > -1;
  // 浙里办微信小程序
  const bIsWxMini=userAgent.indexOf('micromessenger') > -1
  return {
    bIsDtDreamApp,
    bIsAlipayMini,
    bIsWxMini
  };
}

/* 文件格式转base64 */
export function fileToBase64(file){
  return new Promise((resolve,reject)=>{
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e) {
      resolve(e.target.result)
    }
    reader.onerror=function(e){
      reject(e)
    }
  })
}