'use strict';

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

const index = require('./index-ee190ee0.js');
const hardwareBackButton = require('./overlays-6a64deff.js');
const getHandlebarsHelpers = require('./getHandlebarsHelpers-8ef28401.js');
const getServiceTypes = require('./getServiceTypes-7a4ee228.js');
const fuse_esm = require('./fuse.esm-847a739f.js');
const store = require('./store-27073b3b.js');
const formatUSD = require('./formatUSD-65108a08.js');
require('./ionic-global-542e0609.js');
require('./helpers-905b0d22.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./index-4901314f.js');
require('./localforage-af1b8c0e.js');

async function searchServices(query, filter, limit) {
  let services = ((store.state === null || store.state === void 0 ? void 0 : store.state.services) || []).filter((service) => filter && typeof filter === "function" ? filter(service) : true);
  if (query === null || query === void 0 ? void 0 : query.length) {
    const fuse = new fuse_esm.Fuse(services, {
      keys: ["name", "description"],
    });
    services = fuse.search(query, { limit }).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  return services;
}

const FormScope = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.jobId = undefined;
    this.filter = {
      status: "bill",
      tab: null,
    };
    this.site = undefined;
    this.invoiceType = "water";
    this.query = undefined;
    this.sort = null;
    this.services = undefined;
    this.servicesOnBill = [];
    this.scopeToServiceMap = {};
    this.sitesToServiceMap = {};
    this.serviceTotals = {};
  }
  async onInvoiceType() {
    await this.getServices();
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findJob" &&
      this.jobId === ((_d = (_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.job) === null || _d === void 0 ? void 0 : _d.id)) {
      this.serviceTotals = {};
      this.sitesToServiceMap = {};
      const servicesOnBill = [];
      for (const jobService of ((_e = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _e === void 0 ? void 0 : _e.services) || []) {
        if (!servicesOnBill.includes((_f = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _f === void 0 ? void 0 : _f.id))
          servicesOnBill.push((_g = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _g === void 0 ? void 0 : _g.id);
        this.scopeToServiceMap[`${this.jobId}-${(_h = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _h === void 0 ? void 0 : _h.id}`] =
          jobService;
      }
      for (const site of ((_j = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _j === void 0 ? void 0 : _j.sites) || []) {
        for (const siteService of (site === null || site === void 0 ? void 0 : site.services) || []) {
          if (!servicesOnBill.includes((_k = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _k === void 0 ? void 0 : _k.id))
            servicesOnBill.push((_l = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _l === void 0 ? void 0 : _l.id);
          if (!((_m = this.sitesToServiceMap) === null || _m === void 0 ? void 0 : _m[(_o = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _o === void 0 ? void 0 : _o.id]))
            this.sitesToServiceMap[(_p = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _p === void 0 ? void 0 : _p.id] = [];
          if (!((_q = this.serviceTotals) === null || _q === void 0 ? void 0 : _q[(_r = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _r === void 0 ? void 0 : _r.id]))
            this.serviceTotals[siteService.service.id] = {
              total: 0,
              unit: ((_s = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _s === void 0 ? void 0 : _s.unit) || null,
              quantity: 0,
              price: (siteService === null || siteService === void 0 ? void 0 : siteService.price) ||
                ((_u = (_t = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _t === void 0 ? void 0 : _t.prices) === null || _u === void 0 ? void 0 : _u.default) ||
                0,
              amount: 0,
            };
          this.serviceTotals[siteService.service.id].quantity += parseFloat((siteService === null || siteService === void 0 ? void 0 : siteService.quantity) || "0");
          this.serviceTotals[siteService.service.id].amount += parseFloat((siteService === null || siteService === void 0 ? void 0 : siteService.amount) || "0");
          this.serviceTotals[siteService.service.id].total += parseFloat((siteService === null || siteService === void 0 ? void 0 : siteService.total) || "0");
          this.sitesToServiceMap[(_v = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _v === void 0 ? void 0 : _v.id].push(site);
          this.scopeToServiceMap[`${site === null || site === void 0 ? void 0 : site.id}-${(_w = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _w === void 0 ? void 0 : _w.id}`] =
            siteService;
        }
      }
      this.servicesOnBill = servicesOnBill;
      await this.getServices();
    }
    else if (["addScopeService", "editScopeService", "deleteScopeService"].includes((_x = event === null || event === void 0 ? void 0 : event.detail) === null || _x === void 0 ? void 0 : _x.endpoint)) {
      await this.getJob();
    }
  }
  async onChange(event) {
    var _a, _b, _c, _d;
    const targetEl = event === null || event === void 0 ? void 0 : event.target;
    if ((targetEl === null || targetEl === void 0 ? void 0 : targetEl.tagName) === "ION-SEARCHBAR") {
      this.query = ((_b = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.length) > 0 ? event.target.value : null;
      if (((_c = this.query) === null || _c === void 0 ? void 0 : _c.length) > 0 && ((_d = this.filter) === null || _d === void 0 ? void 0 : _d.status))
        this.filter = { ...this.filter, status: null };
    }
    await this.getServices();
  }
  async onTrigger(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    console.log(event);
    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,
      };
      if (((_h = (_g = event.detail) === null || _g === void 0 ? void 0 : _g.payload) === null || _h === void 0 ? void 0 : _h.name) === "status" &&
        ((_k = (_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.payload) === null || _k === void 0 ? void 0 : _k.value) === "bill") {
        this.query = null;
        this.searchBarEl.value = null;
      }
    }
    else if (event.detail.name === "sort") {
      this.sort = ((_m = (_l = event === null || event === void 0 ? void 0 : event.detail) === null || _l === void 0 ? void 0 : _l.payload) === null || _m === void 0 ? void 0 : _m.value) || null;
    }
    await this.getServices();
  }
  async getJob() {
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      params: {
        id: this.jobId,
        withPayments: false,
        withEfiles: false,
        withUsers: false,
        withSites: true,
        withPayModel: false,
        withPhotos: false,
        withPayables: false,
        withReadings: false,
      },
    });
  }
  removeService(event, service) {
    var _a, _b;
    event.preventDefault();
    event.stopPropagation();
    if (!confirm(`Are you sure you want to remove ${((_a = service === null || service === void 0 ? void 0 : service.service) === null || _a === void 0 ? void 0 : _a.name) ? service.service.name : "service"}?`))
      return;
    this.fireenjinFetch.emit({
      endpoint: "deleteScopeService",
      params: {
        data: {
          id: service.id,
          siteId: (_b = service === null || service === void 0 ? void 0 : service.site) === null || _b === void 0 ? void 0 : _b.id,
          jobId: this.jobId,
        },
      },
    });
  }
  addService(service, siteId) {
    this.fireenjinFetch.emit({
      endpoint: "addScopeService",
      params: {
        data: {
          serviceId: service.id,
          siteId,
          jobId: this.jobId,
        },
      },
    });
  }
  async openScopeServiceModal(event, service, currentExpanded, site) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.modalEl = await hardwareBackButton.modalController.create({
      component: "modal-scope-service",
      componentProps: {
        scopeToServiceMap: this.scopeToServiceMap,
        service,
        jobId: this.jobId,
        site,
        sites: ((_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.sites) || [],
        currentExpanded,
      },
    });
    return this.modalEl.present(event);
  }
  async openServicePopover(event, service, currentExpanded, site) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.popoverEl = await hardwareBackButton.popoverController.create({
      component: "popover-site-service",
      componentProps: {
        scopeToServiceMap: this.scopeToServiceMap,
        service,
        jobId: this.jobId,
        site,
        sites: ((_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.sites) || [],
        currentExpanded,
      },
      cssClass: "popover-site-service",
    });
    return this.popoverEl.present(event);
  }
  async getServices() {
    var _a;
    this.services = await searchServices(this.query, (service) => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
      try {
        if (((_a = this.filter) === null || _a === void 0 ? void 0 : _a.status) === "smart" &&
          getHandlebarsHelpers.logic.apply(service === null || service === void 0 ? void 0 : service.smartScoping, {
            service,
            job: store.state === null || store.state === void 0 ? void 0 : store.state.job,
          }) &&
          (!this.invoiceType ||
            (this.invoiceType &&
              ((service === null || service === void 0 ? void 0 : service.types) || []).includes(this.invoiceType))))
          return true;
      }
      catch (error) {
        console.log("Error applying json logic to service: ", service.id, error);
      }
      if (((_b = this.filter) === null || _b === void 0 ? void 0 : _b.status) === "bill" &&
        this.servicesOnBill.includes(service === null || service === void 0 ? void 0 : service.id) &&
        (!((_d = (_c = this.filter) === null || _c === void 0 ? void 0 : _c.tab) === null || _d === void 0 ? void 0 : _d.length) ||
          ((service === null || service === void 0 ? void 0 : service.tab) &&
            ((_g = (_f = (_e = this.filter) === null || _e === void 0 ? void 0 : _e.tab) === null || _f === void 0 ? void 0 : _f.includes) === null || _g === void 0 ? void 0 : _g.call(_f, getServiceTypes.cleanAndLowerString(service === null || service === void 0 ? void 0 : service.tab))))))
        return true;
      if (!((_h = this.filter) === null || _h === void 0 ? void 0 : _h.status) &&
        (service === null || service === void 0 ? void 0 : service.tab) &&
        ((_k = (_j = this.filter) === null || _j === void 0 ? void 0 : _j.tab) === null || _k === void 0 ? void 0 : _k.includes(getServiceTypes.cleanAndLowerString(service === null || service === void 0 ? void 0 : service.tab))) &&
        (!this.invoiceType ||
          (this.invoiceType &&
            (service === null || service === void 0 ? void 0 : service.tab) &&
            ((service === null || service === void 0 ? void 0 : service.types) || []).includes(this.invoiceType))))
        return true;
      if (((_l = this.query) === null || _l === void 0 ? void 0 : _l.length) &&
        (!((_o = (_m = this.filter) === null || _m === void 0 ? void 0 : _m.tab) === null || _o === void 0 ? void 0 : _o.length) ||
          ((service === null || service === void 0 ? void 0 : service.tab) &&
            ((_q = (_p = this.filter) === null || _p === void 0 ? void 0 : _p.tab) === null || _q === void 0 ? void 0 : _q.includes(getServiceTypes.cleanAndLowerString(service === null || service === void 0 ? void 0 : service.tab))))))
        return true;
      return false;
    }, ((_a = this.query) === null || _a === void 0 ? void 0 : _a.length) && 30);
  }
  async fetchData() {
    if (this.jobId) {
      this.getJob();
    }
  }
  componentDidLoad() {
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.fetchData();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    const serviceTypes = getServiceTypes.getServiceTypes();
    return (index.h(index.Host, null, index.h("fireenjin-search-bar", { ref: (el) => { var _a; return (this.searchBarEl = (_a = el === null || el === void 0 ? void 0 : el.querySelector) === null || _a === void 0 ? void 0 : _a.call(el, "ion-searchbar")); }, filters: [
        {
          name: "status",
          label: "Status",
          icon: "pricetags",
          multiple: false,
          options: [
            {
              label: "On Bill",
              value: "bill",
            },
            {
              label: "Smart Scope",
              value: "smart",
            },
            {
              label: "All",
              value: null,
            },
          ],
          value: ((_a = this.filter) === null || _a === void 0 ? void 0 : _a.status) || null,
        },
        {
          name: "tab",
          label: "Tab",
          icon: "albums",
          multiple: true,
          options: serviceTypes,
          value: (_b = this.filter) === null || _b === void 0 ? void 0 : _b.tab,
        },
      ] }), index.h("ion-card", null, index.h("ion-list", { class: "ion-no-padding" }, index.h("ion-item-divider", null, ((_c = this.filter) === null || _c === void 0 ? void 0 : _c.status) ? "Services on Bill" : "Services"), !((_d = this.query) === null || _d === void 0 ? void 0 : _d.length) &&
      !((_e = this.filter) === null || _e === void 0 ? void 0 : _e.status) &&
      !((_g = (_f = this.filter) === null || _f === void 0 ? void 0 : _f.tab) === null || _g === void 0 ? void 0 : _g.length) && (index.h("ion-grid", null, index.h("ion-row", null, getServiceTypes.getServiceTypes().map((tab) => (index.h("ion-col", { size: "6", sizeMd: "4", sizeLg: "3" }, index.h("ion-card", { href: "#" }, index.h("ion-item", { onClick: () => this.fireenjinTrigger.emit({
        name: "set",
        payload: {
          name: "tab",
          value: tab === null || tab === void 0 ? void 0 : tab.value,
        },
      }), lines: "none", detail: true }, tab === null || tab === void 0 ? void 0 : tab.value)))))))), index.h("fireenjin-pagination", { name: "scope", disableVirtualScroll: true, results: this.services, renderItem: (service) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
        return (index.h("ion-item", { href: "#", detail: true, detailIcon: "list-circle", onClick: (event) => this.openScopeServiceModal(event, service) }, index.h("ion-label", { style: {
            marginTop: "3px",
            marginBottom: "3px",
          } }, index.h("small", null, (service === null || service === void 0 ? void 0 : service.name) ? service.name : "No Name Given..."), this.servicesOnBill.includes(service === null || service === void 0 ? void 0 : service.id) ? (index.h("fireenjin-chip-bar", { style: {
            height: "auto",
            padding: "0",
          } }, ((_a = service === null || service === void 0 ? void 0 : service.tab) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "general" && (index.h("ion-chip", { onClick: (event) => {
            var _a;
            return this.openScopeServiceModal(event, service, `${(_a = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _a === void 0 ? void 0 : _a.id}-${service === null || service === void 0 ? void 0 : service.id}`);
          } }, index.h("ion-label", null, index.h("small", { style: {
            opacity: "0.6",
            letterSpacing: "2px",
            textIndent: "5px",
            display: "block",
            padding: "0 0 3px 0",
          } }, "General"), index.h("div", { style: {
            display: "flex",
            flexWrap: "nowrap",
            gap: "5px",
            textTransform: "lowercase",
          } }, index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
          } }, index.h("ion-icon", { name: "calculator", style: { width: "18px", opacity: "0.4" } }), ((_d = (_b = this.scopeToServiceMap) === null || _b === void 0 ? void 0 : _b[`${(_c = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _c === void 0 ? void 0 : _c.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _d === void 0 ? void 0 : _d.quantity) || 0), index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
          } }, index.h("ion-icon", { style: {
            width: "18px",
            opacity: "0.4",
          }, name: "scale" }), (((_g = (_e = this.scopeToServiceMap) === null || _e === void 0 ? void 0 : _e[`${(_f = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _f === void 0 ? void 0 : _f.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _g === void 0 ? void 0 : _g.amount) || 0) +
          " " +
          (((_l = (_k = (_h = this.scopeToServiceMap) === null || _h === void 0 ? void 0 : _h[`${(_j = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _j === void 0 ? void 0 : _j.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _k === void 0 ? void 0 : _k.service) === null || _l === void 0 ? void 0 : _l.unit) || "")), index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
          } }, index.h("ion-icon", { style: { width: "18px", opacity: "0.4" }, name: "cash" }), formatUSD.formatUSD(((_p = (_m = this.scopeToServiceMap) === null || _m === void 0 ? void 0 : _m[`${(_o = store.state === null || store.state === void 0 ? void 0 : store.state.job) === null || _o === void 0 ? void 0 : _o.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _p === void 0 ? void 0 : _p.total) || 0)))))), (((_q = this.sitesToServiceMap) === null || _q === void 0 ? void 0 : _q[service === null || service === void 0 ? void 0 : service.id]) || []).map((site) => {
          var _a, _b, _c, _d, _e, _f, _g, _h, _j;
          return (index.h("ion-chip", { onClick: (event) => this.openScopeServiceModal(event, service, `${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`, site) }, index.h("ion-label", null, index.h("small", { style: {
              opacity: "0.6",
              letterSpacing: "2px",
              textIndent: "5px",
              display: "block",
              padding: "0 0 3px 0",
            } }, (site === null || site === void 0 ? void 0 : site.name) || "No Site Name on File..."), index.h("div", { style: {
              display: "flex",
              flexWrap: "nowrap",
              gap: "5px",
              textTransform: "lowercase",
            } }, index.h("span", { style: {
              display: "flex",
              alignContent: "baseline",
              padding: "0",
            } }, index.h("ion-icon", { name: "calculator", style: { width: "18px", opacity: "0.4" } }), ((_b = (_a = this.scopeToServiceMap) === null || _a === void 0 ? void 0 : _a[`${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _b === void 0 ? void 0 : _b.quantity) || 0), index.h("span", { style: {
              display: "flex",
              alignContent: "baseline",
              padding: "0",
            } }, index.h("ion-icon", { style: { width: "18px", opacity: "0.4" }, name: "scale" }), (((_d = (_c = this.scopeToServiceMap) === null || _c === void 0 ? void 0 : _c[`${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _d === void 0 ? void 0 : _d.amount) || 0) +
            " " +
            (((_g = (_f = (_e = this.scopeToServiceMap) === null || _e === void 0 ? void 0 : _e[`${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _f === void 0 ? void 0 : _f.service) === null || _g === void 0 ? void 0 : _g.unit) || "")), index.h("span", { style: {
              display: "flex",
              alignContent: "baseline",
              padding: "0",
            } }, index.h("ion-icon", { style: { width: "18px", opacity: "0.4" }, name: "cash" }), formatUSD.formatUSD(((_j = (_h = this.scopeToServiceMap) === null || _h === void 0 ? void 0 : _h[`${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`]) === null || _j === void 0 ? void 0 : _j.total) || 0))))));
        }))) : (index.h("p", null, (service === null || service === void 0 ? void 0 : service.description) || "No Description Given"))), index.h("ion-chip", { class: "service-totals", color: "success", outline: true, style: {
            height: "auto",
          } }, index.h("ion-label", { style: {
            display: "flex",
            flexWrap: "nowrap",
            gap: "5px",
            textTransform: "lowercase",
          } }, index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
          } }, index.h("ion-icon", { name: "calculator", style: { width: "18px", opacity: "0.4" } }), ((_s = (_r = this.serviceTotals) === null || _r === void 0 ? void 0 : _r[service === null || service === void 0 ? void 0 : service.id]) === null || _s === void 0 ? void 0 : _s.quantity) || 0), index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
          } }, index.h("ion-icon", { style: { width: "18px", opacity: "0.4" }, name: "scale" }), (((_u = (_t = this.serviceTotals) === null || _t === void 0 ? void 0 : _t[service === null || service === void 0 ? void 0 : service.id]) === null || _u === void 0 ? void 0 : _u.amount) || 0) +
          " " +
          (((_w = (_v = this.serviceTotals) === null || _v === void 0 ? void 0 : _v[service === null || service === void 0 ? void 0 : service.id]) === null || _w === void 0 ? void 0 : _w.unit) || "")), index.h("span", { style: {
            display: "flex",
            alignContent: "baseline",
            padding: "0",
            color: "var(--ion-color-success)",
          } }, index.h("ion-icon", { style: {
            width: "18px",
            opacity: "0.4",
            color: "var(--ion-color-success)",
          }, name: "cash" }), formatUSD.formatUSD(((_y = (_x = this.serviceTotals) === null || _x === void 0 ? void 0 : _x[service === null || service === void 0 ? void 0 : service.id]) === null || _y === void 0 ? void 0 : _y.total) || "0"))))));
      } })))));
  }
  static get watchers() { return {
    "invoiceType": ["onInvoiceType"]
  }; }
};

exports.floodteam_form_scope = FormScope;
