'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-fd2bbf0b.js');
const hardwareBackButton = require('./overlays-de05dc6a.js');
const store = require('./store-f2110833.js');
require('./ionic-global-1340f737.js');
require('./helpers-c2496722.js');
require('./index-81aa1bcb.js');
require('./getCache-c039a6ae.js');

const modalTakeReadingsCss = "";

let ModalTakeReadings = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.floodteamCloseModal = index.createEvent(this, "floodteamCloseModal", 7);
    this.steps = [
      {
        tab: "weather",
        name: "Weather",
        label: "Get Weather",
        icon: "rainy",
      },
      {
        tab: "dots",
        name: "MC Dots",
        label: "Take Moisture Content Readings",
        icon: "ellipse",
      },
      {
        tab: "equipment",
        name: "Equipment",
        label: "Take Equipment Readings",
        icon: "construct",
      },
      {
        tab: "atmospheric",
        name: "Atmospheric",
        label: "Take Atmospheric Readings",
        icon: "cloud-circle-outline",
      },
    ];
    this.dotForm = false;
    this.closeOnFinish = false;
    this.started = false;
    this.completed = false;
    this.actionQueue = [];
    this.dotSiteMap = {};
    this.equipmentSiteMap = {};
  }
  onSubmit(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "weather") {
      this.tabsEl.select("dots");
    }
    else if (((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.name) === "atmospheric") {
      if (!((_d = (_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.chamber)) {
        alert("Chamber readings required!");
        return;
      }
      for (const [chamber, { temperature, humidity }] of Object.entries(event.detail.data.chamber)) {
        const action = {
          endpoint: "addAtmospheric",
          params: {
            data: {
              jobId: this.jobId,
              chamber: chamber ? Number(chamber) : 0,
              temperature: temperature ? Number(temperature) : 0,
              humidity: humidity ? Number(humidity) : 0,
            },
          },
        };
        this.fireenjinFetch.emit(action);
        this.actionQueue.push(action);
      }
    }
    else if (((_e = event === null || event === void 0 ? void 0 : event.target) === null || _e === void 0 ? void 0 : _e.name) === "moisture") {
      if (!((_g = (_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.dot)) {
        alert("Moisture Content readings required!");
        return;
      }
      for (const [dotId, { temperature, readings }] of Object.entries(event.detail.data.dot)) {
        const action = {
          endpoint: "addMoistureContent",
          params: {
            data: {
              dotId,
              jobId: this.jobId,
              siteId: this.dotSiteMap[dotId],
              temperature: temperature ? Number(temperature) : 0,
              readings: readings
                ? readings.split(",").map((reading) => parseFloat(reading))
                : [],
            },
          },
        };
        this.fireenjinFetch.emit(action);
        this.actionQueue.push(action);
      }
    }
    else if (((_h = event === null || event === void 0 ? void 0 : event.target) === null || _h === void 0 ? void 0 : _h.name) === "equipment") {
      if (!((_k = (_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.data) === null || _k === void 0 ? void 0 : _k.equipment)) {
        alert("Exhaust readings required!");
        return;
      }
      for (const [equipmentId, { inletTemperature, inletHumidity, inletGrainsPerPound, outletTemperature, outletHumidity, outletGrainsPerPound, },] of Object.entries(event.detail.data.equipment)) {
        const action = {
          endpoint: "addExhaust",
          params: {
            data: {
              equipmentId,
              jobId: this.jobId,
              siteId: this.equipmentSiteMap[equipmentId],
              inletTemperature: inletTemperature ? Number(inletTemperature) : 0,
              inletHumidity: inletHumidity ? Number(inletHumidity) : 0,
              inletGrainsPerPound: inletGrainsPerPound
                ? Number(inletGrainsPerPound)
                : 0,
              outletTemperature: outletTemperature
                ? Number(outletTemperature)
                : 0,
              outletHumidity: outletHumidity ? Number(outletHumidity) : 0,
              outletGrainsPerPound: outletGrainsPerPound
                ? Number(outletGrainsPerPound)
                : 0,
            },
          },
        };
        this.fireenjinFetch.emit(action);
        this.actionQueue.push(action);
      }
    }
  }
  async onSuccess(event) {
    if (event.detail.endpoint === "findJob") {
      this.setReadingData();
    }
    else if (event.detail.endpoint === "addWeather") {
      this.fetchJob();
    }
    else if (event.detail.endpoint === "addAtmospheric") {
      const actionQueue = this.actionQueue;
      actionQueue.pop();
      this.actionQueue = actionQueue;
      this.tabsEl.select("dots");
      this.fetchJob();
    }
    else if (event.detail.endpoint === "addMoistureContent") {
      const actionQueue = this.actionQueue;
      actionQueue.pop();
      this.actionQueue = actionQueue;
      this.tabsEl.select("equipment");
      this.fetchJob();
    }
    else if (event.detail.endpoint === "addExhaust") {
      const actionQueue = this.actionQueue;
      actionQueue.pop();
      this.actionQueue = actionQueue;
      this.fetchJob();
      this.tabsEl.select("atmospheric");
    }
    else if (event.detail.endpoint === "addDot") {
      this.fetchJob();
      this.dotForm = false;
      if (this.closeOnFinish)
        this.floodteamCloseModal.emit();
    }
  }
  async onReset(event) {
    var _a, _b, _c, _d, _e;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "weather") {
      event.preventDefault();
      this.started = false;
    }
    else if (((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.name) === "atmospheric") {
      this.tabsEl.select("equipment");
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.name) === "moisture") {
      this.tabsEl.select("weather");
    }
    else if (((_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.name) === "equipment") {
      this.tabsEl.select("dots");
    }
    else if (((_e = event === null || event === void 0 ? void 0 : event.target) === null || _e === void 0 ? void 0 : _e.name) === "dot") {
      if (this.closeOnFinish)
        this.floodteamCloseModal.emit();
      this.dotForm = false;
    }
  }
  formatDate(date) {
    const d = typeof date === "string" ? new Date(date) : date;
    let month = "" + (d.getMonth() + 1);
    let day = "" + d.getDate();
    const year = d.getFullYear();
    if (month.length < 2)
      month = "0" + month;
    if (day.length < 2)
      day = "0" + day;
    return [year, month, day].join("-");
  }
  setReadingData() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    const readings = {};
    for (const reading of ((_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.readings) ? store.state === null || store.state === void 0 ? void 0 : store.state.job.readings : []) {
      if (!(reading === null || reading === void 0 ? void 0 : reading.name))
        continue;
      readings[reading.name] = reading;
    }
    this.readings = readings;
    this.temperature =
      ((_d = (_c = (_b = this.readings) === null || _b === void 0 ? void 0 : _b[this.selectedDay]) === null || _c === void 0 ? void 0 : _c.weather) === null || _d === void 0 ? void 0 : _d.temperature) || null;
    this.humidity =
      ((_g = (_f = (_e = this.readings) === null || _e === void 0 ? void 0 : _e[this.selectedDay]) === null || _f === void 0 ? void 0 : _f.weather) === null || _g === void 0 ? void 0 : _g.humidity) || null;
    this.zip = ((_j = (_h = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _h === void 0 ? void 0 : _h.address) === null || _j === void 0 ? void 0 : _j.zip) || this.zip || null;
    for (const site of ((_k = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _k === void 0 ? void 0 : _k.sites) || []) {
      for (const dot of ((_l = site === null || site === void 0 ? void 0 : site.dots) === null || _l === void 0 ? void 0 : _l.length) ? site.dots : []) {
        this.dotSiteMap[dot.id] = site.id;
      }
      for (const equipment of ((_m = site === null || site === void 0 ? void 0 : site.equipment) === null || _m === void 0 ? void 0 : _m.length) ? site.equipment : []) {
        this.equipmentSiteMap[equipment.id] = equipment.id;
      }
    }
  }
  onTabClick(tabName) {
    let stopUnderline = false;
    this.tabBarEl.querySelectorAll("ion-tab-button").forEach((tabButtonEl) => {
      tabButtonEl.style.borderBottom = "none";
      if (tabName === tabButtonEl.tab)
        stopUnderline = true;
      if (!stopUnderline)
        tabButtonEl.style.borderBottom = "2px solid var(--ion-color-success)";
    });
  }
  getChambers() {
    var _a, _b;
    const chambers = [];
    if ((_b = (_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.sites) === null || _b === void 0 ? void 0 : _b.length)
      store.state.job.sites.map((site) => !isNaN(site.chamber) &&
        !chambers.includes(site.chamber) &&
        chambers.push(site.chamber));
    return chambers;
  }
  async fetchJob() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      params: {
        id: this.jobId,
        withPayments: false,
        withUsers: true,
        withSites: true,
        withPayModel: true,
        withPhotos: true,
        withEfiles: true,
        withPayables: false,
        withReadings: true,
      },
    });
  }
  async fetchWeather() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "addWeather",
      params: {
        data: {
          jobId: this.jobId,
          zip: this.zip,
        },
      },
    });
  }
  async openReadingSelect(event) {
    this.readingSelectPopoverEl = await hardwareBackButton.popoverController.create({
      component: "popover-reading-select",
      componentProps: {
        readings: this.readings ? Object.values(this.readings) : [],
      },
      event,
    });
    return this.readingSelectPopoverEl.present();
  }
  componentDidLoad() {
    var _a;
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.selectedDay = this.formatDate(new Date());
    if (!((_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.id)) {
      this.fetchJob();
    }
    else {
      this.setReadingData();
    }
    this.fetchWeather();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
    return [
      index.h("ion-header", null, index.h("ion-toolbar", null, index.h("ion-buttons", { slot: "start", onClick: (event) => this.openReadingSelect(event) }, index.h("ion-button", null, index.h("ion-icon", { slot: "icon-only", name: "caret-down-circle-outline" }))), index.h("ion-title", null, this.dotForm ? "Add Dot" : "Take Readings"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { onClick: () => this.floodteamCloseModal.emit() }, index.h("ion-icon", { slot: "icon-only", name: "close-circle-outline" }))))),
      index.h("ion-content", null, index.h("ion-card", { style: {
          opacity: this.dotForm ? "1" : "0",
          height: this.dotForm ? "auto" : "0",
        } }, index.h("fireenjin-form", { endpoint: "addDot", name: "dot", resetButton: "Cancel", submitButton: "Save", class: "ion-padding", beforeSubmit: (data) => ({
          ...data,
          dryStandard: data.dryStandard ? parseFloat(data.dryStandard) : 0,
          evaporationPotentialGoal: data.evaporationPotentialGoal
            ? parseFloat(data.evaporationPotentialGoal)
            : 0,
          jobId: this.jobId,
        }) }, index.h("ion-list", null, index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-select", { name: "siteId", options: (((_b = (_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.sites) === null || _b === void 0 ? void 0 : _b.length)
          ? store.state === null || store.state === void 0 ? void 0 : store.state.job.sites
          : []).map((site) => ({
          label: (site === null || site === void 0 ? void 0 : site.name) || (site === null || site === void 0 ? void 0 : site.id),
          value: site.id,
        })), value: this.siteId, label: "Site" }), index.h("fireenjin-input", { name: "name", labelPosition: "stacked", label: "Name", required: true }), index.h("fireenjin-input", { labelPosition: "stacked", name: "meter", label: "Meter Used" }), index.h("fireenjin-input", { labelPosition: "stacked", name: "placement", label: "Placement" }), index.h("fireenjin-input", { labelPosition: "stacked", name: "material", label: "Material" }))), index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-input", { labelPosition: "stacked", name: "dryStandard", label: "Dry Standard", type: "number", step: "0.01" })), index.h("ion-col", null, index.h("fireenjin-input", { labelPosition: "stacked", name: "evaporationPotentialGoal", label: "EP Goal", type: "number", step: "0.01" }))))))), index.h("fireenjin-form", { style: {
          opacity: this.started || this.dotForm ? "0" : "1",
          height: this.started || this.dotForm ? "0" : "auto",
        }, class: "ion-padding", name: "overview", submitButton: "Start", resetButton: "Cancel", disableLoader: true, beforeSubmit: (data) => {
          this.started = true;
          return data;
        } }, index.h("ion-card", { class: "ion-padding" }, index.h("ion-list", null, this.steps.map((step, index$1) => (index.h("ion-item", null, index.h("ion-icon", { name: step.icon, slot: "start", color: "primary" }), index.h("ion-label", null, index$1 + 1, ". ", step.label))))))), index.h("ion-tabs", { style: {
          opacity: this.started && !this.dotForm ? "1" : "0",
          height: this.started && !this.dotForm ? "auto" : "0",
        }, ref: (el) => (this.tabsEl = el) }, index.h("ion-tab-bar", { ref: (el) => (this.tabBarEl = el), slot: "top" }, this.steps.map((step) => (index.h("ion-tab-button", { tab: step.tab, onClick: () => this.onTabClick(step.tab) }, index.h("ion-icon", { name: step.icon }), index.h("ion-label", null, step.name))))), index.h("ion-tab", { tab: "weather" }, index.h("ion-content", null, index.h("ion-card", null, index.h("fireenjin-form", { class: "ion-padding", style: {
          paddingTop: "0",
        }, name: "weather", endpoint: "addWeather", formData: {
          jobId: this.jobId,
          zip: this.zip,
        }, disableReset: true, disableLoader: true, submitButton: "Next", resetButtonColor: "medium", resetButton: "Back" }, index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", { size: "12", sizeMd: "7" }, index.h("img", { alt: "Weather Icon", style: {
          height: "150px",
          width: "150px",
          display: "block",
          margin: "0 auto",
        }, src: `./assets/images/${((_g = (_f = (_e = (_d = (_c = this.readings) === null || _c === void 0 ? void 0 : _c[this.selectedDay]) === null || _d === void 0 ? void 0 : _d.weather) === null || _e === void 0 ? void 0 : _e.conditions) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.icon)
          ? "weather/" +
            this.readings[this.selectedDay].weather
              .conditions[0].icon
          : "icon"}.png` }), index.h("h1", { style: {
          display: "block",
          textAlign: "center",
          margin: "0",
        } }, ((_l = (_k = (_j = (_h = this.readings) === null || _h === void 0 ? void 0 : _h[this.selectedDay]) === null || _j === void 0 ? void 0 : _j.weather.conditions) === null || _k === void 0 ? void 0 : _k[0]) === null || _l === void 0 ? void 0 : _l.main) || "No Weather Data..."), index.h("h3", { style: {
          display: "block",
          textAlign: "center",
          fontSize: "14px",
          color: "var(--ion-text-color)",
        } }, ((_o = (_m = this.readings) === null || _m === void 0 ? void 0 : _m[this.selectedDay]) === null || _o === void 0 ? void 0 : _o.name) ||
        "Enter Zip Code to Search")), index.h("ion-col", { size: "12", sizeMd: "5", class: "ion-padding" }, index.h("fireenjin-input", { label: "Zip Code", labelPosition: "stacked", placeholder: "Enter a Zip Code", name: "zip", value: this.zip, required: true }), index.h("fireenjin-input", { label: "Temperature", labelPosition: "stacked", placeholder: "Outside Temp", name: "temperature", value: this.temperature }), index.h("fireenjin-input", { label: "Humidity", labelPosition: "stacked", placeholder: "Outside Humidity", name: "humidity", value: this.humidity })))))))), index.h("ion-tab", { tab: "atmospheric" }, index.h("ion-content", null, index.h("ion-card", null, index.h("fireenjin-form", { disableLoader: true, disableReset: true, name: "atmospheric", submitButton: "Next", resetButtonColor: "medium", resetButton: "Back" }, index.h("ion-list", { class: "ion-no-padding" }, this.getChambers().map((chamber) => [
        index.h("ion-item-divider", null, "Chamber ", chamber),
        index.h("ion-item", null, index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-input", { label: "Temperature", labelPosition: "stacked", placeholder: "Inside Temp", name: `chamber.${chamber}.temperature` })), index.h("ion-col", null, index.h("fireenjin-input", { label: "Humidity", labelPosition: "stacked", placeholder: "Inside Humidity", name: `chamber.${chamber}.humidity` }))))),
      ])))))), index.h("ion-tab", { tab: "dots" }, index.h("ion-content", null, index.h("ion-card", null, index.h("fireenjin-form", { disableLoader: true, disableReset: true, name: "moisture", submitButton: "Next", resetButtonColor: "medium", resetButton: "Back" }, index.h("ion-list", { class: "ion-no-padding" }, (((_p = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _p === void 0 ? void 0 : _p.sites) ? store.state === null || store.state === void 0 ? void 0 : store.state.job.sites : []).map((site) => [
        index.h("ion-item-divider", null, site === null || site === void 0 ? void 0 :
          site.name, index.h("ion-button", { slot: "end", fill: "clear", onClick: () => {
            this.siteId = site.id;
            this.dotForm = true;
          } }, "Add Dot", index.h("ion-icon", { name: "add-circle", slot: "end" }))),
        ((site === null || site === void 0 ? void 0 : site.dots) ? site.dots : []).map((dot, index$1) => (index.h("ion-item", null, index.h("div", { class: "floodteam-dot", slot: "start" }, (dot === null || dot === void 0 ? void 0 : dot.name) || index$1 + 1), index.h("ion-grid", { style: { paddingTop: "0" } }, index.h("ion-row", null, index.h("ion-col", { style: { paddingTop: "0" } }, index.h("fireenjin-input", { label: "Surface Temperature", labelPosition: "stacked", placeholder: "Surface Temp", name: `dot.${dot === null || dot === void 0 ? void 0 : dot.id}.temperature` })), index.h("ion-col", { style: { paddingTop: "0" } }, index.h("fireenjin-input", { label: "Moisture Content", labelPosition: "stacked", placeholder: "Insert Reading(s)", name: `dot.${dot === null || dot === void 0 ? void 0 : dot.id}.readings` }))))))),
      ])))))), index.h("ion-tab", { tab: "equipment" }, index.h("ion-content", null, index.h("ion-card", null, index.h("fireenjin-form", { disableLoader: true, disableReset: true, name: "equipment", submitButton: "Next", resetButtonColor: "medium", resetButton: "Back" }, index.h("ion-list", { class: "ion-no-padding" }, (((_q = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _q === void 0 ? void 0 : _q.sites) || []).map((site) => [
        index.h("ion-item-divider", null, site === null || site === void 0 ? void 0 : site.name),
        ((site === null || site === void 0 ? void 0 : site.equipment) ? site.equipment : []).map((equipment) => {
          var _a, _b;
          return (index.h("ion-item", null, index.h("div", null, index.h("ion-label", { class: "ion-padding-top" }, index.h("h2", null, ((_a = equipment === null || equipment === void 0 ? void 0 : equipment.inventory) === null || _a === void 0 ? void 0 : _a.name)
            ? equipment.inventory.name
            : (_b = equipment === null || equipment === void 0 ? void 0 : equipment.service) === null || _b === void 0 ? void 0 : _b.name)), index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-input", { label: "Temperature", labelPosition: "stacked", placeholder: "Inlet Temp", name: `equipment.${equipment.id}.inletTemperature` }), index.h("fireenjin-input", { label: "Humidity", labelPosition: "stacked", placeholder: "Inlet Humidity", name: `equipment.${equipment.id}.inletHumidity` }), index.h("fireenjin-input", { label: "GPP", labelPosition: "stacked", placeholder: "Inlet Grains Per Pound", name: `equipment.${equipment.id}.inletGrainsPerPound` })), index.h("ion-col", null, index.h("fireenjin-input", { label: "Temperature", placeholder: "Outlet Temp", name: `equipment.${equipment.id}.outletTemperature` }), index.h("fireenjin-input", { label: "Humidity", placeholder: "Outlet Humidity", name: `equipment.${equipment.id}.outletHumidity` }), index.h("fireenjin-input", { label: "GPP", placeholder: "Outlet Grains Per Pound", name: `equipment.${equipment.id}.outletGrainsPerPound` })))))));
        }),
      ])))))))),
    ];
  }
};
ModalTakeReadings.style = modalTakeReadingsCss;

exports.modal_take_readings = ModalTakeReadings;

//# sourceMappingURL=modal-take-readings.cjs.entry.js.map