import hud from '#/utils/hud'

export function useFillType() {
  const { formData, editQuestion } = inject('sharedData') as any

  const onTypeChange = () => {
    const defaultFillAnswers = ['']
    if (!editQuestion.value.id) {
      formData.value.fillAnswers = defaultFillAnswers
      return
    }
    // 填空项
    formData.value.fillAnswers = editQuestion.value.answer || defaultFillAnswers
  }

  const submit = () => {
    if (!formData.value.fillAnswers?.length) {
      hud.error('请添加答案')
      return
    }
    for (const option of formData.value.fillAnswers) {
      if (!option) {
        hud.error('请填写答案内容')
        return
      }
    }
    return {
      answer: JSON.stringify(formData.value.fillAnswers),
    }
  }

  return {
    submit,
    onTypeChange,
  }
}
