'use strict';

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

const index = require('./index-fd2bbf0b.js');
const store = require('./store-9988299d.js');
require('./index-81aa1bcb.js');
require('./getCache-901b0f67.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 site of job.sites || []) {
      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) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.floodteamOpenModal = index.createEvent(this, "floodteamOpenModal", 7);
  }
  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 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() {
    store.state.job = null;
    store.state.site = null;
    this.fireenjinTrigger.emit({
      name: "unsubscribe",
      payload: {
        collection: "jobs",
        id: this.jobId,
      },
    });
  }
  render() {
    return [
      index.h("ion-content", { class: "ion-padding" }, index.h("floodteam-title-bar", { back: `/jobs/${this.jobId}` }, index.h("ion-text", null, "Scope"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { fill: "outline", size: "small", onClick: () => this.floodteamOpenModal.emit({
          component: "modal-render-template",
          componentProps: {
            templateId: "tA7FCnTudK0Sd887IeuU",
            headerTitle: "Invoice",
            data: {
              job: store.state.job,
              totals: totalsForJob(store.state.job),
            },
          },
        }) }, "Preview Invoice"))), index.h("floodteam-form-scope", { jobId: this.jobId })),
      index.h("floodteam-navigation-footer", null),
    ];
  }
};
AppScope.style = appScopeCss;

exports.app_scope = AppScope;

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