import queryString from 'query-string';
import monitor from "../../../core/utils/alipayLogger";
let myAppId = null; // 获取当前小程序appId

export function getMyMiniAppId() {
  if (myAppId === null) {
    myAppId = my.getAppIdSync().appId;
  }

  return myAppId;
}
export function reportCmPV_YL(params) {
  return reportCmPV(params, 'h5SE_YL');
} // 页面pv上报云监控

export function reportCmPV({
  pageCode,
  title,
  query
}, tag) {
  const url = 'pages/web-view/index';

  if (!title) {
    // eslint-disable-next-line no-param-reassign
    title = url;
  }

  if (!query) {
    // 如果没有query直接跳SYYJS服务到首页
    // eslint-disable-next-line no-param-reassign,prefer-destructuring
    query = {
      serviceCode: 'SYYJS'
    };
  }

  const page = pageCode || title;
  const newMyAppId = getMyMiniAppId();
  let encodedUrl;

  if (query && Object.keys(query).length > 0) {
    encodedUrl = encodeURIComponent(`${url}?${queryString.stringify(query)}`);
  } else {
    encodedUrl = encodeURIComponent(`${url}`);
  }

  const myUrl = `alipays://platformapi/startapp?appId=${newMyAppId}&page=${encodedUrl}`; // console.log(`[YWTB] log: page: ${page}, url: ${url}, myUrl: ${myUrl}}`);

  if (tag === null) {
    // eslint-disable-next-line no-param-reassign
    tag = 'h5SE';
  }

  monitor._lgPV({
    page,
    url,
    c1: tag,
    c2: myUrl
  });
}
export const reportApi = api => {
  monitor.api({
    api,
    success: true,
    c1: 'taSR_YL',
    time: 2000
  });
};