import { r as registerInstance, d as createEvent, B as Build, h } from './index-26447f8a.js';
import { f as formatUSD } from './formatUSD-dae09890.js';
import { s as state } from './store-838a3050.js';
import { s as searchServices, g as getServiceTypes } from './getServiceTypes-a2edbe98.js';
import { m as modalController } from './overlays-a3190093.js';
import './index-b0d3ac50.js';
import './getHandlebarsHelpers-0376b57c.js';
import './_commonjsHelpers-7c4ea794.js';
import './index-e6b3332e.js';
import './localforage-c63d55c8.js';
import './fuse.esm-80f40788.js';
import './ionic-global-b53b5faa.js';
import './helpers-06537f39.js';

const appServiceListCss = "";

const AppServiceList = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinSubmit = createEvent(this, "fireenjinSubmit", 7);
    this.floodteamCloseModal = createEvent(this, "floodteamCloseModal", 7);
    this.paginationEl = undefined;
    this.query = undefined;
    this.selectedIds = [];
    this.selectedServices = [];
    this.services = [];
    this.filter = {
      tab: null,
    };
  }
  onCodeChange(event) {
    var _a, _b;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) === "bulkEdit")
      return;
    for (const id of this.selectedIds) {
      this.fireenjinSubmit.emit({
        name: "bulkEdit",
        endpoint: "editService",
        params: {
          id,
          data: ((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.data) || {},
        },
      });
    }
    this.floodteamCloseModal.emit();
  }
  async onSearchInput(event) {
    this.query = event.target.value;
    this.services = await searchServices(this.query);
  }
  async onTrigger(event) {
    var _a, _b, _c, _d, _e, _f;
    if (event.detail.name === "set" && ((_b = (_a = event.detail) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.name)) {
      this.filter = {
        ...this.filter,
        [(_d = (_c = event.detail) === null || _c === void 0 ? void 0 : _c.payload) === null || _d === void 0 ? void 0 : _d.name]: ((_f = (_e = event === null || event === void 0 ? void 0 : event.detail) === null || _e === void 0 ? void 0 : _e.payload) === null || _f === void 0 ? void 0 : _f.value) || null,
      };
      this.services = await searchServices(this.query, (service) => { var _a, _b; return (_b = (_a = this.filter) === null || _a === void 0 ? void 0 : _a.tab) === null || _b === void 0 ? void 0 : _b.includes(service === null || service === void 0 ? void 0 : service.tab); });
    }
  }
  async toggleService(event) {
    var _a, _b;
    event.preventDefault();
    event.stopPropagation();
    const selectedIds = this.selectedIds;
    console.log(event);
    if (this.selectedIds.includes((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value)) {
      selectedIds.splice(this.selectedIds.indexOf(event.target.value), 1);
    }
    else {
      selectedIds.push((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value);
    }
    this.selectedIds = selectedIds;
    console.log(this.selectedIds);
  }
  async openCodeEditor(name, value) {
    const modalEl = await modalController.create({
      component: "fireenjin-mx-modal-code-editor",
      cssClass: "fireenjin-mx-modal-code-editor-wrapper",
      componentProps: {
        pageTitle: name === "autofill" ? "Set Auto-Fill" : "Set Smart Scoping",
        name,
        language: "json",
        value,
        endpoint: "",
        documentId: true,
      },
    });
    await modalEl.present();
    return modalEl;
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.services = await searchServices(this.query);
    console.log(this.services);
  }
  render() {
    var _a, _b, _c;
    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", href: "/services/new" }, "Add"))), h("fireenjin-search-bar", { paginationEl: this.paginationEl, filters: ((_a = state === null || state === void 0 ? void 0 : state.claims) === null || _a === void 0 ? void 0 : _a.tester) || ((_b = state === null || state === void 0 ? void 0 : state.claims) === null || _b === void 0 ? void 0 : _b.admin)
          ? [
            {
              name: "tab",
              label: "Tab",
              icon: "albums",
              multiple: true,
              options: getServiceTypes(),
              value: (_c = this.filter) === null || _c === void 0 ? void 0 : _c.tab,
            },
          ]
          : null }), h("ion-card", { class: "list-card" }, h("ion-list", { class: "ion-no-padding" }, h("fireenjin-pagination", { name: "services", approxItemHeight: 84, results: this.services, renderItem: (service) => (h("ion-item", { href: `/services/${service.id}`, detail: true }, h("ion-checkbox", { id: `toggle.${(service === null || service === void 0 ? void 0 : service.id) || ""}`, slot: "start", onClick: (event) => this.toggleService(event), value: service === null || service === void 0 ? void 0 : service.id, checked: this.selectedIds.includes(service === null || service === void 0 ? void 0 : service.id) }), h("ion-label", null, h("h2", null, (service === null || service === void 0 ? void 0 : service.name) || "", " "), h("div", null, Object.entries((service === null || service === void 0 ? void 0 : service.prices) || {}).map(([name, amount]) => (h("ion-chip", null, h("ion-label", null, name || "", ":", " ", h("ion-text", { color: "success", slot: "end" }, formatUSD(amount || 0)))))))))) })))),
      h("floodteam-navigation-footer", null, h("fireenjin-chip-bar", null, h("ion-chip", { onClick: () => this.fireenjinTrigger.emit({
          name: "import",
          type: "services",
        }) }, h("ion-icon", { class: "ft-icon-style", name: "cloud-upload" }), h("ion-label", null, "Import from CSV")), h("ion-chip", { onClick: () => this.fireenjinTrigger.emit({
          name: "export",
          collection: "services",
        }) }, h("ion-icon", { name: "cloud-download", class: "ft-icon-style" }), h("ion-label", null, "Export to CSV")), h("ion-chip", { onClick: () => this.openCodeEditor("autofill", null) }, h("ion-icon", { class: "ft-icon-style", name: "code" }), h("ion-label", null, "Set Auto-Fill")), h("ion-chip", { onClick: () => this.openCodeEditor("smartScoping", null) }, h("ion-icon", { class: "ft-icon-style", name: "code" }), h("ion-label", null, "Set Smart Scoping")))),
    ];
  }
};
AppServiceList.style = appServiceListCss;

export { AppServiceList as app_service_list };
