import { Get, Post } from "../../lib/request/method";
import { createHealthIndex, transformHealthIndex2Chart } from "./tools/health-index";
import dayjs from "dayjs";

export function healthPatientMethod() {
  return Post(`/patientInfoManage/getPatientInfoDetail`, {}, { transform({ data }) { return data } });
}

export function healthReportListMethod() {
  const transform = ({ data }: AnyObject) => {
    return Array.isArray(data) ? data.map(item => {
      return {
        id: item.healthAnalysisReportId,
        reportTime: item.time3,
        description: [item.willillStateName, item.willillDegreeName, item.willillSocialName, item.willillFunctionName].filter(Boolean).join('，')
      }
    }) : []
  }
  return Get(`/analysisManage/getHarsTid`, { transform })
}

export function healthReportMethod(id?: string) {
  const transform = ({ data }: AnyObject) => {
    if (Array.isArray(data?.conditProgram?.types)) {
      data.conditProgram.types = data.conditProgram.types.map((item: AnyObject) => (item.summary = item.summary?.replace(/null/g, '') || '', item))
    }
    return data;
  };

  return id
    ? Get(`/analysisManage/getHealRepDetailById`, { params: { healthAnalysisReportId: id }, transform })
    : Post(`/analysisManage/getLastHealRepDetail`, {}, { transform })
}

/**
 * 获取指标信息
 * @param id 健康分析报告
 */
export function healthIndexMethod(id?: string) {
  const transform = ({ data }: AnyObject) => Array.isArray(data) ? data.map(item => createHealthIndex(item)) : [];

  return id
    ? Get(`/analysisManage/getQuovalByHarId`, { params: { healthAnalysisReportId: id }, transform })
    : Post(`/patientQuota/getCurQuoval`, {}, { transform })
}

export function healthIndexReportMethod() {
  return Post(`/patientQuota/getQuovalRecord`, {}, {
    transform({ data }) { return transformHealthIndex2Chart(<any[]>data) }
  })
}

export function healthSchemeMethod(id: string) {
  const transform = ({ data }: AnyObject) => {
    return {
      reportTime: dayjs(data?.time).format('YYYY年MM月DD日'),
      types: Array.isArray(data?.types) ? data.types.map((item: AnyObject) => {
        return {
          type: item.type || '',
          summary: item.summary?.replace(/null/g, '') || '',
          groups: item.groups?.map((group: AnyObject) => {
            const descriptions = group.description?.split('\n').filter(Boolean) ?? [];
            const media = [] as any[];
            const items = group.items;
            if (Array.isArray(items)) {
              let medicine = [] as any[];
              const fn = () => {
                const str = medicine.map(item => [item.name, [item.doase, item.unit].filter(Boolean).join('')].filter(Boolean).join(' ')).join('; ')
                descriptions.push(`${str}`)
                medicine = [];
              }
              for (const item of items) {
                const { type, imgUrl, mediaUrl, name, description } = item;
                if (type !== 'medicine' && medicine.length) fn();
                switch (type) {
                  case 'img':
                    if (mediaUrl || imgUrl) media.push({
                      type: 'picture',
                      mediaUrl: mediaUrl ?? imgUrl,
                      imgUrl: imgUrl ?? mediaUrl,
                      name: name ?? '',
                      description: description ?? ''
                    })
                    break;
                  case 'video':
                    if (mediaUrl) media.push({
                      type: 'video',
                      mediaUrl: mediaUrl,
                      imgUrl: imgUrl,
                      name: name ?? '',
                      description: description ?? ''
                    });
                    break;
                  case 'text':
                    descriptions.push(`${name}: ${description}`)
                    break;
                  case 'medicine':
                    medicine.push(item);
                    break;
                }
              }
              if (medicine.length) fn();
            }
            return {
              name: group.name,
              descriptions, media,
            }
          })
        }
      }) : []
    }
  };
  return Get(`/analysisManage/getCondProgDetailById`, { params: { healthAnalysisReportId: id }, transform })
}