import { defineStore } from 'pinia';
import type { PulseAnalysisModel, RegisterModel } from '@/request/model';

type Patient = { [K in keyof RegisterModel]?: RegisterModel[K] | string };

export const useVisitor = defineStore('visitor', () => {
  const patientId = ref<string>();
  const resultId = ref<string>();
  const reportId = ref<string>();
  const pulseReport = shallowRef<PulseAnalysisModel>();

  const patient = shallowRef<Patient>();

  const $reset = () => {
    patientId.value = '';
    resultId.value = '';
    reportId.value = '';
    pulseReport.value = void 0;
    patient.value = void 0;
  };

  function updatePulseReport(report: PulseAnalysisModel, patient?: string) {
    if (patientId.value?.toString() !== patient || !pulseReport.value?.results) {
      if (patient) patientId.value = patient;
      pulseReport.value = report;
    }
  }

  function updatePatient(data: Patient, id?: string) {
    if (!id || id === patientId.value) patient.value = data;
    else patient.value = void 0;
  }

  return { patientId, resultId, reportId, pulseReport, $reset, updatePulseReport, patient, updatePatient };
});
