'use strict';

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

const index = require('./index-fd2bbf0b.js');
const getInvoiceTypes = require('./getInvoiceTypes-1377950b.js');
const store = require('./store-d6f21087.js');
require('./index-81aa1bcb.js');
require('./getHandlebarsHelpers-7b2b1728.js');
require('./_commonjsHelpers-a5111d61.js');
require('./logic-d8c34975.js');
require('./index-1f381af8.js');
require('./localforage-af1b8c0e.js');

function totalsForJob(job) {
  const totals = {
    amountDeducted: (job === null || job === void 0 ? void 0 : job.amountDeducted) || 0,
    amountOwed: 0,
    amountBilled: 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);
    totals.amountOwed = totals.amountOwed + ((service === null || service === void 0 ? void 0 : service.total) || 0);
    totals.amountBilled = totals.amountBilled + ((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);
      totals.amountOwed = totals.amountOwed + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.amountBilled = totals.amountBilled + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
    }
  }
  for (const payment of (job === null || job === void 0 ? void 0 : job.payments) || []) {
    totals.amountPaid = totals.amountPaid + ((payment === null || payment === void 0 ? void 0 : payment.amount) || 0);
  }
  totals.amountOwed = ((totals === null || totals === void 0 ? void 0 : totals.amountBilled) || 0) - ((totals === null || totals === void 0 ? void 0 : totals.amountPaid) || 0);
  return totals;
}

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

let AppScope = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinSubmit = index.createEvent(this, "fireenjinSubmit", 7);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.floodteamOpenModal = index.createEvent(this, "floodteamOpenModal", 7);
    this.floodteamOpenPopover = index.createEvent(this, "floodteamOpenPopover", 7);
    this.invoiceTypes = getInvoiceTypes.getInvoiceTypes();
    this.invoiceType = "mitigation";
    this.attachments = [];
  }
  async fetchJob() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      cacheKey: "jobs",
      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: store.state.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 = store.state.job) === null || _a === void 0 ? void 0 : _a.users) || [],
      },
    });
  }
  async sendEmail(event, userId) {
    var _a;
    event.preventDefault();
    event.stopPropagation();
    if (!((_a = store.state === null || store.state === void 0 ? void 0 : store.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: store.state.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);
  }
  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 (!(index.Build === null || index.Build === void 0 ? void 0 : index.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 [
      index.h("ion-content", { class: "ion-padding" }, index.h("floodteam-title-bar", { back: `/jobs/${this.jobId}` }, index.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", index.h("ion-icon", { name: "chevron-down" })), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { fill: "clear", onClick: (event) => this.send(event) }, "Send"), index.h("ion-button", { fill: "outline", onClick: () => this.floodteamOpenModal.emit({
          component: "modal-render-template",
          componentProps: {
            templateId: "tA7FCnTudK0Sd887IeuU",
            headerTitle: "Invoice",
            data: {
              job: store.state.job,
              totals: totalsForJob(store.state.job),
            },
          },
        }) }, "Preview"))), index.h("floodteam-form-scope", { invoiceType: this.invoiceType, jobId: this.jobId })),
      index.h("floodteam-navigation-footer", null),
    ];
  }
};
AppScope.style = appScopeCss;

exports.app_scope = AppScope;

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