const env = __VITE_ENV__;

export const SUBSCRIBE_MESSAGE_ROLE = {
  PATIENT: "patient",
  DOCTOR: "doctor",
};

export const SUBSCRIBE_MESSAGE_SCENE = {
  DEFAULT: "default",
  LIST: "list",
  CHAT: "chat",
};

export const SUBSCRIBE_MESSAGE_EVENT = {
  PATIENT_CONSULT_APPLY: "patient_consult_apply",
  PATIENT_CHAT_MESSAGE: "patient_chat_message",
  DOCTOR_ACCEPT: "doctor_accept",
  DOCTOR_REJECT: "doctor_reject",
  DOCTOR_CHAT_MESSAGE: "doctor_chat_message",
};

export const SUBSCRIBE_MESSAGE_TEMPLATES = {
  consultationReply: {
    code: "consultationReply",
    role: SUBSCRIBE_MESSAGE_ROLE.PATIENT,
    id:
      env.MP_SUBSCRIBE_TEMPLATE_CONSULT_REPLY ||
      "VF9AC-7Rr3E1drbxBCrxbC-rLTnidmlNXopKReSAd_w",
    name: "咨询回复通知",
    events: [
      SUBSCRIBE_MESSAGE_EVENT.DOCTOR_ACCEPT,
      SUBSCRIBE_MESSAGE_EVENT.DOCTOR_REJECT,
      SUBSCRIBE_MESSAGE_EVENT.DOCTOR_CHAT_MESSAGE,
    ],
    fields: ["患者姓名", "回复时间", "回复者", "所属机构"],
  },
};

export const SUBSCRIBE_MESSAGE_SCENE_TEMPLATE_MAP = {
  [SUBSCRIBE_MESSAGE_ROLE.PATIENT]: {
    [SUBSCRIBE_MESSAGE_SCENE.DEFAULT]: ["consultationReply"],
    [SUBSCRIBE_MESSAGE_SCENE.LIST]: ["consultationReply"],
    [SUBSCRIBE_MESSAGE_SCENE.CHAT]: ["consultationReply"],
  },
};

export function resolveSubscribeTemplates({
  role,
  scene = SUBSCRIBE_MESSAGE_SCENE.DEFAULT,
} = {}) {
  const roleMap = SUBSCRIBE_MESSAGE_SCENE_TEMPLATE_MAP[role] || {};
  const keys =
    roleMap[scene] || roleMap[SUBSCRIBE_MESSAGE_SCENE.DEFAULT] || [];
  const seen = new Set();

  return keys
    .map((key) => SUBSCRIBE_MESSAGE_TEMPLATES[key])
    .filter((item) => item && item.id)
    .filter((item) => {
      if (seen.has(item.id)) return false;
      seen.add(item.id);
      return true;
    });
}
