import { type LineAlarms, type Station, type StationAlarms } from '@/apis';
import { NDM_ALARM_STORE_ID } from '@/constants';
import { defineStore } from 'pinia';
import { shallowRef, triggerRef } from 'vue';

export const useAlarmStore = defineStore(
  NDM_ALARM_STORE_ID,
  () => {
    // 全线所有车站的告警
    const lineAlarms = shallowRef<LineAlarms>({}); // 数据量很大所以用shallowRef配合triggerRef优化性能
    const setLineAlarms = (alarms: LineAlarms) => {
      lineAlarms.value = alarms;
      triggerRef(lineAlarms);
    };
    const setStationAlarms = (stationCode: Station['code'], alarms: StationAlarms) => {
      lineAlarms.value[stationCode] = alarms;
      triggerRef(lineAlarms);
    };

    return {
      lineAlarms,
      setLineAlarms,
      setStationAlarms,
    };
  },
  {
    persistToIndexedDB: true,
  },
);
