import { popoverController } from "@ionic/core";
import { Build, Component, Event, h, Listen, Prop, State, } from "@stencil/core";
import getInvoiceTypes from "../../../helpers/getInvoiceTypes";
import totalsForJob from "../../../helpers/totalsForJob";
import state from "../../../store";
export class AppScope {
  constructor() {
    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: Build.isBrowser ? 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),
    ];
  }
  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": "InvoiceType",
        "resolved": "\"content\" | \"drain\" | \"mitigation\" | \"mold\" | \"reconstruction\"",
        "references": {
          "InvoiceType": {
            "location": "import",
            "path": "../../../interfaces"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "invoice-type",
      "reflect": false,
      "defaultValue": "\"mitigation\""
    }
  }; }
  static get states() { return {
    "attachments": {}
  }; }
  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": "FireEnjinFetchEvent",
        "resolved": "FireEnjinFetchEvent",
        "references": {
          "FireEnjinFetchEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "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": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamSelect",
      "method": "onSelect",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "floodteamSelect",
      "method": "onChange",
      "target": "document",
      "capture": false,
      "passive": false
    }, {
      "name": "ionRouteDidChange",
      "method": "componentDidLoad",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
