'use strict';

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

const index = require('./index-fd2bbf0b.js');
const formatUSD = require('./formatUSD-65108a08.js');
const store = require('./store-9988299d.js');
const getServiceTypes = require('./getServiceTypes-30604c9e.js');
require('./index-81aa1bcb.js');
require('./getCache-901b0f67.js');
require('./fuse.esm-580ef9a3.js');

function flatten(obj, roots = [], sep = ".") {
  return (Object
    // find props of given object
    .keys(obj)
    // return an object by iterating props
    .reduce((memo, prop) => Object.assign(
  // create a new object
  {}, 
  // include previously returned object
  memo, Object.prototype.toString.call(obj[prop]) === "[object Object]"
    ? // keep working if value is an object
      flatten(obj[prop], roots.concat([prop]), sep)
    : // include current prop and value and prefix prop with the roots
      { [roots.concat([prop]).join(sep)]: obj[prop] }), {}));
}

function omit(obj, ...props) {
  const result = { ...obj };
  props.forEach(function (prop) {
    delete result[prop];
  });
  return result;
}

const appServiceListCss = "";

let AppServiceList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.selectedIds = [];
    this.selectedServices = [];
    this.services = [];
    this.filter = {
      tab: null,
    };
  }
  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, _c;
    if (!((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value))
      return;
    event.preventDefault();
    event.stopPropagation();
    const selectedIds = this.selectedIds;
    console.log(event);
    if (this.selectedIds.includes((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value)) {
      selectedIds.splice(this.selectedIds.indexOf(event.target.value), 1);
    }
    else {
      selectedIds.push((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.value);
    }
    this.selectedIds = selectedIds;
    console.log(this.selectedIds);
  }
  async componentDidLoad() {
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.services = await getServiceTypes.searchServices(this.query);
  }
  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", null, index.h("ion-list", { class: "ion-no-padding" }, index.h("fireenjin-pagination", { 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", value: service === null || service === void 0 ? void 0 : service.id, onClick: (event) => this.toggleService(event), 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",
          payload: this.services.map((service) => flatten(omit(service, "createdAt", "updatedAt"))),
        }) }, index.h("ion-icon", { name: "cloud-download", class: "ft-icon-style" }), index.h("ion-label", null, "Export to CSV")))),
    ];
  }
};
AppServiceList.style = appServiceListCss;

exports.app_service_list = AppServiceList;

//# sourceMappingURL=app-service-list.cjs.entry.js.map