import { r as registerInstance, d as createEvent, B as Build, h, H as Host } from './index-000d2977.js';
import { p as popoverController } from './overlays-675cb3f1.js';
import { l as logic } from './logic-88140d62.js';
import { s as searchServices, c as cleanAndLowerString, g as getServiceTypes } from './getServiceTypes-d44aae3b.js';
import { s as state } from './store-cfa3d67a.js';
import './ionic-global-8913b6b6.js';
import './helpers-7e937ffa.js';
import './_commonjsHelpers-4ed75ef8.js';
import './fuse.esm-80f40788.js';
import './getHandlebarsHelpers-91de4454.js';
import './index-d4e79b88.js';
import './localforage-9daa31d6.js';

const FormScope = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 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 = {};
      const servicesOnBill = [];
      for (const jobService of ((_e = state === null || state === void 0 ? void 0 : 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 = state === null || state === void 0 ? void 0 : 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] = [];
          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;
        }
      }
      this.servicesOnBill = servicesOnBill;
      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 = { ...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 openServicePopover(event, service, currentExpanded, site) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.popoverEl = await popoverController.create({
      component: "popover-site-service",
      componentProps: {
        scopeToServiceMap: this.scopeToServiceMap,
        service,
        jobId: this.jobId,
        site,
        sites: ((_a = state === null || state === void 0 ? void 0 : 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" &&
          logic.apply(service === null || service === void 0 ? void 0 : service.smartScoping, {
            service,
            job: state === null || state === void 0 ? void 0 : 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) ||
          ((_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, cleanAndLowerString(service === null || service === void 0 ? void 0 : service.tab)))))
        return true;
      if (!((_h = this.filter) === null || _h === void 0 ? void 0 : _h.status) &&
        ((_k = (_j = this.filter) === null || _j === void 0 ? void 0 : _j.tab) === null || _k === void 0 ? void 0 : _k.includes(cleanAndLowerString(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;
      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) ||
          ((_q = (_p = this.filter) === null || _p === void 0 ? void 0 : _p.tab) === null || _q === void 0 ? void 0 : _q.includes(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 (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.fetchData();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    const serviceTypes = getServiceTypes();
    return (h(Host, null, 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,
        },
      ] }), h("ion-card", null, h("ion-list", { class: "ion-no-padding" }, 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) && (h("ion-grid", null, h("ion-row", null, serviceTypes.map((tab) => (h("ion-col", { size: "6", sizeMd: "4", sizeLg: "3" }, h("ion-card", { href: "#" }, 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)))))))), h("fireenjin-pagination", { disableVirtualScroll: true, results: this.services, renderItem: (service) => {
        var _a, _b;
        return (h("ion-item", { href: "#", detail: true, detailIcon: "list-circle", onClick: (event) => this.openServicePopover(event, service) }, h("ion-label", { style: {
            marginTop: "3px",
            marginBottom: "3px",
          } }, 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) ? (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" && (h("ion-chip", { style: {
            height: "20px",
            paddingTop: "0px",
            paddingBottom: "0px",
          }, onClick: (event) => {
            var _a;
            return this.openServicePopover(event, service, `${(_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.id}-${service === null || service === void 0 ? void 0 : service.id}`);
          } }, 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) => (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) }, h("small", null, (site === null || site === void 0 ? void 0 : site.name) || "No Name")))))) : (h("p", null, (service === null || service === void 0 ? void 0 : service.description) || "No Description Given")))));
      } })))));
  }
  static get watchers() { return {
    "invoiceType": ["onInvoiceType"]
  }; }
};

export { FormScope as floodteam_form_scope };
