import { getJsDateFromExcel } from "excel-date-to-js";
import { writeFileSync } from "fs";
import { join } from "path";
import excelData from "./excelToJson";

const recordsToJson = () => {
  let pacientiNr = 0;
  let klinikaNr = 0;
  let diagnozaNr = 0;
  let shtrimiNr = 0;
  let trajtimiNr = 0;
  let errorNr = 0;
  let bariNr = 0;
  let records: any[] = [];

  const data = excelData();
  const klinika_emer = 'file.split("_")[0];'
  klinikaNr++;
  const klinika: any = {
    nr: klinikaNr,
    emer: klinika_emer,
    shtrimi: [],
  };
  data.every((record: any) => {
    const emer = record.EMER;
    const mbiemer = record.MBIEMER;
    const mosha = record.MOSHA;
    let gjinia = record.GJINIA;
    let datelindja = record.DATELINDJA_KORIGJ;
    let data_shtrimit = record.DT_SHTRIMI_KORIGJ;
    let diagnozaRaw = record.DIAGNOZA;
    let kodi_dg_icd_9Raw = record["Kodi DG  ICD-9"];
    let dite_qendrimiRaw = record["DITE QENDRIMI NE SPITAL"];
    let trajtimiRaw =
      record["TRAJTIMI SPITALOR Urgjence"] ||
      record["TRAJTIMI SPITALOR Pavion"];
    if (emer.trim() !== "" && mbiemer.trim() !== "") {
      pacientiNr++;
      try {
        datelindja = getJsDateFromExcel(datelindja);
        data_shtrimit = getJsDateFromExcel(data_shtrimit);
      } catch (error) {
        errorNr++;
        // console.log(errorNr, pacientiNr, emer, mbiemer);
        // return false;
      }
      let trajtimi = {};
      if (trajtimiRaw) {
        trajtimiRaw = trajtimiRaw.trim();
        let barnatArr = trajtimiRaw.split("\n");
        barnatArr = barnatArr.map((bar: any) => {
          bar = bar.trim();
          let obj: any = {};
          let bariRaw = bar.split(",");
          if (
            bariRaw[0] === "" ||
            bariRaw[0] === "??" ||
            bariRaw[0] === "???" ||
            bariRaw[0] === "????" ||
            bariRaw[0] === "---" ||
            bariRaw[0] === "----" ||
            bariRaw[0] === "-----"
          ) {
            return;
          } else {
            bariNr++;
            obj.bari = bariRaw[0];
            obj.sasia = null;
            obj.nr = bariNr;
            console.log(obj.bari);
            return obj;
          }
        });
        trajtimiNr++;
        trajtimi = {
          nr: trajtimiNr,
          trajtimi: barnatArr,
        };
      }
      let diagnoza = {};
      if (diagnozaRaw.trim()) {
        diagnozaNr++;
        diagnoza = {
          nr: diagnozaNr,
          emer: diagnozaRaw.trim(),
          kodi_dg_icd_9: kodi_dg_icd_9Raw.trim(),
        };
      }

      const pacienti = {
        nr: pacientiNr,
        emer: emer.trim(),
        mbiemer: mbiemer.trim(),
        mosha: mosha,
        datelindja,
        gjinia: gjinia,
      };
      shtrimiNr++;
      const shtrimi = {
        nr: shtrimiNr,
        data_shtrimit,
        dite_qendrimi: parseInt(dite_qendrimiRaw),
        pacienti,
        diagnoza: diagnoza,
        trajtimi,
      };
      klinika.shtrimi.push(shtrimi);
    }
    return true;
  });
  records.push(klinika);
  console.log(`Mbaroi ${klinika_emer}`);
  const jsonFile = join(__dirname, "..", "..", "JSON", "records.json");
  writeFileSync(jsonFile, JSON.stringify(records));
};

export { recordsToJson, excelData };
