import { useVisitor }                                 from '@/stores';
import HTTP                                           from '../alova';
import type { QuestionnaireStorage }                  from '../model';
import { fromQuestionnaireData, toQuestionnaireData } from '../model';


const visitor = useVisitor();
let storage: Pick<QuestionnaireStorage, 'dialogId'> & { questions: QuestionnaireStorage['questions'][] } = { questions: [] };

export function questionnaireMethod(data = []) {
  if ( !data?.length ) { storage = { questions: [] }; }
  return HTTP.Post(
    `/fdhb-tablet/dialogueManage/dialog/${ visitor.patientId }/${ visitor.resultId }`,
    toQuestionnaireData(data, storage),
    {
      transform(data: Record<string, any>, headers) {
        const { storage: _storage, model } = fromQuestionnaireData(data);
        storage = _storage;
        return model;
      },
    },
  );
}
