import { place } from "@/app/fire/constant/fire";
import {
  axios,
  cameraTypeStatistics,
  casePlaceStatistics,
  caseReasonStatistics,
  caseStatistics,
  caseTimeStatistics,
  sceneStatistics,
} from "@/request";

export type StatisticsParams = {
  startTime: string;
  endTime: string;
};
export type StatisticsItem = { groupKey: string; dataCount: number };
export type StatisticsItems = StatisticsItem[];

const getAmount = (sItem: StatisticsItem, options: string[]) =>
  options.length - options.findIndex((item) => item === sItem.groupKey);

export const getSceneStatistics = async (params: StatisticsParams) =>
  (await axios.post<StatisticsItems>(sceneStatistics, params)).data;

export const getCaseStatistics = async (params: StatisticsParams) =>
  (await axios.post<StatisticsItems>(caseStatistics, params)).data;

const types = ["八目", "双目转台", "激光转台", "激光移动"];
export const getCameraTypeStatistics = async (params: StatisticsParams) => {
  const typeItems = (
    await axios.post<StatisticsItems>(cameraTypeStatistics, params)
  ).data;
  typeItems.sort((a, b) => getAmount(b, types) - getAmount(a, types));
  console.log(typeItems);
  return typeItems;
};

export const getCaseTimeStatistics = async (params: StatisticsParams) =>
  (await axios.post<StatisticsItems>(caseTimeStatistics, params)).data;

const placeLabels = place.map((item) => item.label);
export const getCasePlaceStatistics = async (params: StatisticsParams) => {
  const placeItems = (
    await axios.post<StatisticsItems>(casePlaceStatistics, params)
  ).data;
  placeItems.sort(
    (a, b) => getAmount(b, placeLabels) - getAmount(a, placeLabels)
  );
  return placeItems;
};

export const getCaseReasonStatistics = async (params: StatisticsParams) =>
  (await axios.post<StatisticsItems>(caseReasonStatistics, params)).data;
