export function fromReportData(data: Record<string, any>) {
  const tongueException: ReportTongueException[] = [];
  const fromTongueException = fromReportTongueExceptionData.bind(null, tongueException);
  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 ],
      ],
    },
    tongueTable: {
      column: [ '舌象维度', '检测结果', '标准值' ],
      data: [
        [ '舌色', fromTongueException(data?.tongueColor), data?.tongueColor?.standardValue ],
        [ '苔色', fromTongueException(data?.tongueCoatingColor), data?.tongueCoatingColor?.standardValue ],
        [ '舌形', fromTongueException(data?.tongueShape), data?.tongueShape?.standardValue ],
        [ '苔质', fromTongueException(data?.tongueCoating), data?.tongueCoating?.standardValue ],
        [ '津液', fromTongueException(data?.bodyFluid), data?.bodyFluid?.standardValue ],
        [ '舌下', fromTongueException(data?.sublingualVein), data?.sublingualVein?.standardValue ],
      ],
    },
    tongueException,
    tongueAnalysis: {
      [ '结果' ]: data?.tongueAnalysisResult,
      [ '舌上' ]: data?.upImg,
      [ '舌下' ]: data?.downImg,
    },
    faceAnalysis: {
      [ '结果' ]: data?.faceAnalysisResult,
      [ '面象' ]: data?.faceImg,
    },

    [ '中医证素' ]: 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,
  };
}


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>[]) {
  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,
  ]);
}
