import { Build, Component, Event, h, Listen, Prop, } from "@stencil/core";
import formatUSD from "../../../helpers/formatUSD";
import state from "../../../store";
import pick from "../../../helpers/pick";
export class AppService {
  constructor() {
    this.serviceInputKeys = [
      "autofill",
      "name",
      "smartScoping",
      "code",
      "description",
      "group",
      "prices",
      "tab",
      "unit",
    ];
    this.service = {};
  }
  onChange(event) {
    var _a, _b, _c, _d;
    if (!((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) ||
      !this.serviceInputKeys.includes((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.name))
      return;
    this.service[event.target.name] =
      ((_c = event.detail) === null || _c === void 0 ? void 0 : _c.value) || ((_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.value);
  }
  async save() {
    var _a, _b, _c, _d;
    const data = {
      ...pick(this.service, this.serviceInputKeys),
      autofill: ((_a = this.service) === null || _a === void 0 ? void 0 : _a.autofill) || null,
      smartScoping: ((_b = this.service) === null || _b === void 0 ? void 0 : _b.smartScoping) || null,
    };
    for (const [priceName, amount] of Object.entries((data === null || data === void 0 ? void 0 : data.prices) || {})) {
      data.prices[priceName] =
        typeof amount === "string"
          ? parseFloat(amount.replace(/\$|,/g, ""))
          : amount || 0;
    }
    try {
      if (typeof (data === null || data === void 0 ? void 0 : data.smartScoping) === "string" &&
        ["{", "["].includes((_c = data === null || data === void 0 ? void 0 : data.smartScoping) === null || _c === void 0 ? void 0 : _c.substr(0, 1)))
        data.smartScoping = JSON.parse(data.smartScoping);
    }
    catch (e) {
      alert("There was an error parsing your smart scoping rules!");
      return;
    }
    try {
      if (typeof (data === null || data === void 0 ? void 0 : data.autofill) === "string" &&
        ["{", "["].includes((_d = data === null || data === void 0 ? void 0 : data.autofill) === null || _d === void 0 ? void 0 : _d.substr(0, 1)))
        data.autofill = JSON.parse(data.autofill);
    }
    catch (e) {
      alert("There was an error parsing your smart scoping rules!");
      return;
    }
    if (!this.serviceId || this.serviceId === "new") {
      this.fireenjinSubmit.emit({
        endpoint: "addService",
        data,
      });
    }
    else {
      this.fireenjinSubmit.emit({
        endpoint: "editService",
        id: this.serviceId,
        data,
      });
    }
  }
  async fetchData() {
    this.service = ((state === null || state === void 0 ? void 0 : state.services) || []).find((service) => (service === null || service === void 0 ? void 0 : service.id) === this.serviceId);
  }
  componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    if (this.serviceId && this.serviceId !== "new")
      this.fetchData();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    return [
      h("ion-content", { class: "ion-padding" },
        h("floodteam-title-bar", null,
          h("ion-router-link", { href: "/services" }, "Services"),
          h("ion-buttons", { slot: "end" },
            h("ion-button", { fill: "solid", color: "primary", onClick: () => this.save() }, "Save"))),
        h("ion-grid", null,
          h("ion-row", null,
            h("ion-col", null,
              h("ion-card", null,
                h("ion-list", { class: "ion-no-padding" },
                  h("ion-item-divider", null, "Service Information"),
                  h("fireenjin-input", { name: "name", label: "Name", value: (_a = this.service) === null || _a === void 0 ? void 0 : _a.name, labelPosition: "stacked" }),
                  h("fireenjin-input", { name: "code", label: "Code", value: (_b = this.service) === null || _b === void 0 ? void 0 : _b.code, labelPosition: "stacked" }),
                  h("fireenjin-select", { name: "unit", label: "Unit", value: (_c = this.service) === null || _c === void 0 ? void 0 : _c.unit, labelPosition: "stacked", options: [
                      {
                        label: "Square Feet",
                        value: "sqft",
                      },
                      {
                        label: "Lineal Feet",
                        value: "lnft",
                      },
                      {
                        label: "Per Hour",
                        value: "hour",
                      },
                      {
                        label: "Per Day",
                        value: "day",
                      },
                      {
                        label: "Per Item",
                        value: "each",
                      },
                    ] }),
                  h("fireenjin-input", { name: "description", label: "Description", value: (_d = this.service) === null || _d === void 0 ? void 0 : _d.description, labelPosition: "stacked" }),
                  h("fireenjin-input", { name: "group", label: "Group", value: (_e = this.service) === null || _e === void 0 ? void 0 : _e.group, labelPosition: "stacked" }),
                  h("fireenjin-input", { name: "tab", label: "Tab", value: (_f = this.service) === null || _f === void 0 ? void 0 : _f.tab, labelPosition: "stacked" })))),
            h("ion-col", null,
              h("ion-card", null,
                h("ion-list", { class: "ion-no-padding" },
                  h("ion-item-divider", null, "Pricing"), (_j = (_h = Object.keys(((_g = this.service) === null || _g === void 0 ? void 0 : _g.prices) || {})).map) === null || _j === void 0 ? void 0 :
                  _j.call(_h, (name) => (h("ion-item", null,
                    h("ion-label", null,
                      name || "",
                      " - ",
                      formatUSD(this.service.prices[name]),
                      " ")))),
                  h("ion-item-divider", null, "Add Price"),
                  h("fireenjin-form", { class: "ion-padding", disableLoader: true, name: "pricing", submitButton: "Add", resetButton: "Clear", beforeSubmit: (data) => {
                      var _a;
                      this.service = {
                        ...this.service,
                        prices: {
                          ...(((_a = this.service) === null || _a === void 0 ? void 0 : _a.prices) || {}),
                          [(data === null || data === void 0 ? void 0 : data.price) || "default"]: parseFloat((data === null || data === void 0 ? void 0 : data.amount) || 0),
                        },
                      };
                      return data;
                    } },
                    h("fireenjin-input", { name: "price", label: "Price Name", labelPosition: "stacked" }),
                    h("fireenjin-input", { type: "number", step: "0.01", name: "amount", label: "Amount", labelPosition: "stacked" })))))),
          h("ion-row", null,
            h("ion-col", { size: "12", sizeMd: "6" },
              h("ion-card", null,
                h("ion-list", { class: "ion-no-padding" },
                  h("ion-item-divider", null, "Smart Scoping"),
                  (Build === null || Build === void 0 ? void 0 : Build.isBrowser) && (h("fireenjin-code-editor", { autoExpand: true, language: "json", name: "smartScoping", value: ((_k = this.service) === null || _k === void 0 ? void 0 : _k.smartScoping)
                      ? JSON.stringify((_l = this.service) === null || _l === void 0 ? void 0 : _l.smartScoping)
                      : "" }))))),
            h("ion-col", { size: "12", sizeMd: "6" },
              h("ion-card", null,
                h("ion-list", { class: "ion-no-padding" },
                  h("ion-item-divider", null, "Autofill"),
                  (Build === null || Build === void 0 ? void 0 : Build.isBrowser) && (h("fireenjin-code-editor", { autoExpand: true, language: "json", name: "autofill", value: ((_m = this.service) === null || _m === void 0 ? void 0 : _m.autofill)
                      ? JSON.stringify((_o = this.service) === null || _o === void 0 ? void 0 : _o.autofill)
                      : "" })))))))),
      h("floodteam-navigation-footer", null),
    ];
  }
  static get is() { return "app-service"; }
  static get originalStyleUrls() { return {
    "$": ["app-service.css"]
  }; }
  static get styleUrls() { return {
    "$": ["app-service.css"]
  }; }
  static get properties() { return {
    "serviceId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "service-id",
      "reflect": false
    },
    "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": "{}"
    }
  }; }
  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": "ionChange",
      "method": "onChange",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "fireenjinCodeChange",
      "method": "onChange",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=app-service.js.map