import { toastController } from "@ionic/core";
import valueToPath from "./valueToPath";
export default async function importJobsFromData(fireenjin, results) {
  let jobColumnKeys = [
    "id",
    "title",
    "amountDeducted",
    "dateOfLoss",
    "address",
    "status",
    "type",
    "title",
    "picture",
    "phone",
    "customer",
    "email",
    "locationId",
    "payModelId",
    "isFranchise",
    "category",
    "cause",
    "propertyAge",
    "referralId",
    "matterportModelId",
    "tags",
    "isInsured",
    "claimNumber",
    "isSearchable",
  ];
  let booleanKeys = ["isInsured", "isSearchable", "isFranchise"];
  for (const row of results) {
    let jobData = {
      tags: ["csvImport"],
    };
    const columnKeys = Object.keys(row);
    for (const columnKey of columnKeys) {
      jobData = valueToPath(columnKey, booleanKeys.includes(columnKey)
        ? row[columnKey] === "TRUE"
        : row[columnKey], jobData);
    }
    const data = {};
    for (const columnKey of jobColumnKeys) {
      if (jobData === null || jobData === void 0 ? void 0 : jobData[columnKey])
        data[columnKey] = jobData[columnKey];
    }
    if (jobData === null || jobData === void 0 ? void 0 : jobData.address)
      data.address = { full: jobData.address };
    delete data.id;
    await ((jobData === null || jobData === void 0 ? void 0 : jobData.id)
      ? fireenjin.sdk.editJob({
        id: jobData === null || jobData === void 0 ? void 0 : jobData.id,
        data,
      })
      : fireenjin.sdk.addJob({
        data,
      }));
  }
  (await toastController.create({
    message: `Successfully imported ${(results === null || results === void 0 ? void 0 : results.length) || 0} records.`,
    duration: 3000,
  })).present();
}
