import { r as registerInstance, d as createEvent, B as Build, h } from './index-000d2977.js';
import { p as popoverController } from './overlays-1080973b.js';
import { g as getInvoiceTypes } from './getInvoiceTypes-d6baa75e.js';
import { s as state } from './store-2624d5cd.js';
import './ionic-global-8913b6b6.js';
import './helpers-7e937ffa.js';
import './index-ac0c2732.js';
import './getHandlebarsHelpers-1f444a62.js';
import './_commonjsHelpers-44457d8d.js';
import './index-d4e79b88.js';
import './localforage-394b59fa.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}";

const 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 = "mitigation";
    this.attachments = [];
  }
  onSuccess(event) {
    var _a, _b, _c;
    if (event.detail.endpoint === "generatePdf") {
      console.log(event.detail);
      this.attachments = [(_c = (_b = (_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.generatePdf) === null || _c === void 0 ? void 0 : _c.url];
      this.openSendEfileModal();
    }
  }
  onSelect(event) {
    var _a, _b, _c, _d, _e;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) === "user" && ((_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.user) === null || _c === void 0 ? void 0 : _c.id)) {
      this.sendEmail(event, (_e = (_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.user) === null || _e === void 0 ? void 0 : _e.id);
      this.popoverEl.dismiss();
    }
  }
  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 openUserSelect(event) {
    var _a;
    this.popoverEl = await popoverController.create({
      event,
      cssClass: "popover-user-select",
      component: "popover-user-select",
      componentProps: {
        jobId: this.jobId,
        users: ((_a = state === null || state === void 0 ? void 0 : state.job) === null || _a === void 0 ? void 0 : _a.users) || [],
      },
    });
    this.popoverEl.present();
  }
  async openSendEfileModal() {
    var _a;
    this.floodteamOpenModal.emit({
      component: "modal-send-email",
      cssClass: "modal-send-email",
      componentProps: {
        templateId: "0xtJNtGRrT3GIosbN7iP",
        job: 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 = state.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: state.job,
        },
      },
    });
  }
  async generatePdf(templateId, filename = null) {
    this.fireenjinSubmit.emit({
      endpoint: "generatePdf",
      data: {
        filename,
        jobId: this.jobId,
        templateId,
      },
    });
  }
  async send() {
    this.generatePdf("tA7FCnTudK0Sd887IeuU", "Invoice PDF");
  }
  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: () => this.send() }, "Send"), h("ion-button", { fill: "outline", onClick: () => this.floodteamOpenModal.emit({
          component: "modal-render-template",
          componentProps: {
            templateId: "tA7FCnTudK0Sd887IeuU",
            headerTitle: "Invoice",
            data: {
              job: state.job,
              totals: totalsForJob(state.job),
            },
          },
        }) }, "Preview"))), h("floodteam-form-scope", { invoiceType: this.invoiceType, jobId: this.jobId })),
      h("floodteam-navigation-footer", null),
    ];
  }
};
AppScope.style = appScopeCss;

export { AppScope as app_scope };
