import { r as registerInstance, d as createEvent, h } from './index-33e43734.js';
import { j as jsonLogic, l as logic } from './logic-88140d62.js';
import { s as state } from './store-be7d1de5.js';
import './_commonjsHelpers-4ed75ef8.js';
import './index-91eb4513.js';
import './getHandlebarsHelpers-91de4454.js';
import './index-d4e79b88.js';
import './localforage-9daa31d6.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}";

let PopoverSiteService = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.service = {};
    this.scopeToServiceMap = {};
  }
  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 = state === null || state === void 0 ? void 0 : 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 = state === null || state === void 0 ? void 0 : 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;
        }
      }
    }
    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 = jsonLogic === null || jsonLogic === void 0 ? void 0 : logic.apply) === null || _e === void 0 ? void 0 : _e.call(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;
    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 [
      h("ion-content", null, h("ion-item-divider", null, ((_d = this.service) === null || _d === void 0 ? void 0 : _d.name) || "No Name Given"), h("ion-accordion-group", { ref: (el) => (this.accordionEl = el), value: this.currentExpanded }, isGeneral ? (h("floodteam-form-site-service", { isGeneral: true, serviceId: (_e = this.service) === null || _e === void 0 ? void 0 : _e.id, jobId: this.jobId, siteServiceId: ((_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.id) || null, amount: this.getAutoFillValue(`${this.jobId}-${(_j = this.service) === null || _j === void 0 ? void 0 : _j.id}`, "amount", defaultJsonLogicData, 0), price: this.getAutoFillValue(`${this.jobId}-${(_k = this.service) === null || _k === void 0 ? void 0 : _k.id}`, "price", defaultJsonLogicData, (_m = (_l = this.service) === null || _l === void 0 ? void 0 : _l.prices) === null || _m === void 0 ? void 0 : _m.default), quantity: this.getAutoFillValue(`${this.jobId}-${(_o = this.service) === null || _o === void 0 ? void 0 : _o.id}`, "quantity", defaultJsonLogicData, 1), unit: (_p = this.service) === null || _p === void 0 ? void 0 : _p.unit })) : ((((_q = state.job) === null || _q === void 0 ? void 0 : _q.sites) || []).map((site) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        return (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, siteServiceId: ((_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.id) || null, amount: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_e = this.service) === null || _e === void 0 ? void 0 : _e.id}`, "amount", {
            ...defaultJsonLogicData,
            site,
          }, 0), price: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_f = this.service) === null || _f === void 0 ? void 0 : _f.id}`, "price", {
            ...defaultJsonLogicData,
            site,
          }, (_h = (_g = this.service) === null || _g === void 0 ? void 0 : _g.prices) === null || _h === void 0 ? void 0 : _h.default), quantity: this.getAutoFillValue(`${site === null || site === void 0 ? void 0 : site.id}-${(_j = this.service) === null || _j === void 0 ? void 0 : _j.id}`, "quantity", {
            ...defaultJsonLogicData,
            site,
          }, 1), name: site === null || site === void 0 ? void 0 : site.name, unit: (_k = this.service) === null || _k === void 0 ? void 0 : _k.unit }));
      })))),
    ];
  }
};
PopoverSiteService.style = popoverSiteServiceCss;

export { PopoverSiteService as popover_site_service };

//# sourceMappingURL=popover-site-service.entry.js.map