import { Get, Post } from "../../lib/request/method";

export function getPatients(id?: string) {
  id ??= wx.getStorageSync('patientId')
  const transform = ({ data }: AnyObject) => {
    const patient = void 0
      ?? data.find((item: AnyObject) => item.patientId == id)
      ?? data.find((item: AnyObject) => item.isDefault?.toUpperCase() === 'Y')
      ?? data[0];

    return { patient, patients: data }
  }
  return Post('/mobileAccountManage/getPatsByAid', {}, { transform })
}

export async function getPatientDescription(patient: { sex: '0' | '1'; patientId: string; }) {
  if (patient.sex === '0') return '';
  try {
    const transform = ({ data }: any) => data;
    const dict = await Get('/dict/getDicts', { transform });
    const options = dict.find((item: any) => item.dictType === 'women_special_period').items;
    const { womenSpecialPeriod } = await Post('/patientInfoManage/getPatientInfoDetail', {}, { transform });
    if (womenSpecialPeriod) return womenSpecialPeriod.split(',').map((k: string) => {
      const [key, value] = k.split(':')
      const label = options.find((option: any) => option.dictValue === key).dictLabel;
      return value ?? label !== '无' ? label : ''
    }).filter(Boolean).join(',')
  } catch (error) {
    return ''
  }

}

export function healthReportMethod() {
  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 Post(`/analysisManage/getLastHealRepDetail`, {}, { transform })
}

export function healthIndexMethod() {
  const transform = ({ data }: AnyObject) => {
    return Array.isArray(data) ? data.map(item => ({ ...item, ...item.patientQuotaRecordDTOS?.slice(-1)[0] })) : [];
  };
  return Post(`/patientQuota/getCurQuoval`, {}, { transform })
}

export function getSolarTerms() {
  return Get(`/solarTerm/getCurSolarTerm`, {
    transform({ data }) { return { description: data }; }
  });
}

export function getShortScienceList(page = 1, size = 3) {
  return Post(`/psarticle/pagePsarticle?pageNum=${page}&pageSize=${size}`, void 0, {
    transform({ data }: AnyObject) {
      return {
        total: data.total, page: data?.current ?? page, size: data?.size ?? size,
        data: data.data.map((item: AnyObject) => (item.id = item.popularScienceArticleId, item))
      };
    }
  })
}