import { getToken } from '@/utils/auth';
import { ElNotification } from 'element-plus';
import { useNoticeStore } from '@/store/modules/notice';
import { parseAlarmRealtimeMessage, useAlarmRealtimeBus } from '@/utils/alarmRealtime';

// 初始化
export const initSSE = (url: any) => {
  if (import.meta.env.VITE_APP_SSE === 'false') {
    return;
  }

  url = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID;
  const { data, error } = useEventSource(url, [], {
    autoReconnect: {
      retries: 5,
      delay: 5000,
      onFailed() {
        console.log('Failed to connect after 5 retries');
      }
    }
  });

  watch(error, () => {
    console.log('SSE connection error:', error.value);
    error.value = null;
  });

  watch(data, () => {
    if (!data.value) return;
    const { envelope, displayMessage } = parseAlarmRealtimeMessage(data.value);
    if (envelope) {
      useAlarmRealtimeBus().emit({
        ...envelope,
        channel: envelope.channel ?? 'SSE'
      });
    }
    useNoticeStore().addNotice({
      type: envelope?.eventType,
      message: displayMessage,
      rawMessage: data.value,
      read: false,
      time: new Date().toLocaleString()
    });
    ElNotification({
      title: envelope ? '实时告警' : '消息',
      message: displayMessage,
      type: envelope ? 'warning' : 'success',
      duration: 3000
    });
    data.value = null;
  });
};
