import HTTP from '@/request/alova';
import { fromAnalysisModel, type PulseAnalysisModel } from '@/request/model';
import { getNetworkWall } from '@/platform';

const getSummary = (value?: string[]) => (Array.isArray(value) && value.length ? value : void 0);

export function putPulseMethod(patientId: string, result: PulseAnalysisModel) {
  const summary =
    getSummary(result.summaryLabel?.summary) ??
    getSummary(result.summaryLabel?.left?.summary) ??
    getSummary(result.summaryLabel?.right?.summary) ??
    [];
  const data = Object.assign(result, { patientId, results: summary.join(',') });
  return HTTP.Post<PulseAnalysisModel, any>(`/fdhb-tablet/analysisManage/savePulseAnalysisReport`, data, {
    async transform() {
      const network = await getNetworkWall();
      return {
        measureId: data.measureId,
        hand: [
          data.summaryLabel?.left?.summary?.length,
          data.summaryLabel?.right?.summary?.length,
        ].filter(Boolean).length,
        summaryValue: data.summaryValue,
        summaryLabel: data.summaryLabel,
        results: data.results,
        url: network === 'interior' ? (console.info('log: 判定内网环境 舍弃脉诊报告 url'),void 0) : data.url,
      };
    },
  });
}

/**
 * 暂未实现
 * @deprecated
 */
export function getPulseMethod(id: string) {
  return HTTP.Get(`/fdhb-tablet/analysisManage/getHealRepDetailById`, {
    transform(data: Record<string, any>) {
      return {
        date: data?.pulseAnalysisReportDate,
        pulse: fromAnalysisModel('pulse', data),
      }
    }
  });
}

export function getPulseAgentMethod() {
  return HTTP.Post(
    `/fdhb-tablet/analysisManage/getPulseSupport`,
    {},
    {
      transform(data: any) {
        const url = data?.origin ?? data?.url;
        if (!url) return void 0;

        const origin = url.endsWith('/report.html') ? url : `${url}/taiyi/report.html`;
        return {
          appId: data.appId ?? 'hJn5D3rr',
          token: data.token,
          url: `${origin}#/`,
        };
      },
    }
  );
}