import { toastController } from "@ionic/core";
import valueToPath from "./valueToPath";
export default async function importServicesFromData(fireenjin, results) {
  var _a;
  console.log(results);
  for (const row of results) {
    let serviceData = {
      tags: ["csvImport"],
    };
    const columnKeys = Object.keys(row);
    for (const columnKey of columnKeys) {
      serviceData = valueToPath(columnKey, row[columnKey], serviceData);
    }
    let prices = Object.entries((serviceData === null || serviceData === void 0 ? void 0 : serviceData.prices) || {}).map(([name, price]) => ({
      name,
      amount: price ? parseFloat(price.replace(/[^\d.-]/g, "")) : 0,
    }));
    const data = {
      name: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.name) || null,
      category: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.category) || null,
      code: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.code) || null,
      unit: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.unit) || null,
      description: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.description) || null,
      group: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.group) || null,
      tab: (serviceData === null || serviceData === void 0 ? void 0 : serviceData.tab) || null,
      prices,
    };
    await (((_a = serviceData === null || serviceData === void 0 ? void 0 : serviceData.id) === null || _a === void 0 ? void 0 : _a.length) > 0
      ? fireenjin.sdk.editService({
        id: serviceData === null || serviceData === void 0 ? void 0 : serviceData.id,
        data,
      })
      : await fireenjin.sdk.addService({
        data,
      }));
  }
  (await toastController.create({
    message: `Successfully imported ${(results === null || results === void 0 ? void 0 : results.length) || 0} records.`,
    duration: 3000,
  })).present();
}
