import { cacheFor }                                             from '@/request/api/index';
import { useVisitor }                                           from '@/stores';
import HTTP                                                     from '../alova';
import { fromAnalysisModel, fromReportData, fromSchemeRequest } from '../model';


export function getAnalysisResultsMethod() {
  const Visitor = useVisitor();
  return HTTP.Post(
    `/fdhb-tablet/dialogueManage/dialog/${ Visitor.patientId }/${ Visitor.resultId }`,
    { questions: [], asyncTongueResult: false, patientId: Visitor.patientId },
    {
      meta: { ignoreException: true },
      async transform(data: Record<string, any>, headers) {
        const id = data?.tonguefaceAnalysisReportId;
        const date = data?.tonguefaceAnalysisReportDate;
        const miniProgramURL = data?.tonguefaceAnalysisReportAppletImg;
        data = data.nextQuestions?.find((item: any) => item.classify === 'tongue_result');
        if ( data ) {
          const { show, force } = await miniProgramMethod();
          return {
            id, date, miniProgramURL: show ? miniProgramURL : void 0,
            tongue: fromAnalysisModel('tongue', data),
            face: fromAnalysisModel('face', data),
            payLock: show && force,
          };
        }
        throw { message: `[分析结果] 照片不符合检测要求，图片不是舌头（请拍摄带有舌头的、清晰的彩色照！）` };
      },
    },
  );
}

export function getReportMethod(id: string) {
  const Visitor = useVisitor();
  const params = { healthAnalysisReportId: id, patientId: Visitor.patientId };
  return HTTP.Get(`/fdhb-tablet/analysisManage/getHealRepDetailById`, {
    params,
    async transform(data, headers) {
      const report = fromReportData(<any> data);
      const { show, force } = await miniProgramMethod();
      if ( !show ) { report.miniProgramURL = void 0; }
      report.payLock = show && force;
      return report;
    },
  });
}

export function updateReportMethod(id: string, data: Record<string, any>) {
  const Visitor = useVisitor();
  const params = {
    healthAnalysisReportId: id,
    constitutionGroupImg: data?.constitutionGroupImg,
    factorItemRadarImg: data?.factorItemRadarImg,
    patientId: Visitor.patientId
  };
  return HTTP.Post(`/fdhb-tablet/analysisManage/upConFacImgById`, params, {});
}

export function getReportSchemeMethod(id: string) {
  const Visitor = useVisitor();
  const params = { healthAnalysisReportId: id, patientId: Visitor.patientId };
  return HTTP.Get(`/fdhb-tablet/analysisManage/getCondProgDetailById`, {
    params,
    transform(data: any, headers) {
      return fromSchemeRequest(data);
    },
  });
}


export function miniProgramMethod() {
  return HTTP.Post<{ show: boolean; force: boolean; }, { tabletRequiredPageOperationElements: string[] }>(
    `/fdhb-tablet/warrantManage/getPageSets`, void 0, {
      cacheFor, name: `variate:mini_program`,
      params: { k: 'mini_program' },
      transform(data, headers) {
        return {
          show: data?.tabletRequiredPageOperationElements?.includes('health_analysis_report_page_appletbutton'),
          force: data?.tabletRequiredPageOperationElements?.includes('health_analysis_report_page_appletscan'),
        };
      },
    });
}

