/**
 * 灾害预警公用hook
 */
import { ref } from 'vue';
import type { SysDictDataDetail } from '@/api/dict';
import { queryDictTypeDetail } from '@/api/dict';
import { DICT_CODE } from '@/constant/dict';

export const useDisasterWarningHook = () => {
  // 预警类型
  const warningTypeDice = ref<SysDictDataDetail[]>([]);
  // 灾害类型
  const disasterTypeDice = ref<SysDictDataDetail[]>([]);
  // 灾害等级
  const disasterLevelDice = ref<SysDictDataDetail[]>([]);
  const getWarningInfoDict = async () => {
    const disasterTypeRes = await queryDictTypeDetail(DICT_CODE.WEATHER_WARNING);
    warningTypeDice.value = disasterTypeRes.sysDictDataList;
  };
  const getDisasterTypeDict = async () => {
    const disasterTypeRes = await queryDictTypeDetail(DICT_CODE.DISASTER_TYPE);
    disasterTypeDice.value = disasterTypeRes.sysDictDataList;
  };
  const getDisasterLevelDict = async () => {
    const disasterLevelRes = await queryDictTypeDetail(DICT_CODE.DISASTER_LEVEL);
    disasterLevelDice.value = disasterLevelRes.sysDictDataList;
  };
  const getWarningType = (warningType: string) => {
    return warningTypeDice.value.find((item) => item.itemCode === warningType)?.itemValue;
  };
  const getWarningIcon = (warningType: string) => {
    return warningTypeDice.value.find((item) => item.itemCode === warningType)?.imageUrl;
  };
  const getDisasterType = (disasterType: string) => {
    return disasterTypeDice.value.find((item) => item.itemCode === disasterType)?.itemValue;
  };
  const getDisasterLevel = (disasterCode: string) => {
    return disasterLevelDice.value.find((item) => item.itemCode === disasterCode)?.itemValue;
  };
  return {
    warningTypeDice,
    disasterTypeDice,
    disasterLevelDice,
    getWarningInfoDict,
    getDisasterTypeDict,
    getDisasterLevelDict,
    getWarningType,
    getWarningIcon,
    getDisasterType,
    getDisasterLevel,
  };
};
