var SYS_PRODUCTYPE = 'TEST'

var ua = window.navigator.userAgent.toLowerCase();
// var uaArr = ua.split(';');
// var isWebview = /supportJDSHWK\/1/i.test(ua); //iosapp,webview内核是否为wk
// console.log(isWebview, 'isWebview')

const isWebview = (name) => {
  ua = ua || navigator.userAgent;
  if (name === 'xview') return typeof window.XView !== 'undefined';
  if (name === 'wk') return !!ua.match(/supportjdshwk/i) || window._is_jdsh_wkwebview == 1;
  return false;
}

//关窗方法
const XViewClose = () => {
  console.log(isWebview, 'isWebview111')

  if (!isWebview('xview')) return;
  if (isWebview('wk')) {
    window.webkit.messageHandlers.XView.postMessage({
      "method": "close",
    });
  } else {
    window.XView && window.XView.close();
  }
}
const closeGiftPop = (content) => {
  var vm = this;
  // $(".ui-modal-body").removeClass('animate__' + vm.styleInfo.intoEffectType).addClass('animate__' + vm.styleInfo.outEffectType);
  // 关注信息同步给京东
  if (SYS_PRODUCTYPE != "DEVELOP") {
    if (!vm.hasFollow) {
      // window.location.href = 'xview://search?request={"action":"shopHome_followShop","des":"followShop_status","status":"true"}'
      window.location.href =
        'xview://search?request={"action":"shopHome_followShop","des":"followShop_status","status":"true","sourceType":"ISVRedEnvelope"}'
    }
  }
  setTimeout(function() {
    vm.isShowApp = false
    if (SYS_PRODUCTYPE != "DEVELOP") {
      XViewClose();
    }
  }, 1000)
}

const jumpGetCoupon = (url, callBack) => {
  console.log(url, '跳转方法')
  url = url.trim()
  let linkUrl = '';
  if(url && url.indexOf('path=') > -1 && url.indexOf('mini-app-static.jd.com') > -1){
    // 原始链接
    const urlObj = new URL(url);
    const urlParams = {
      appId: urlObj.searchParams.get('appId'),
      vapptype: urlObj.searchParams.get('vapptype')?urlObj.searchParams.get('vapptype'):'1',
      path: urlObj.searchParams.get('path')
    };
    const pathParam = urlObj.searchParams.get('path');
    if (pathParam) {
      // 解码 path 参数
      const decodedPath = decodeURIComponent(pathParam);
      // 检查 path 中是否包含查询参数
      if (decodedPath.includes('?')) {
        // 提取查询参数部分
        const pathQuery = decodedPath.split('?')[1];
        
        // 解析 path 中的查询参数
        const pathParams = new URLSearchParams(pathQuery);
        
        // 将 path 中的参数添加到 urlParams 对象中
        urlParams.param = {};
        for (const [key, value] of pathParams) {
          urlParams.param[key] = value;
        }
      }
    }
    if (urlParams.appId) {
      var params = {
        category:"jump",
        des:"jdmp",
        appId:urlParams.appId,
        path: urlParams.path,
        vapptype: urlParams.vapptype,
        pageAlias:"",
        param:urlParams.param
      }
      params = JSON.stringify(params)
      linkUrl = "openapp.jdmobile://virtual?params=" + encodeURIComponent(params);
    }else{
      linkUrl= url;
    }
  }else{
    var params = {
      category: "jump",
      des: "getCoupon",
      sourceValue: "sourceValue_test",
      sourceType: "sourceType_test",
      url: decodeURIComponent(url)
    };
    params = JSON.stringify(params)
    linkUrl = "openapp.jdmobile://virtual?params=" + encodeURIComponent(params);
  }
  window.location.href = linkUrl;
}

export {
  jumpGetCoupon,
  closeGiftPop,
  XViewClose
}
