import { initStationDevices, ndmClient, userClient, type Station, type StationDevices } from '@/apis';
import { unwrapResponse } from '@/utils';

export const getAllDevicesApi = async (options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmDevices/all`;
  const resp = await client.get<StationDevices>(endpoint, { retRaw: true, signal });
  const data = unwrapResponse(resp);
  // 由于各线路后端版本不一致，接口返回的设备类型可能不够完整，需要做一次合并
  return {
    ...initStationDevices(),
    ...data,
  };
};
