import { ref } from "vue";
import {
  DEFAULT_LABEL,
  DATA_CHECK_TYPE,
  ABSENT_CHECK_TYPE,
  PAPER_TYPE_STATUS,
  EXAM_STATUS,
  EXAM_SIMPLE_STATUS,
  IMAGE_TYPE,
} from "@/constants/enumerate";

const dicts = {
  DATA_CHECK_TYPE,
  ABSENT_CHECK_TYPE,
  PAPER_TYPE_STATUS,
  EXAM_STATUS,
  EXAM_SIMPLE_STATUS,
  IMAGE_TYPE,
};

type DictTypeType = keyof typeof dicts;

/**
 * 将字典数据转成为option列表数据
 * @param data 字典数据
 * @returns option列表
 */
export function dictToOption(dict: Record<any, string>) {
  const options = [] as Array<{
    value: number | string | boolean;
    label: string;
  }>;
  Object.keys(dict).forEach((k) => {
    options.push({
      value: k,
      label: dict[k],
    });
  });
  return options;
}

export default function useDictOption(dictType: DictTypeType) {
  const optionList = ref<SelectOption[]>(dictToOption(dicts[dictType] || {}));

  function getLabel(val: string): string {
    // @ts-ignore
    return dicts[dictType] ? dicts[dictType][val] : DEFAULT_LABEL;
  }

  return {
    optionList,
    getLabel,
  };
}
