import { r as registerInstance, d as createEvent, B as Build, h } from './index-26447f8a.js';
import { p as popoverController } from './overlays-a3190093.js';
import { g as getInvoiceTypes } from './getInvoiceTypes-8686f775.js';
import { s as state } from './store-838a3050.js';
import './ionic-global-b53b5faa.js';
import './helpers-06537f39.js';
import './index-b0d3ac50.js';
import './getHandlebarsHelpers-0376b57c.js';
import './_commonjsHelpers-7c4ea794.js';
import './index-e6b3332e.js';
import './localforage-c63d55c8.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 =
      parseFloat(totals.general) +
        parseFloat(((service === null || service === void 0 ? void 0 : service.total) || 0));
    totals.amountOwed =
      parseFloat(totals.amountOwed) +
        parseFloat(((service === null || service === void 0 ? void 0 : service.total) || 0));
    totals.amountBilled =
      parseFloat(totals.amountBilled) +
        parseFloat(((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] =
        parseFloat(totals.sites[site.id]) +
          parseFloat(((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0));
      totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] =
        parseFloat(totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0]) +
          parseFloat(((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0));
      totals.amountOwed =
        parseFloat(totals.amountOwed) + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.amountBilled =
        parseFloat(totals.amountBilled) +
          parseFloat(((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 =
      parseFloat(totals.amountPaid) +
        parseFloat(((payment === null || payment === void 0 ? void 0 : payment.amount) || 0));
  }
  totals.amountOwed =
    parseFloat(((totals === null || totals === void 0 ? void 0 : totals.amountBilled) || 0)) -
      parseFloat(((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.invoiceTypesLookup = {
      water: "Water Clean-up & Drying",
      mold: "Mold Remediation",
      content: "Box Out / Contents",
      reconstruction: "Reconstruction",
      drain: "Drain Cleaning",
    };
    this.jobId = undefined;
    this.invoiceType = "water";
    this.attachments = [];
  }
  onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g;
    console.log("Scope Success", event.detail);
    if (event.detail.endpoint === "generatePdf") {
      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();
    }
    else if (event.detail.endpoint === "findJob") {
      this.invoiceType = ((_g = (_f = (_e = (_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.job) === null || _f === void 0 ? void 0 : _f.serviceTypes) === null || _g === void 0 ? void 0 : _g[0]) || "water";
    }
  }
  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,
        invoiceType: this.invoiceType,
      },
    });
  }
  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: "beLtN4HFStll5UgL9NaZ",
            headerTitle: "Invoice",
            data: {
              job: state.job,
              totals: totalsForJob(state.job),
              invoice: {
                type: this.invoiceType,
                typePretty: this.invoiceTypesLookup[this.invoiceType || "water"],
              },
            },
          },
        }) }, "Preview"))), h("floodteam-form-scope", { invoiceType: this.invoiceType, jobId: this.jobId })),
      h("floodteam-navigation-footer", null),
    ];
  }
};
AppScope.style = appScopeCss;

export { AppScope as app_scope };
