'use strict';

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

const index = require('./index-fd2bbf0b.js');
const hardwareBackButton = require('./overlays-0248227e.js');
const logic = require('./logic-d8c34975.js');
const getServiceTypes = require('./getServiceTypes-0016414e.js');
const store = require('./store-d6f21087.js');
require('./ionic-global-1340f737.js');
require('./helpers-c2496722.js');
require('./_commonjsHelpers-a5111d61.js');
require('./fuse.esm-847a739f.js');
require('./index-81aa1bcb.js');
require('./getHandlebarsHelpers-7b2b1728.js');
require('./index-1f381af8.js');
require('./localforage-af1b8c0e.js');

let FormScope = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.filter = {
      status: "bill",
      tab: null,
    };
    this.invoiceType = "mitigation";
    this.sort = null;
    this.servicesOnBill = [];
    this.scopeToServiceMap = {};
    this.sitesToServiceMap = {};
  }
  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;
    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.sitesToServiceMap = {};
      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 (!this.servicesOnBill.includes((_f = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _f === void 0 ? void 0 : _f.id))
          this.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 (!this.servicesOnBill.includes((_k = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _k === void 0 ? void 0 : _k.id))
            this.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] = [];
          this.sitesToServiceMap[(_q = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _q === void 0 ? void 0 : _q.id].push(site);
          this.scopeToServiceMap[`${site === null || site === void 0 ? void 0 : site.id}-${(_r = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _r === void 0 ? void 0 : _r.id}`] =
            siteService;
        }
      }
      await this.getServices();
    }
    else if (["addScopeService", "editScopeService", "deleteScopeService"].includes((_s = event === null || event === void 0 ? void 0 : event.detail) === null || _s === void 0 ? void 0 : _s.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.status = null;
        this.filter = { ...this.filter };
      }
    }
    this.getServices();
  }
  async onTrigger(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    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,
      };
    }
    else if (event.detail.name === "sort") {
      this.sort = ((_h = (_g = event === null || event === void 0 ? void 0 : event.detail) === null || _g === void 0 ? void 0 : _g.payload) === null || _h === void 0 ? void 0 : _h.value) || null;
    }
    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 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() {
    this.services = await getServiceTypes.searchServices(this.query, (service) => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
      if (!((_a = this.filter) === null || _a === void 0 ? void 0 : _a.status) &&
        !((_b = this.filter) === null || _b === void 0 ? void 0 : _b.tab) &&
        this.invoiceType &&
        (!this.invoiceType ||
          (this.invoiceType &&
            ((service === null || service === void 0 ? void 0 : service.types) || []).includes(this.invoiceType))))
        return true;
      try {
        if (((_c = this.filter) === null || _c === void 0 ? void 0 : _c.status) === "smart" &&
          logic.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 (((_d = this.filter) === null || _d === void 0 ? void 0 : _d.status) === "bill" &&
        this.servicesOnBill.includes(service === null || service === void 0 ? void 0 : service.id) &&
        (!((_f = (_e = this.filter) === null || _e === void 0 ? void 0 : _e.tab) === null || _f === void 0 ? void 0 : _f.length) ||
          ((_h = (_g = this.filter) === null || _g === void 0 ? void 0 : _g.tab) === null || _h === void 0 ? void 0 : _h.includes(service === null || service === void 0 ? void 0 : service.tab))) &&
        this.invoiceType &&
        ((service === null || service === void 0 ? void 0 : service.types) || []).includes(this.invoiceType))
        return true;
      if (!((_j = this.filter) === null || _j === void 0 ? void 0 : _j.status) &&
        ((_l = (_k = this.filter) === null || _k === void 0 ? void 0 : _k.tab) === null || _l === void 0 ? void 0 : _l.includes(service === null || service === void 0 ? void 0 : service.tab)) &&
        (!this.invoiceType ||
          (this.invoiceType &&
            ((service === null || service === void 0 ? void 0 : service.types) || []).includes(this.invoiceType))))
        return true;
      return false;
    });
  }
  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;
    return (index.h(index.Host, null, index.h("fireenjin-search-bar", { 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: getServiceTypes.getServiceTypes(),
          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"), index.h("fireenjin-pagination", { approxItemHeight: 55, disableVirtualScroll: true, results: this.services, renderItem: (service) => {
        var _a, _b;
        return (index.h("ion-item", { href: "#", detail: true, detailIcon: "list-circle", onClick: (event) => this.openServicePopover(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", { style: {
            height: "20px",
            paddingTop: "0px",
            paddingBottom: "0px",
          }, onClick: (event) => {
            var _a;
            return this.openServicePopover(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("small", null, "General"))), (((_b = this.sitesToServiceMap) === null || _b === void 0 ? void 0 : _b[service === null || service === void 0 ? void 0 : service.id]) || []).map((site) => (index.h("ion-chip", { style: {
            height: "20px",
            paddingTop: "0px",
            paddingBottom: "0px",
          }, onClick: (event) => this.openServicePopover(event, service, `${site === null || site === void 0 ? void 0 : site.id}-${service === null || service === void 0 ? void 0 : service.id}`, site) }, index.h("small", null, (site === null || site === void 0 ? void 0 : site.name) || "No Name")))))) : (index.h("p", null, (service === null || service === void 0 ? void 0 : service.description) || "No Description Given")))));
      } })))));
  }
  static get watchers() { return {
    "invoiceType": ["onInvoiceType"]
  }; }
};

exports.floodteam_form_scope = FormScope;

//# sourceMappingURL=floodteam-form-scope.cjs.entry.js.map