'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-e0385008.js');
const formatUSD = require('./formatUSD-65108a08.js');
const store = require('./store-8d1de66f.js');
const getServiceTypes = require('./getServiceTypes-4320ef1f.js');
const hardwareBackButton = require('./overlays-83fe75fb.js');
require('./index-37fc02d8.js');
require('./getHandlebarsHelpers-1b368c8a.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./localforage-af1b8c0e.js');
require('./fuse.esm-847a739f.js');
require('./ionic-global-45d4a7f7.js');
require('./helpers-905b0d22.js');

const appServiceListCss = "";

const AppServiceList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinSubmit = index.createEvent(this, "fireenjinSubmit", 7);
    this.floodteamCloseModal = index.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 getServiceTypes.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 getServiceTypes.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 hardwareBackButton.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 (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.services = await getServiceTypes.searchServices(this.query);
    console.log(this.services);
  }
  render() {
    var _a, _b, _c;
    return [
      index.h("ion-content", { class: "ion-padding" }, index.h("floodteam-title-bar", null, index.h("ion-router-link", { href: "/services" }, "Services"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { fill: "solid", color: "primary", href: "/services/new" }, "Add"))), index.h("fireenjin-search-bar", { paginationEl: this.paginationEl, filters: ((_a = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _a === void 0 ? void 0 : _a.tester) || ((_b = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _b === void 0 ? void 0 : _b.admin)
          ? [
            {
              name: "tab",
              label: "Tab",
              icon: "albums",
              multiple: true,
              options: getServiceTypes.getServiceTypes(),
              value: (_c = this.filter) === null || _c === void 0 ? void 0 : _c.tab,
            },
          ]
          : null }), index.h("ion-card", { class: "list-card" }, index.h("ion-list", { class: "ion-no-padding" }, index.h("fireenjin-pagination", { name: "services", approxItemHeight: 84, results: this.services, renderItem: (service) => (index.h("ion-item", { href: `/services/${service.id}`, detail: true }, index.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) }), index.h("ion-label", null, index.h("h2", null, (service === null || service === void 0 ? void 0 : service.name) || "", " "), index.h("div", null, Object.entries((service === null || service === void 0 ? void 0 : service.prices) || {}).map(([name, amount]) => (index.h("ion-chip", null, index.h("ion-label", null, name || "", ":", " ", index.h("ion-text", { color: "success", slot: "end" }, formatUSD.formatUSD(amount || 0)))))))))) })))),
      index.h("floodteam-navigation-footer", null, index.h("fireenjin-chip-bar", null, index.h("ion-chip", { onClick: () => this.fireenjinTrigger.emit({
          name: "import",
          type: "services",
        }) }, index.h("ion-icon", { class: "ft-icon-style", name: "cloud-upload" }), index.h("ion-label", null, "Import from CSV")), index.h("ion-chip", { onClick: () => this.fireenjinTrigger.emit({
          name: "export",
          collection: "services",
        }) }, index.h("ion-icon", { name: "cloud-download", class: "ft-icon-style" }), index.h("ion-label", null, "Export to CSV")), index.h("ion-chip", { onClick: () => this.openCodeEditor("autofill", null) }, index.h("ion-icon", { class: "ft-icon-style", name: "code" }), index.h("ion-label", null, "Set Auto-Fill")), index.h("ion-chip", { onClick: () => this.openCodeEditor("smartScoping", null) }, index.h("ion-icon", { class: "ft-icon-style", name: "code" }), index.h("ion-label", null, "Set Smart Scoping")))),
    ];
  }
};
AppServiceList.style = appServiceListCss;

exports.app_service_list = AppServiceList;
