import { r as registerInstance, d as createEvent, B as Build, h } from './index-000d2977.js';
import { f as formatUSD } from './formatUSD-dae09890.js';
import { s as state } from './store-f6a9eb30.js';
import { p as pick } from './pick-1c5678f4.js';
import { g as getInvoiceTypes } from './getInvoiceTypes-d6baa75e.js';
import './getHandlebarsHelpers-91de4454.js';
import './_commonjsHelpers-4ed75ef8.js';
import './logic-88140d62.js';
import './index-d4e79b88.js';
import './localforage-9daa31d6.js';

const appServiceCss = "";

const AppService = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinSubmit = createEvent(this, "fireenjinSubmit", 7);
    this.serviceInputKeys = [
      "autofill",
      "name",
      "smartScoping",
      "code",
      "description",
      "group",
      "prices",
      "tab",
      "type",
      "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, _p;
    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-select", { name: "types", label: "Types", value: (_a = this.service) === null || _a === void 0 ? void 0 : _a.types, multiple: true, labelPosition: "stacked", options: getInvoiceTypes() }), h("fireenjin-input", { name: "name", label: "Name", value: (_b = this.service) === null || _b === void 0 ? void 0 : _b.name, labelPosition: "stacked" }), h("fireenjin-input", { name: "code", label: "Code", value: (_c = this.service) === null || _c === void 0 ? void 0 : _c.code, labelPosition: "stacked" }), h("fireenjin-select", { name: "unit", label: "Unit", value: (_d = this.service) === null || _d === void 0 ? void 0 : _d.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: (_e = this.service) === null || _e === void 0 ? void 0 : _e.description, labelPosition: "stacked" }), h("fireenjin-input", { name: "group", label: "Group", value: (_f = this.service) === null || _f === void 0 ? void 0 : _f.group, labelPosition: "stacked" }), h("fireenjin-input", { name: "tab", label: "Tab", value: (_g = this.service) === null || _g === void 0 ? void 0 : _g.tab, labelPosition: "stacked" })))), h("ion-col", null, h("ion-card", null, h("ion-list", { class: "ion-no-padding" }, h("ion-item-divider", null, "Pricing"), (_k = (_j = Object.keys(((_h = this.service) === null || _h === void 0 ? void 0 : _h.prices) || {})).map) === null || _k === void 0 ? void 0 :
        _k.call(_j, (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: ((_l = this.service) === null || _l === void 0 ? void 0 : _l.smartScoping)
          ? JSON.stringify((_m = this.service) === null || _m === void 0 ? void 0 : _m.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: ((_o = this.service) === null || _o === void 0 ? void 0 : _o.autofill)
          ? JSON.stringify((_p = this.service) === null || _p === void 0 ? void 0 : _p.autofill)
          : "" })))))))),
      h("floodteam-navigation-footer", null),
    ];
  }
};
AppService.style = appServiceCss;

export { AppService as app_service };
