import { r as registerInstance, d as createEvent, B as Build, h, H as Host } from './index-33e43734.js';
import { p as popoverController } from './overlays-97615cf7.js';
import { l as logic } from './logic-2eecfe1e.js';
import { s as searchServices, g as getServiceTypes } from './getServiceTypes-4c27ab0e.js';
import { s as state } from './store-b9af0ae0.js';
import './ionic-global-bd76bde2.js';
import './helpers-a9270f9e.js';
import './_commonjsHelpers-cca61ea8.js';
import './fuse.esm-80f40788.js';
import './index-91eb4513.js';
import './getCache-f7c123d8.js';
import './index-964da72a.js';

let FormScope = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.filter = {
      status: "bill",
      tab: null,
    };
    this.sort = null;
    this.servicesOnBill = [];
    this.scopeToServiceMap = {};
    this.sitesToServiceMap = {};
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findJob") {
      this.job = (_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;
      this.sitesToServiceMap = {};
      for (const jobService of ((_d = this.job) === null || _d === void 0 ? void 0 : _d.services) || []) {
        if (!this.servicesOnBill.includes((_e = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _e === void 0 ? void 0 : _e.id))
          this.servicesOnBill.push((_f = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _f === void 0 ? void 0 : _f.id);
        this.scopeToServiceMap[`${this.jobId}-${(_g = jobService === null || jobService === void 0 ? void 0 : jobService.service) === null || _g === void 0 ? void 0 : _g.id}`] =
          jobService;
      }
      for (const site of ((_h = this.job) === null || _h === void 0 ? void 0 : _h.sites) || []) {
        for (const siteService of (site === null || site === void 0 ? void 0 : site.services) || []) {
          if (!this.servicesOnBill.includes((_j = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _j === void 0 ? void 0 : _j.id))
            this.servicesOnBill.push((_k = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _k === void 0 ? void 0 : _k.id);
          if (!((_l = this.sitesToServiceMap) === null || _l === void 0 ? void 0 : _l[(_m = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _m === void 0 ? void 0 : _m.id]))
            this.sitesToServiceMap[(_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].push(site);
          this.scopeToServiceMap[`${site === null || site === void 0 ? void 0 : site.id}-${(_q = siteService === null || siteService === void 0 ? void 0 : siteService.service) === null || _q === void 0 ? void 0 : _q.id}`] =
            siteService;
        }
      }
      await this.getServices();
    }
    else if (["addScopeService", "editScopeService", "deleteScopeService"].includes((_r = event === null || event === void 0 ? void 0 : event.detail) === null || _r === void 0 ? void 0 : _r.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) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.popoverEl = await popoverController.create({
      component: "popover-site-service",
      componentProps: {
        scopeToServiceMap: this.scopeToServiceMap,
        service,
        job: this.job,
        jobId: this.jobId,
        sites: ((_a = this.job) === null || _a === void 0 ? void 0 : _a.sites) || [],
        currentExpanded,
      },
      cssClass: "popover-site-service",
    });
    return this.popoverEl.present(event);
  }
  async getServices() {
    this.services = await 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))
        return true;
      try {
        if (((_c = this.filter) === null || _c === void 0 ? void 0 : _c.status) === "smart" &&
          logic.apply(service === null || service === void 0 ? void 0 : service.smartScoping, { service, job: this === null || this === void 0 ? void 0 : this.job }))
          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))))
        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)))
        return true;
      return false;
    });
  }
  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;
    return (h(Host, null, 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(),
          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"), h("fireenjin-pagination", { approxItemHeight: 55, 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}`) }, 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")))));
      } })))));
  }
};

export { FormScope as floodteam_form_scope };

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