import request from "./http";

const urlsConfig = {
  corp: {
    getCorpMemberHomepageInfo: 'getCorpMemberHomepageInfo',
    getTeamBaseInfo: 'getTeamBaseInfo',
    getTeamData: 'getTeamData',
    queryWxJoinedTeams: 'queryWxJoinedTeams',
    wxAppLogin: 'wxAppLogin',
    getTeamHealthTemplate: 'getTeamHealthTemplate',
    getTeamHealthTemps: "getTeamHealthTemps",
    getCorpMemberJob: "getCorpMemberJob",
    bindWxappWithTeam: 'bindWxappWithTeam',
    getWxappRelateTeams: 'getWxappRelateTeams',
    getTeamMemberAvatarsAndName: "getTeamMemberAvatarsAndName",
    getMiniAppHomeStats: "getMiniAppHomeStats",
    getResponsiblePerson: 'getTeamResponsiblePerson',
    relateWxappTeamByExternalUserId: 'relateWxappTeamByExternalUserId',
    getBusinessCard: 'getBusinessCard',
    recordBusinessCardBehavior: 'recordBusinessCardBehavior',
    getJoinedTeams: 'getJoinedTeams',
    getCorpBusinessCardStatus: 'getCorpBusinessCardStatus',
  },

  knowledgeBase: {
    getArticleByIds: 'getArticleByIds',
    getCommonPhrases: 'getCommonPhrases',
    saveCommonPhrase: 'saveCommonPhrase',
    deleteCommonPhrase: 'deleteCommonPhrase',
    getCommonPhraseCategories: 'getCommonPhraseCategories',
    saveCommonPhraseCategory: 'saveCommonPhraseCategory',
    // 个人常用语接口
    getPersonalPhrases: 'getPersonalPhrases',
    savePersonalPhrase: 'savePersonalPhrase',
    deletePersonalPhrase: 'deletePersonalPhrase',
    getPersonalPhraseCategories: 'getPersonalPhraseCategories',
    savePersonalPhraseCategory: 'savePersonalPhraseCategory',
    deletePersonalPhraseCategory: 'deletePersonalPhraseCategory',
    // 宣教文章接口
    getArticleCateList: 'getArticleCateList',
    getArticleList: 'getArticleList',
    getArticle: 'getArticle',
    addArticleSendRecord: 'addArticleSendRecord',
    addArticleReadRecord: 'addArticleReadRecord',
    getMiniAppReceivedArticleList: 'getMiniAppReceivedArticleList',
    getPageDisease: 'getPageDisease',
    searchRateList: 'searchRateList',
    submitRateRecord: 'submitRateRecord',
    getRateRecord: 'getRateRecord',
    relateWechatAccountWithTeam: 'relateWechatAccountWithTeam'
  },
  member: {
    addCustomer: 'add',
    addMedicalRecord: 'addMedicalRecord',
    authCustomerToTeam: 'authCustomerToTeam', // 授权客户到团队
    bindMiniAppArchive: "bindMiniAppArchive",
    getCustomerByCustomerId: 'getCustomerByCustomerId',
    getMiniAppCustomers: 'getMiniAppCustomers',
    getTeamCustomers: 'getTeamCustomers',
    getUnbindMiniAppCustomers: 'getUnbindMiniAppCustomers',
    getCustomerMedicalRecord: 'getCustomerMedicalRecord',
    getMedicalRecordById: 'getMedicalRecordById',
    unbindMiniAppArchive: 'unbindMiniAppArchive',
    updateMedicalRecord: 'updateMedicalRecord',
    getUnionidToExternalUserid: 'getUnionidToExternalUserid',
    syncWxappExternalUserRelation: 'syncWxappExternalUserRelation',
    getWxAppCustomerCount: "getWxAppCustomerCount",
    updateCustomer: 'update',
    getRefrencePeople: 'getRefrencePeople',
  },
  wecom: {
    addContactWay: 'getCorpFriendQrcode'
  },
  im: {
    getUserSig: 'getUserSig',
    sendSystemMessage: "sendSystemMessage",
    getChatRecordsByGroupId: "getChatRecordsByGroupId",
    sendConsultRejectedMessage: "sendConsultRejectedMessage",
    endConsultation: "endConsultation",
    getGroupListByGroupId: "getGroupListByGroupId",
    createConsultGroup: "createConsultGroup",
    cancelConsultApplication: "cancelConsultApplication",
    getGroupList: "getGroupList",
    getConversationSubscribeConfig: "getConversationSubscribeConfig",
    saveConversationSubscribeResult: "saveConversationSubscribeResult",
    sendConversationSubscribeEvent: "sendConversationSubscribeEvent"
  },
  survery: {
    getMiniAppReceivedSurveryList: 'getMiniAppReceivedSurveryList',
    getSurveryDetail: 'getDetail',
    answerSurvery: 'answerSurvery',
    getAnswerRecord: 'getAnswerRecord',
    getAnswer: 'getAnswer'
  }
}
const urls = Object.keys(urlsConfig).reduce((acc, path) => {
  const config = urlsConfig[path] || {};
  const keys = Object.keys(config);
  keys.forEach((key) => {
    const data = acc[key];
    if (data) {
      throw new Error(`${data[0]}.${data[1]}和${path}.${url}重复了`)
    }
    acc[key] = [path, config[key]]
    return acc
  })
  return acc
}, {})

export default async function api(urlId, data = {}, loading = true) {
  const config = urls[urlId];
  if (!config) {
    throw new Error(`Unknown URL ID: ${urlId}`);
  }
  const [path, type] = config;
  return request({
    url: `/getYoucanData/${path}`,
    data: {
      ...data,
      type,
    }
  }, loading)
}
