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.DOCTOR,
    id:
      env.MP_SUBSCRIBE_TEMPLATE_CONSULT_REPLY ||
      "1etY1Mdfz9c0xJlI8kx79Re6uKmc3BoHJBHsrXeiCm4",
    name: "咨询回复通知",
    events: [
      SUBSCRIBE_MESSAGE_EVENT.PATIENT_CONSULT_APPLY,
      SUBSCRIBE_MESSAGE_EVENT.PATIENT_CHAT_MESSAGE,
    ],
    fields: ["咨询人", "回复时间", "回复内容"],
  },
};

export const SUBSCRIBE_MESSAGE_SCENE_TEMPLATE_MAP = {
  [SUBSCRIBE_MESSAGE_ROLE.DOCTOR]: {
    [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;
    });
}
