/**
 * 接诊页与传方悬浮窗之间的导入桥接
 * 悬浮窗调用 invokeDoctorTransferImport，接诊页 onMounted 时注册 handler
 */

export type DoctorTransferImportPayload = {
  transferId: number;
  registerId: number;
};

export type DoctorTransferImportHandler = (
  payload: DoctorTransferImportPayload,
) => void | Promise<void>;

let importHandler: DoctorTransferImportHandler | null = null;

/** 接诊页挂载时注册导入处理器 */
export function registerDoctorTransferImportHandler(
  fn: DoctorTransferImportHandler,
) {
  importHandler = fn;
}

/** 接诊页卸载时注销，避免误导入到其他页面 */
export function unregisterDoctorTransferImportHandler() {
  importHandler = null;
}

/**
 * 悬浮窗触发导入：返回 false 表示接诊页未就绪
 */
export function invokeDoctorTransferImport(
  payload: DoctorTransferImportPayload,
): boolean {
  if (!importHandler) {
    return false;
  }
  void importHandler(payload);
  return true;
}
