'use strict';

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

const index = require('./index-ee190ee0.js');
const getHandlebarsHelpers = require('./getHandlebarsHelpers-8ef28401.js');
const store = require('./store-27073b3b.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./index-4901314f.js');
require('./localforage-af1b8c0e.js');

const popoverSiteServiceCss = ".popover-site-service{--min-width:300px;--offset-y:-50px}popover-site-service ion-accordion-group{background:var(--background-color) !important;--ion-item-background:var(--background-color);--background:var(--background-color);background-color:var(--background-color) !important}popover-site-service ion-list ion-item{--ion-item-background:transparent}popover-site-service fireenjin-input ion-label{font-size:12px !important}";

const PopoverSiteService = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.service = {};
    this.scopeToServiceMap = {};
    this.jobId = undefined;
    this.job = undefined;
    this.currentExpanded = undefined;
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findJob" &&
      this.jobId === ((_d = (_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.job) === null || _d === void 0 ? void 0 : _d.id)) {
      for (const jobService of ((_e = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _e === void 0 ? void 0 : _e.services) || []) {
        this.scopeToServiceMap[`${this.jobId}-${(_f = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _f === void 0 ? void 0 : _f.id}`] =
          jobService;
      }
      for (const site of ((_g = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _g === void 0 ? void 0 : _g.sites) || []) {
        for (const siteService of (site === null || site === void 0 ? void 0 : site.services) || []) {
          this.scopeToServiceMap[`${site === null || site === void 0 ? void 0 : site.id}-${(_h = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _h === void 0 ? void 0 : _h.id}`] =
            siteService;
        }
      }
      this.scopeToServiceMap = { ...this.scopeToServiceMap };
    }
    else if (["editScopeService", "addScopeService", "deleteScopeService"].includes((_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.endpoint)) {
      this.accordionEl.requestAccordionToggle(null, false);
    }
  }
  getAutoFillValue(parentIdServiceId, autofillKey, jsonLogicData = {}, defaultValue) {
    var _a, _b, _c, _d, _e, _f, _g;
    let value = defaultValue;
    try {
      value =
        ((_b = (_a = this.scopeToServiceMap) === null || _a === void 0 ? void 0 : _a[parentIdServiceId]) === null || _b === void 0 ? void 0 : _b[autofillKey]) ||
          (((_d = (_c = this.service) === null || _c === void 0 ? void 0 : _c.autofill) === null || _d === void 0 ? void 0 : _d[autofillKey]) &&
            ((_e = getHandlebarsHelpers.jsonLogic === null || getHandlebarsHelpers.jsonLogic === void 0 ? void 0 : getHandlebarsHelpers.logic.apply) === null || _e === void 0 ? void 0 : _e.call(getHandlebarsHelpers.jsonLogic, (_g = (_f = this.service) === null || _f === void 0 ? void 0 : _f.autofill) === null || _g === void 0 ? void 0 : _g[autofillKey], jsonLogicData || {}))) ||
          defaultValue;
    }
    catch (error) {
      console.log(`Error applying json logic to ${autofillKey}: `, this.service.id, error);
    }
    return value;
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
    const isGeneral = ((_c = (_b = (_a = this.service) === null || _a === void 0 ? void 0 : _a.tab) === null || _b === void 0 ? void 0 : _b.toLowerCase) === null || _c === void 0 ? void 0 : _c.call(_b)) === "general";
    const defaultJsonLogicData = {
      service: this.service,
      job: this.job,
    };
    return [
      index.h("ion-content", null, index.h("ion-item-divider", null, ((_d = this.service) === null || _d === void 0 ? void 0 : _d.name) || "No Name Given"), index.h("ion-accordion-group", { ref: (el) => (this.accordionEl = el), value: this.currentExpanded }, isGeneral ? (index.h("floodteam-form-site-service", { isGeneral: true, serviceId: (_e = this.service) === null || _e === void 0 ? void 0 : _e.id, jobId: this.jobId, notes: ((_h = (_f = this.scopeToServiceMap) === null || _f === void 0 ? void 0 : _f[`${this.jobId}-${(_g = this.service) === null || _g === void 0 ? void 0 : _g.id}`]) === null || _h === void 0 ? void 0 : _h.notes) || null, siteServiceId: ((_l = (_j = this.scopeToServiceMap) === null || _j === void 0 ? void 0 : _j[`${this.jobId}-${(_k = this.service) === null || _k === void 0 ? void 0 : _k.id}`]) === null || _l === void 0 ? void 0 : _l.id) || null, amount: this.getAutoFillValue(`${this.jobId}-${(_m = this.service) === null || _m === void 0 ? void 0 : _m.id}`, "amount", defaultJsonLogicData, 0), price: this.getAutoFillValue(`${this.jobId}-${(_o = this.service) === null || _o === void 0 ? void 0 : _o.id}`, "price", defaultJsonLogicData, (_q = (_p = this.service) === null || _p === void 0 ? void 0 : _p.prices) === null || _q === void 0 ? void 0 : _q.default), quantity: this.getAutoFillValue(`${this.jobId}-${(_r = this.service) === null || _r === void 0 ? void 0 : _r.id}`, "quantity", defaultJsonLogicData, 1), unit: (_s = this.service) === null || _s === void 0 ? void 0 : _s.unit })) : ((((_t = store.state.job) === null || _t === void 0 ? void 0 : _t.sites) || []).map((site) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
        return (index.h("floodteam-form-site-service", { siteId: site === null || site === void 0 ? void 0 : site.id, jobId: this.jobId, serviceId: (_a = this.service) === null || _a === void 0 ? void 0 : _a.id, site: site, notes: ((_d = (_b = this.scopeToServiceMap) === null || _b === void 0 ? void 0 : _b[`${site === null || site === void 0 ? void 0 : site.id}-${(_c = this.service) === null || _c === void 0 ? void 0 : _c.id}`]) === null || _d === void 0 ? void 0 : _d.notes) || null, siteServiceId: ((_g = (_e = this.scopeToServiceMap) === null || _e === void 0 ? void 0 : _e[`${site === null || site === void 0 ? void 0 : site.id}-${(_f = this.service) === null || _f === void 0 ? void 0 : _f.id}`]) === null || _g === void 0 ? void 0 : _g.id) || null, amount: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_h = this.service) === null || _h === void 0 ? void 0 : _h.id}`, "amount", {
            ...defaultJsonLogicData,
            site,
          }, 0), price: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_j = this.service) === null || _j === void 0 ? void 0 : _j.id}`, "price", {
            ...defaultJsonLogicData,
            site,
          }, (_l = (_k = this.service) === null || _k === void 0 ? void 0 : _k.prices) === null || _l === void 0 ? void 0 : _l.default), quantity: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_m = this.service) === null || _m === void 0 ? void 0 : _m.id}`, "quantity", {
            ...defaultJsonLogicData,
            site,
          }, 1), name: site === null || site === void 0 ? void 0 : site.name, unit: (_o = this.service) === null || _o === void 0 ? void 0 : _o.unit }));
      })))),
    ];
  }
};
PopoverSiteService.style = popoverSiteServiceCss;

exports.popover_site_service = PopoverSiteService;
