import type { IMatrixListOption, IQuestion, IQuestionOption } from '@/types/question';
import { computed, onUnmounted, type Ref } from 'vue';

function useGetList(question: Ref<IQuestion>) {
  let filterOptions = <IMatrixListOption[]>[];

  const _list = computed(() => {
    //   有意外情况，可能存在 hideOptions
    if (question.value.hideOptions?.length) {
      // 适用于单个选项的，后续再添加逻辑
      const [list] = question.value.list;
      const options = list.options;

      // 取出 hideOptions，然后提出相应的元素，需要重置 hideOptions。
      // 不然会出现再次进入无法得到原来所有的选项
      const hideOptions = question.value.hideOptions;
      // 备份 options
      filterOptions = [...options];

      list.options = options.filter((option) => {
        return !hideOptions.includes(option.option_key);
      });

      question.value.hideOptions = [];
    } else {
      return question.value.list;
    }
  });

  onUnmounted(() => {
    if (!filterOptions.length) return;

    // 把 options 还原
    question.value.list[0].options = filterOptions;
  });
  return _list;
}

export { useGetList };
