import request from '@/utils/request-crm.js';
import CacheTool from '@/utils/cache-tool.js';

const BASE_URL = () => {
  const curEnvConst = CacheTool.getCurEnvConst();
  return curEnvConst.BASE_URL;
};
const DEFAULT_CONFIG = {
  refreshTokenUrl: `/oauth/v1/oauth/renewToken?refreshToken=`,
};
// 根据groupId, kipUserId查询crm会员
export function crmGetMemberByKipProfileId(baseUrl, params) {
  const url = `${baseUrl}api/1.0/member/getKipMemberInfo`;
  return request.post(url, params);
}

// 根据brandId，kipUserId 创建会员
export function crmCreateNewMemberByBrand(baseUrl, params) {
  const url = `${baseUrl}api/1.0/login/saveMember`;
  return request.post(url, params);
}

export function crmGetMiniCodeParam(baseUrl, params) {
  const url = `${baseUrl}api/1.0/login/getMiniCodeParam`;
  return request.post(url, params);
}

export function crmUpdateMemberCompleted(baseUrl, params) {
  const { kipUserId, groupId, mallid } = params || {};
  if (!kipUserId || !groupId || !mallid) {
    console.error(`===>[参数缺失] kipUserId || groupId || mallid 不能为空`);
    return;
  }
  const url = `${baseUrl}api/1.0/member/isCompleted`;
  return request.put(url, params);
}

export function memberCertification(baseUrl, params) {
  const url = `${baseUrl}api/1.0/member/memberCertification`;
  return request.post(url, params);
}

// 退出接口
export function crmLogout(baseUrl, params) {
  const { openid } = params || {};
  if (!openid) {
    console.error(`===>[参数缺失] kipUserId & groupId 不能为空`);
    return;
  }
  const url = `${baseUrl}api/1.0/login/logout`;
  return request.post(url, params);
}

export function crmLogOff(baseUrl, params) {
  const { mobile } = params || {};
  if (!mobile) {
    console.error(`===>[参数缺失] mobile 不能为空`);
    return;
  }
  const url = `${baseUrl}api/1.0/login/logOff`;
  return request.post(url, params);
}

export function crmGetOpenId(baseUrl, params) {
  const { code, mallid } = params || {};
  if (!code) {
    console.error(`===>[参数缺失] code 不能为空`);
    return;
  }
  if (!mallid) {
    console.error(`===>[参数缺失] mallid 不能为空`);
    return;
  }
  const url = `${baseUrl}api/1.0/login/onLogin`;
  return request.post(url, params);
}
export function crmUpdateMobilePhone(params) {
  const url = `${BASE_URL()}api/1.0/member/mobilePhone`;
  return request.put(url, params);
}

// 获取腾讯key
export function getTencentKey(params) {
  const url = `${BASE_URL()}api/1.0/tencent/list`;
  return request.get(url, params);
}

/**
 * 只返回crm特有的信息，基础信息不返回
 * @param {*} params
 */
export function crmQueryMemberInfo(params) {
  const url = `${BASE_URL()}api/1.0/member/queryMemberInfo`;
  return request.get(url, params);
}

// 获取会员详细信息用于会员注册后完善信息操作，在obj.extend属性中，外层是基础会员信息
export function kipGetUserDetail(params) {
  // https://apim.kerryplus.com/c/api/profile/v1/customer/crm/current
  const url = `${window.profileApi}/profile/v1/customer/crm/current`;
  return request.get(url, params, { ...DEFAULT_CONFIG });
}

export function crmSaveMemberSource(params) {
  const url = `${BASE_URL}api/1.0/login/updateRegister`;
  return request.post(url, params);
}


// https://kerryprops.atlassian.net/browse/SCRM-4004
// 临停｜UI获取纸质券描述｜增加API
export function paperCouponDescription( {groupId, mallId}) {
  const url = `${window.cmrApi}/1.0/park/paperCouponDescription?groupId=${groupId}&mallId=${mallId}`;
  return request.post(url);
}