import { Component, Event, Prop, h, Listen } from "@stencil/core";
import formatUSD from "../../helpers/formatUSD";
export class FormSiteService {
  constructor() {
    this.isGeneral = false;
    this.amount = 0;
    this.quantity = 1;
    this.price = 0;
  }
  onSuccess(event) {
    var _a, _b, _c, _d, _e;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "deleteScopeService") {
      this.amount = 0;
      this.quantity = 1;
      this.siteServiceId = null;
    }
    else if (((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.endpoint) === "addScopeService") {
      this.siteServiceId = (_e = (_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.addScopeService) === null || _e === void 0 ? void 0 : _e.id;
    }
  }
  onReset(event) {
    this.fireenjinSubmit.emit({
      endpoint: "deleteScopeService",
      event,
      data: {
        id: this.siteServiceId,
        siteId: this.siteId,
        jobId: this.jobId,
      },
    });
  }
  render() {
    var _a, _b, _c, _d;
    return (h("ion-accordion", { value: `${this.isGeneral ? this.jobId : this.siteId}-${this.serviceId}` },
      h("ion-item", { slot: "header" },
        h("ion-label", null,
          h("h2", null, this.name ||
            (this.isGeneral ? "General Service" : "No Site Name")),
          this.siteServiceId ? (h("ion-text", { color: "primary" },
            h("b", null,
              this.amount || 0,
              " ",
              (this === null || this === void 0 ? void 0 : this.unit) || ""),
            " ",
            "x ",
            h("b", null,
              this.quantity || 1,
              " Qty"),
            " x",
            " ",
            h("b", null, formatUSD(this.price || 0)))) : (h("p", null, "Click to Add"))),
        h("ion-text", { color: "success", slot: "end" }, this.siteServiceId && (h("small", null, formatUSD(parseFloat(this.amount || 0) *
          parseFloat(this.quantity || 1) *
          parseFloat(this.price || 0)))))),
      h("fireenjin-form", { slot: "content", disableLoader: true, name: `siteService-${this.siteId}-${this.serviceId}`, resetButton: "Remove", resetButtonColor: "danger", disableReset: true, endpoint: this.siteServiceId ? "editScopeService" : "addScopeService", beforeSubmit: async (data) => {
          const formData = {
            jobId: this.jobId,
            siteId: this.siteId,
            ...(this.siteServiceId
              ? { id: this.siteServiceId }
              : { serviceId: this.serviceId }),
            price: parseFloat((data === null || data === void 0 ? void 0 : data.price) || this.price || 0),
            quantity: parseFloat((data === null || data === void 0 ? void 0 : data.quantity) || this.quantity || 1),
            amount: parseFloat((data === null || data === void 0 ? void 0 : data.amount) || this.amount || 1),
          };
          if (data === null || data === void 0 ? void 0 : data.price) {
            try {
              formData.price = parseFloat((data === null || data === void 0 ? void 0 : data.price) || 0);
            }
            catch (error) {
              console.log("Error setting price: ", error);
            }
          }
          return formData;
        } },
        h("ion-grid", { class: "ion-no-padding" },
          h("ion-row", { class: "ion-no-padding" },
            h("ion-col", { size: "6", sizeXs: "4" },
              h("fireenjin-input", { type: "number", name: "amount", value: this.amount || 0, label: "Amount", labelPosition: "stacked", min: "0", step: "0.01" })),
            h("ion-col", { size: "6", sizeXs: "4" },
              h("fireenjin-input", { type: "number", name: "quantity", value: this.quantity || 1, label: "Quantity", labelPosition: "stacked", min: "1" })),
            h("ion-col", { size: "12", sizeXs: "4" },
              h("fireenjin-input", { type: "number", name: "price", value: this.price || 0, label: "Price", labelPosition: "stacked", step: "0.01", min: "0" }))),
          h("ion-row", null,
            h("ion-col", null,
              h("fireenjin-chip-bar", null, [
                {
                  label: "Affected Sqft",
                  value: (_a = this.site) === null || _a === void 0 ? void 0 : _a.affectedAreaTotalSqft,
                },
                {
                  label: "Affected CuSqft",
                  value: (_b = this.site) === null || _b === void 0 ? void 0 : _b.affectedAreaTotalCuft,
                },
                {
                  label: "Total Sqft",
                  value: (_c = this.site) === null || _c === void 0 ? void 0 : _c.areaTotalSqft,
                },
                {
                  label: "Total CuSqft",
                  value: (_d = this.site) === null || _d === void 0 ? void 0 : _d.areaTotalCuft,
                },
              ]
                .filter((chip) => (chip === null || chip === void 0 ? void 0 : chip.value) > 0)
                .map((chip) => (h("ion-chip", { onClick: () => (this.amount = (chip === null || chip === void 0 ? void 0 : chip.value) || 0) },
                (chip === null || chip === void 0 ? void 0 : chip.label) || "",
                " - ",
                (chip === null || chip === void 0 ? void 0 : chip.value) || ""))))))))));
  }
  static get is() { return "floodteam-form-site-service"; }
  static get properties() { return {
    "siteId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "site-id",
      "reflect": false
    },
    "name": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "name",
      "reflect": false
    },
    "serviceId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "service-id",
      "reflect": false
    },
    "jobId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "job-id",
      "reflect": false
    },
    "unit": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "unit",
      "reflect": false
    },
    "siteServiceId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "site-service-id",
      "reflect": false
    },
    "isGeneral": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-general",
      "reflect": false,
      "defaultValue": "false"
    },
    "site": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Site",
        "resolved": "{ __typename?: \"Site\"; affectedAreaTotalCuft?: number; affectedAreaTotalSqft?: number; affectedAreas?: Area[]; areaTotalCuft?: number; areaTotalSqft?: number; areas?: Area[]; ceilingType?: string; chamber?: number; class?: number; createdAt?: string; createdBy?: User; dots?: Dot[]; equipment?: SiteEquipment[]; exhaust?: Exhaust; floorType?: string; id?: string; inventories?: Inventory[]; job?: Job; level?: number; name?: string; offsets?: number; photos?: Photo[]; services?: ScopeService[]; updatedAt?: string; updatedBy?: User; wallType?: string; }",
        "references": {
          "Site": {
            "location": "import",
            "path": "../../helpers/sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "amount": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "amount",
      "reflect": false,
      "defaultValue": "0"
    },
    "quantity": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "quantity",
      "reflect": false,
      "defaultValue": "1"
    },
    "price": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "price",
      "reflect": false,
      "defaultValue": "0"
    }
  }; }
  static get events() { return [{
      "method": "fireenjinSubmit",
      "name": "fireenjinSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinSubmitEvent",
        "resolved": "FireEnjinSubmitEvent",
        "references": {
          "FireEnjinSubmitEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }]; }
  static get listeners() { return [{
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "fireenjinReset",
      "method": "onReset",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=form-site-service.js.map