import { fromAnalysisModel } from '@/request/model/analysis.model';


export function fromReportData(data: Record<string, any>) {
  return {
    id: data?.healthAnalysisReportId,
    date: data?.reportTime,
    scheme: data?.isHaveConditioningProgram === 'Y' && data?.isConfirmConditioningProgram === 'Y' ? data?.healthAnalysisReportId : null,

    [ '结果' ]: data?.willillStateName,
    [ '程度' ]: data?.willillDegreeName,
    [ '类型' ]: data?.willillSocialName,
    [ '表现' ]: data?.willillFunctionName,
    [ '体质' ]: data?.constitutionGroupName,
    [ '体质描述' ]: data?.constitutionGroupDefinition,
    [ '体质图表' ]: fromReportPhysiqueChartData(data?.allConstitutionGroups),

    descriptionsTable: {
      column: [],
      data: [
        [ '总体特征', data?.constitutionGroupGeneralCharacteristics ],
        [ '形体特征', data?.constitutionGroupPhysicalCharacteristics ],
        [ '精神特征', data?.constitutionGroupPsychicCharacteristics ],
        [ '常见表现', data?.constitutionGroupCommonManifestations ],
        [ '发病倾向', data?.constitutionGroupDiseaseTendency ],
        [ '环境适应能力', data?.constitutionGroupAdaptability ],
      ],
    },

    tongue: fromAnalysisModel('tongue', data),
    face: fromAnalysisModel('face', data),
    pulse: fromAnalysisModel('pulse', data),

    [ '中医证素' ]: data?.factorItems?.map?.((item: Record<string, any>) => {
      return { label: item?.factorItemName, value: item?.factorItemDescription, score: +item?.score };
    }),
    [ '中医证型' ]: data?.diagnoseSyndromes?.map?.((item: Record<string, any>) => {
      return { label: item?.diagnoseSyndromeName, value: item?.diagnoseSyndromeAnalysis };
    }),

    constitutionGroupImg: data?.constitutionGroupImg,
    factorItemRadarImg: data?.factorItemRadarImg,
    reportURL: data?.reportPdfUrl,
    miniProgramURL: data?.appletImg,
    payLock: data?.payLock,

    page: { jumpable: true, printable: true },
  };
}


export interface ReportTongueException {
  title: string;
  cover: string;
  descriptions: string[];
  tags: string[];
}

function fromReportTongueExceptionData(exception: ReportTongueException[], data?: { actualList: Record<string, any>[] }) {
  const values = data?.actualList?.map(item => {
    let title: string = item?.actualValue ?? '';
    const suffix = item?.contrast ?? 's';
    if ( suffix !== 's' ) {
      title += ` (${ suffix || '' }) `;
      exception.push({
        title, cover: item.splitImage,
        descriptions: [
          item.features ? `【特征】${ item.features }` : '',
          item.clinicalSignificance ? `【临床意义】${ item.clinicalSignificance }` : '',
        ].filter(Boolean),
        tags: item.attrs ?? [],
      });
    }
    return title;
  }) ?? [];
  return values.join('<br>');
}

function fromReportPhysiqueChartData(data: Record<string, any>[]) {
  if (data?.some(item => (item.isBasicTo === 'Y' || item.isTendTo === 'Y') && item.constitutionGroupName !== '平和质')) {
    data = data.filter(item => item.constitutionGroupName !== '平和质');
  }
  return data?.map((item: Record<string, any>) => [
    item.constitutionGroupName, +item.score,
    item.constitutionGroupName === '平和质'
    ? /* 平和体质 */ 0
    : item.isBasicTo === 'Y'
      ? /* 所属体质 */ 1
      : item.isTendTo === 'Y'
        ? /* 倾向体质 */ 2
        : item.isInfer === 'Y'
          ? /* 推断体质 */ 3
          : 4,
  ]);
}
