import { ref } from 'vue';

import { useBaseStore } from '#/store';
import { TMS_DIC, labelOfDic, loadTmsDic, type TmsDicOpt } from '#/views/x/tms/shared/dic';

type StatusOpt = TmsDicOpt & { tip?: string; tipColor?: string };

function withStatusTip(list: TmsDicOpt[]): StatusOpt[] {
  return list.map((x) => {
    const code = x.enCode || x.id;
    if (code === 'open') return { ...x, tip: '学生可以模拟', tipColor: 'green' };
    if (code === 'closed') return { ...x, tip: '学生不能模拟', tipColor: 'red' };
    return x;
  });
}

export const QUESTION_TYPE_OPTIONS = ref<TmsDicOpt[]>([]);
export const DIFFICULTY_OPTIONS = ref<TmsDicOpt[]>([]);
export const SOURCE_OPTIONS = ref<TmsDicOpt[]>([]);
export const STATUS_OPTIONS = ref<StatusOpt[]>([]);

export async function loadQuestionDics() {
  const baseStore = useBaseStore();
  const [questionType, difficulty, source, status] = await Promise.all([
    loadTmsDic(baseStore, TMS_DIC.questionType),
    loadTmsDic(baseStore, TMS_DIC.difficulty),
    loadTmsDic(baseStore, TMS_DIC.questionSource),
    loadTmsDic(baseStore, TMS_DIC.openClosedInvalid),
  ]);
  QUESTION_TYPE_OPTIONS.value = questionType;
  DIFFICULTY_OPTIONS.value = difficulty;
  SOURCE_OPTIONS.value = source;
  STATUS_OPTIONS.value = withStatusTip(status);
}

export const OPTION_LABELS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

export function labelOfType(type?: string) {
  return labelOfDic(QUESTION_TYPE_OPTIONS.value, type);
}

export function labelOfStatus(status?: string) {
  return labelOfDic(STATUS_OPTIONS.value, status);
}

export function labelOfDifficulty(v?: string) {
  return labelOfDic(DIFFICULTY_OPTIONS.value, v);
}

export function labelOfSource(v?: string) {
  return labelOfDic(SOURCE_OPTIONS.value, v);
}
