import * as jsonLogic from "json-logic-js";
import { Component, Event, h, Listen, Prop, } from "@stencil/core";
export class PopoverSiteService {
  constructor() {
    this.service = {};
    this.scopeToServiceMap = {};
    this.sites = [];
  }
  async onSuccess(event) {
    var _a;
    if (["editScopeService", "addScopeService", "deleteScopeService"].includes((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.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 : jsonLogic.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;
    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 })) : ((this.sites || [])
          .sort((a, b) => ((a === null || a === void 0 ? void 0 : a.name) > (b === null || b === void 0 ? void 0 : b.name) ? 1 : -1))
          .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, 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 }));
        })))),
    ];
  }
  static get is() { return "popover-site-service"; }
  static get originalStyleUrls() { return {
    "$": ["popover-site-service.css"]
  }; }
  static get styleUrls() { return {
    "$": ["popover-site-service.css"]
  }; }
  static get properties() { return {
    "service": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Service",
        "resolved": "{ __typename?: \"Service\"; autofill?: any; category?: string; code?: string; costs?: any; createdAt?: string; createdBy?: User; description?: string; group?: string; id?: string; name?: string; prices?: any; referencePage?: string; rental?: string; smartScoping?: any; tab?: string; type?: string; unit?: string; updatedAt?: string; updatedBy?: User; }",
        "references": {
          "Service": {
            "location": "import",
            "path": "../../../sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    },
    "scopeToServiceMap": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{\n    [parentIdServiceId: string]: ScopeService;\n  }",
        "resolved": "{ [parentIdServiceId: string]: ScopeService; }",
        "references": {
          "ScopeService": {
            "location": "import",
            "path": "../../../sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    },
    "sites": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Site[]",
        "resolved": "Site[]",
        "references": {
          "Site": {
            "location": "import",
            "path": "../../../sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    },
    "jobId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "job-id",
      "reflect": false
    },
    "job": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Job",
        "resolved": "{ __typename?: \"Job\"; address?: Address; afterhours?: boolean; amountBilled?: number; amountDeducted?: number; amountOwed?: number; amountPaid?: number; appointment?: Appointment; appointments?: Appointment[]; attributionToken?: Token; category?: number; cause?: string; claimNumber?: string; createdAt?: string; createdBy?: User; customer?: User; dateOfLoss?: string; efileCount?: number; efiles?: Efile[]; feed?: Feed[]; id?: string; invoices?: Invoice[]; isFranchise?: boolean; isInsured?: boolean; isSearchable?: boolean; location?: Location; manager?: User; matterportModelId?: string; payModel?: PayModel; payables?: Payable[]; payments?: Payment[]; phone?: string; photos?: Photo[]; picture?: string; propertyAge?: number; ratings?: Rating[]; readings?: Reading[]; referral?: User; services?: ScopeService[]; sites?: Site[]; status?: string; tags?: string[]; title?: string; type?: string; updatedAt?: string; updatedBy?: User; userIds?: string[]; users?: User[]; weather?: Weather[]; weatherIds?: string[]; }",
        "references": {
          "Job": {
            "location": "import",
            "path": "../../../sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "currentExpanded": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "current-expanded",
      "reflect": false
    }
  }; }
  static get events() { return [{
      "method": "fireenjinFetch",
      "name": "fireenjinFetch",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get listeners() { return [{
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": "body",
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=popover-site-service.js.map