import api from "@/request";
import Taro from "@tarojs/taro";

type EnumType =
  'CheckStatus'
  | 'DeleteFlag'
  | 'IsEnable'
  | 'IsOrNot'
  | 'Sex'
  | 'ServiceProjectType'
  | 'MiniProgramUserIdentity'

export const initEnum = () => {
  api.get<Record<EnumType, SelectNodeVo<any>[]>>('/common/enums').then(resp => {
    Taro.setStorageSync('enumMap', resp.data)
  })
};

export const enumSelectNodes = <T, >(enumType: EnumType): SelectNodeVo<T>[] => Taro.getStorageSync('enumMap')?.[enumType]

export const getEnumByValue = <T, >(enumType: EnumType, value: T): null | SelectNodeVo<T> => {
  const enumList = enumSelectNodes<T>(enumType);
  if (!enumList) {
    return null;
  }
  for (let i = 0; i < enumList.length; i++) {
    if (value === enumList[i].value) {
      return enumList[i]
    }
  }
  return null;
};

export const getEnumLabelByValue = <T, >(enumType: EnumType, value: T): string => {
  const enums = getEnumByValue(enumType, value)
  if (!enums) {
    return '-'
  }
  return enums.label;
};
