import { r as registerInstance, d as createEvent, B as Build, h } from './index-33e43734.js';
import { g as getInvoiceTypes } from './getInvoiceTypes-cf540142.js';
import { s as state } from './store-97abe726.js';
import './index-91eb4513.js';
import './getCache-f7c123d8.js';
import './_commonjsHelpers-4ed75ef8.js';
import './logic-88140d62.js';
import './index-964da72a.js';

function totalsForJob(job) {
  const totals = {
    amountDeducted: (job === null || job === void 0 ? void 0 : job.amountDeducted) || 0,
    amountOwed: (job === null || job === void 0 ? void 0 : job.amountOwed) || 0,
    amountPaid: (job === null || job === void 0 ? void 0 : job.amountPaid) || 0,
    general: 0,
    levels: {},
    sites: {},
  };
  for (const service of (job === null || job === void 0 ? void 0 : job.services) || []) {
    totals.general = totals.general + ((service === null || service === void 0 ? void 0 : service.total) || 0);
  }
  for (const site of (job === null || job === void 0 ? void 0 : job.sites) || []) {
    if (!totals.sites[site === null || site === void 0 ? void 0 : site.id])
      totals.sites[site.id] = 0;
    if (!totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0])
      totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] = 0;
    for (const siteService of (site === null || site === void 0 ? void 0 : site.services) || []) {
      totals.sites[site.id] = totals.sites[site.id] + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] =
        totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
    }
  }
  return totals;
}

const appScopeCss = "app-scope ion-infinite-scroll-content{display:none}";

let AppScope = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinSubmit = createEvent(this, "fireenjinSubmit", 7);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.floodteamOpenModal = createEvent(this, "floodteamOpenModal", 7);
    this.floodteamOpenPopover = createEvent(this, "floodteamOpenPopover", 7);
    this.invoiceTypes = getInvoiceTypes();
    this.invoiceType = "remediation";
    this.attachments = [];
  }
  async fetchJob() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      params: {
        id: this.jobId,
        withPayments: false,
        withUsers: true,
        withSites: true,
        withPayModel: true,
        withPhotos: true,
        withEfiles: true,
        withPayables: false,
        withReadings: true,
      },
    });
  }
  async openSendEfileModal(event) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    this.floodteamOpenModal.emit({
      component: "modal-send-email",
      cssClass: "modal-send-email",
      componentProps: {
        job: this.job,
        templateFilter: (template) => { var _a, _b; return !(template === null || template === void 0 ? void 0 : template.partial) && ((_b = (_a = template === null || template === void 0 ? void 0 : template.tags) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, "job")); },
        attachments: this.attachments,
        users: ((_a = this.job) === null || _a === void 0 ? void 0 : _a.users) || [],
      },
    });
  }
  async sendEmail(event, userId) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    if (!((_a = state === null || state === void 0 ? void 0 : state.session) === null || _a === void 0 ? void 0 : _a.uid))
      return;
    this.fireenjinSubmit.emit({
      endpoint: "sendTemplateEmail",
      data: {
        templateId: "0xtJNtGRrT3GIosbN7iP",
        userIds: [userId],
        data: {
          app: {
            url: window.location.origin ,
          },
          job: this.job,
        },
      },
    });
  }
  async generatePdf(templateId, filename = null) {
    this.fireenjinSubmit.emit({
      endpoint: "generatePdf",
      data: {
        filename,
        jobId: this.jobId,
        templateId,
      },
    });
  }
  async send(event) {
    this.generatePdf("tA7FCnTudK0Sd887IeuU", "Invoice PDF");
    this.openSendEfileModal(event);
  }
  onSuccess(event) {
    var _a, _b, _c;
    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;
    }
  }
  onChange(event) {
    var _a, _b;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "type") {
      this.invoiceType = ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value) || null;
    }
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.fireenjinTrigger.emit({
      name: "subscribe",
      payload: {
        collection: "jobs",
        id: this.jobId,
        callback: () => {
          this.fetchJob();
        },
      },
    });
    this.fetchJob();
  }
  disconnectedCallback() {
    this.fireenjinTrigger.emit({
      name: "unsubscribe",
      payload: {
        collection: "jobs",
        id: this.jobId,
      },
    });
  }
  render() {
    var _a;
    return [
      h("ion-content", { class: "ion-padding" }, h("floodteam-title-bar", { back: `/jobs/${this.jobId}` }, h("ion-text", { onClick: (event) => this.floodteamOpenPopover.emit({
          event,
          component: "popover-invoice-select",
          componentProps: {
            value: this.invoiceType,
          },
        }), style: { display: "flex", alignItems: "end", cursor: "pointer" } }, ((_a = this.invoiceTypes.find((type) => (type === null || type === void 0 ? void 0 : type.value) === this.invoiceType)) === null || _a === void 0 ? void 0 : _a.label) || "Other", h("ion-icon", { name: "chevron-down" })), h("ion-buttons", { slot: "end" }, h("ion-button", { fill: "clear", onClick: (event) => this.send(event) }, "Send"), h("ion-button", { fill: "outline", onClick: () => this.floodteamOpenModal.emit({
          component: "modal-render-template",
          componentProps: {
            templateId: "tA7FCnTudK0Sd887IeuU",
            headerTitle: "Invoice",
            data: {
              job: this.job,
              totals: totalsForJob(this.job),
            },
          },
        }) }, "Preview"))), h("floodteam-form-scope", { invoiceType: this.invoiceType, job: this.job, jobId: this.jobId })),
      h("floodteam-navigation-footer", null),
    ];
  }
};
AppScope.style = appScopeCss;

export { AppScope as app_scope };

//# sourceMappingURL=app-scope.entry.js.map