import { cacheFor }           from '@/request/api/index';
import type { RegisterModel } from '@/request/model';
import HTTP                   from '../alova';


export type FieldKey = 'cardno' | 'phone' | 'code' | 'name' | 'sex' | 'height' | 'weight' | 'isEasyAllergy'


export function getCaptchaMethod(mobile: string) {
  return HTTP.Get<string>(`/fdhb-tablet/sms/sendVerCode`, {
    params: { phone: mobile },
  });
}

export function registerFieldsMethod() {
  return HTTP.Post<FieldKey[], { tabletFileFields: FieldKey[] }>(`/fdhb-tablet/warrantManage/getPageSets`, {}, {
    cacheFor,
    transform(data, headers) {
      // 修正 phone,code
      const keys = data?.tabletFileFields?.join(',').replace(`phone`, 'phone,code') ?? '';
      const replace = (key: string) => key.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase());
      return keys.split(',').filter(Boolean).map(replace) as FieldKey[] ?? [];
    },
  });
}

export function registerAccountMethod(params: Partial<RegisterModel>) {
  return HTTP.Post<string, string>(`/fdhb-tablet/patientInfoManage/savePatientInfo`, params, {});
}

export function searchAccountMethod(params: Partial<RegisterModel>) {
  return HTTP.Get(`/fdhb-tablet/patientInfoManage/getPatientInfoDetail`, {
    params,
    transform(data: Record<string, any>, headers) {
      return Object.fromEntries(Object.entries(data).filter(([ item, value ]) => !!value)) as Partial<RegisterModel>;
    },
  });
}
