import { getCameraState } from '@/api/camera/camera-overview';
import { ref } from 'vue';

/** 轮询相机异常的频率，5s */
const INTERVAL_TIME = 5000;

export const useCameraStatus = () => {
  const noNetworkingNum = ref<number>(0);
  let interval;

  const getState = (codeList, callback) => {
    getCameraState({ cameraCodeList: codeList }).then((res) => {
      const filterData = res.filter((item) => item.status === 1);
      noNetworkingNum.value = filterData.length;
      callback(res);
    });
  };

  const openInterval = (codeList, callback) => {
    setTimeout(() => {
      getState(codeList, callback);
    }, 500);
    interval = setInterval(() => {
      getState(codeList, callback);
    }, INTERVAL_TIME);
  };

  const closeInterval = () => {
    clearInterval(interval);
  };

  return {
    noNetworkingNum,
    openInterval,
    closeInterval,
    getState,
  };
};

export default useCameraStatus;
