import { Build, Component, Event, h, Listen, Prop, State, } from "@stencil/core";
import totalsForJob from "../../../helpers/totalsForJob";
import state from "../../../store";
export class AppScope {
  constructor() {
    this.invoiceType = "Water 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: {
        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: Build.isBrowser ? 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) || "Water Remediation";
    }
  }
  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() {
    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" } },
            this.invoiceType,
            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", { job: this.job, jobId: this.jobId })),
      h("floodteam-navigation-footer", null),
    ];
  }
  static get is() { return "app-scope"; }
  static get originalStyleUrls() { return {
    "$": ["app-scope.css"]
  }; }
  static get styleUrls() { return {
    "$": ["app-scope.css"]
  }; }
  static get properties() { return {
    "jobId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "job-id",
      "reflect": false
    },
    "invoiceType": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "| \"Water Remediation\"\n    | \"Box Out\"\n    | \"Contents\"\n    | \"Reconstruction\"\n    | \"Other\"",
        "resolved": "\"Box Out\" | \"Contents\" | \"Other\" | \"Reconstruction\" | \"Water Remediation\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "invoice-type",
      "reflect": false,
      "defaultValue": "\"Water Remediation\""
    }
  }; }
  static get states() { return {
    "attachments": {},
    "job": {}
  }; }
  static get events() { return [{
      "method": "fireenjinTrigger",
      "name": "fireenjinTrigger",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinTriggerInput",
        "resolved": "FireEnjinTriggerInput",
        "references": {
          "FireEnjinTriggerInput": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "method": "fireenjinSubmit",
      "name": "fireenjinSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinSubmitEvent",
        "resolved": "FireEnjinSubmitEvent",
        "references": {
          "FireEnjinSubmitEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "method": "fireenjinFetch",
      "name": "fireenjinFetch",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "floodteamOpenModal",
      "name": "floodteamOpenModal",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{\n    component: string;\n    componentProps?: any;\n    cssClass?: string;\n  }",
        "resolved": "{ component: string; componentProps?: any; cssClass?: string; }",
        "references": {}
      }
    }, {
      "method": "floodteamOpenPopover",
      "name": "floodteamOpenPopover",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{\n    component: string;\n    componentProps?: any;\n    cssClass?: string;\n    event?: any;\n  }",
        "resolved": "{ component: string; componentProps?: any; cssClass?: string; event?: any; }",
        "references": {}
      }
    }]; }
  static get listeners() { return [{
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": "document",
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamSelect",
      "method": "onChange",
      "target": "document",
      "capture": false,
      "passive": false
    }, {
      "name": "ionRouteDidChange",
      "method": "componentDidLoad",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=app-scope.js.map