import { Component, Event, Listen, Prop, State, h, } from "@stencil/core";
import Fuse from "fuse.js";
import getTemplateTagOptions from "../../../helpers/getTemplateTagOptions";
import getTemplateTypeOptions from "../../../helpers/getTemplateTypeOptions";
import state from "../../../store";
export class AppTemplateList {
  constructor() {
    this.loading = false;
  }
  onSuccess(event) {
    var _a, _b, _c, _d;
    if (((_a = event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "generatePdf") {
      this.loading = false;
      open((_d = (_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.generatePdf) === null || _d === void 0 ? void 0 : _d.url, "_system");
    }
  }
  onError(event) {
    var _a;
    if (((_a = event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "generatePdf") {
      this.loading = false;
    }
  }
  async search(event) {
    var _a, _b;
    const query = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value;
    if (!((_b = state.templates) === null || _b === void 0 ? void 0 : _b.length) || !(query === null || query === void 0 ? void 0 : query.length)) {
      this.searchResults = null;
      return;
    }
    const fuse = new Fuse(state.templates, {
      keys: ["subject", "description"],
    });
    this.searchResults = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  async cancel() {
    this.searchResults = null;
  }
  async duplicate(event, template) {
    var _a, _b, _c;
    event.stopPropagation();
    event.preventDefault();
    const data = {
      editor: (template === null || template === void 0 ? void 0 : template.editor) || {
        time: new Date().getTime(),
        blocks: [],
        version: "2.8.1",
      },
      amp: (template === null || template === void 0 ? void 0 : template.amp) || null,
      attachments: (template === null || template === void 0 ? void 0 : template.attachments) || [],
      description: (template === null || template === void 0 ? void 0 : template.description) || null,
      footerId: ((_a = template === null || template === void 0 ? void 0 : template.footer) === null || _a === void 0 ? void 0 : _a.id) || null,
      headerId: ((_b = template === null || template === void 0 ? void 0 : template.header) === null || _b === void 0 ? void 0 : _b.id) || null,
      head: (template === null || template === void 0 ? void 0 : template.head) || null,
      frameId: ((_c = template === null || template === void 0 ? void 0 : template.frame) === null || _c === void 0 ? void 0 : _c.id) || null,
      name: (template === null || template === void 0 ? void 0 : template.name) || null,
      partial: (template === null || template === void 0 ? void 0 : template.partial) || false,
      routes: (template === null || template === void 0 ? void 0 : template.routes) || null,
      sampleData: (template === null || template === void 0 ? void 0 : template.sampleData) || null,
      subject: (template === null || template === void 0 ? void 0 : template.subject) || null,
      tags: (template === null || template === void 0 ? void 0 : template.tags) || [],
      text: (template === null || template === void 0 ? void 0 : template.text) || null,
      type: (template === null || template === void 0 ? void 0 : template.type) || null,
    };
    this.fireenjinSubmit.emit({
      endpoint: "addTemplate",
      params: {
        data,
      },
    });
  }
  async delete(event, id) {
    event.stopPropagation();
    event.preventDefault();
    if (!confirm("Are you sure you want to delete?"))
      return;
    this.fireenjinSubmit.emit({
      endpoint: "deleteTemplate",
      params: {
        id,
      },
    });
  }
  print(event, templateId) {
    this.loading = true;
    event.stopPropagation();
    event.preventDefault();
    const signatureLine = "_________________";
    const signatureShort = "_________";
    const address = {
      full: signatureLine,
      city: signatureLine,
      state: signatureShort,
      zip: signatureShort,
      country: signatureShort,
      street: signatureLine,
      unit: signatureShort,
    };
    const location = ((state === null || state === void 0 ? void 0 : state.locations) || []).find((location) => { var _a, _b; return location.id === ((_b = (_a = state === null || state === void 0 ? void 0 : state.profile) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.id); }) || {
      address,
      createdAt: signatureLine,
      updatedAt: signatureLine,
      name: signatureLine,
      dba: signatureLine,
      email: signatureLine,
      taxId: signatureLine,
      type: signatureLine,
    };
    this.fireenjinSubmit.emit({
      endpoint: "generatePdf",
      data: {
        templateId,
        payload: {
          job: {
            title: signatureLine,
            createdAt: signatureLine,
            updatedAt: signatureLine,
            address,
            amountBilled: signatureShort,
            amountDeducted: signatureShort,
            amountOwed: signatureShort,
            amountPaid: signatureShort,
            category: signatureShort,
            cause: signatureLine,
            claimNumber: signatureLine,
            customer: {
              firstName: signatureLine,
              lastName: signatureLine,
              createdAt: signatureLine,
              email: signatureLine,
              phone: signatureLine,
            },
            dateOfLoss: signatureLine,
            status: signatureLine,
            manager: {
              firstName: signatureLine,
              lastName: signatureLine,
              createdAt: signatureLine,
              email: signatureLine,
              phone: signatureLine,
            },
            propertyAge: signatureShort,
            phone: signatureLine,
            type: signatureLine,
            referral: {
              firstName: signatureLine,
              lastName: signatureLine,
              createdAt: signatureLine,
              email: signatureLine,
              phone: signatureLine,
            },
            location,
          },
        },
      },
    });
  }
  render() {
    var _a;
    const filter = (_a = state === null || state === void 0 ? void 0 : state.filters) === null || _a === void 0 ? void 0 : _a[location === null || location === void 0 ? void 0 : location.pathname];
    const templates = (this.searchResults || state.templates || [])
      .filter((template) => ((filter === null || filter === void 0 ? void 0 : filter.tags) || []).includes("deleted")
      ? true
      : !((filter === null || filter === void 0 ? void 0 : filter.tags) || []).includes("deleted") &&
        !((template === null || template === void 0 ? void 0 : template.tags) || []).includes("deleted"))
      .filter((template) => {
      var _a, _b, _c, _d, _e, _f, _g, _h;
      return (!((_a = filter === null || filter === void 0 ? void 0 : filter.types) === null || _a === void 0 ? void 0 : _a.length) && !((_b = filter === null || filter === void 0 ? void 0 : filter.tags) === null || _b === void 0 ? void 0 : _b.length)) ||
        ((!((_c = filter === null || filter === void 0 ? void 0 : filter.types) === null || _c === void 0 ? void 0 : _c.length) ||
          ((_e = (_d = filter === null || filter === void 0 ? void 0 : filter.types) === null || _d === void 0 ? void 0 : _d.includes) === null || _e === void 0 ? void 0 : _e.call(_d, template === null || template === void 0 ? void 0 : template.type))) &&
          (!((_f = filter === null || filter === void 0 ? void 0 : filter.tags) === null || _f === void 0 ? void 0 : _f.length) ||
            ((_h = (_g = filter === null || filter === void 0 ? void 0 : filter.tags) === null || _g === void 0 ? void 0 : _g.some) === null || _h === void 0 ? void 0 : _h.call(_g, (tag) => { var _a, _b; return (_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, tag); }))));
    });
    return [
      h("ion-content", { class: "ion-padding" },
        h("floodteam-title-bar", null,
          h("ion-router-link", { href: "/templates" }, this.contracts ? "Contracts" : "Templates"),
          !this.contracts && (h("ion-buttons", { slot: "end" },
            h("ion-button", { fill: "solid", color: "primary", href: "/templates/new" }, "Add")))),
        h("fireenjin-search-bar", { ref: (el) => (this.filterBarEl = el), paginationEl: this.paginationEl, filters: [
            {
              name: "types",
              label: "Types",
              icon: "create",
              multiple: true,
              options: getTemplateTypeOptions,
              value: filter === null || filter === void 0 ? void 0 : filter.types,
            },
            {
              name: "tags",
              label: "Tags",
              icon: "pricetags",
              multiple: true,
              options: getTemplateTagOptions,
              value: filter === null || filter === void 0 ? void 0 : filter.tags,
            },
          ] }),
        h("ion-card", { class: {
            "is-loading": this.loading,
          } },
          h("ion-list", { class: "ion-no-padding" }, templates
            .sort(function (a, b) {
            var _a, _b, _c, _d;
            var subjectA = ((_b = (_a = a === null || a === void 0 ? void 0 : a.subject) === null || _a === void 0 ? void 0 : _a.toUpperCase) === null || _b === void 0 ? void 0 : _b.call(_a)) || "";
            var subjectB = ((_d = (_c = b === null || b === void 0 ? void 0 : b.subject) === null || _c === void 0 ? void 0 : _c.toUpperCase) === null || _d === void 0 ? void 0 : _d.call(_c)) || "";
            if (subjectA < subjectB) {
              return -1;
            }
            if (subjectA > subjectB) {
              return 1;
            }
            // names must be equal
            return 0;
          })
            .sort(function (a, b) {
            var _a, _b, _c, _d;
            var typeA = ((_b = (_a = a === null || a === void 0 ? void 0 : a.type) === null || _a === void 0 ? void 0 : _a.toUpperCase) === null || _b === void 0 ? void 0 : _b.call(_a)) || "";
            var typeB = ((_d = (_c = b === null || b === void 0 ? void 0 : b.type) === null || _c === void 0 ? void 0 : _c.toUpperCase) === null || _d === void 0 ? void 0 : _d.call(_c)) || "";
            if (typeA < typeB) {
              return -1;
            }
            if (typeA > typeB) {
              return 1;
            }
            // names must be equal
            return 0;
          })
            .map((template, index) => {
            var _a, _b, _c;
            return [
              !this.contracts &&
                ((_a = templates === null || templates === void 0 ? void 0 : templates[index - 1]) === null || _a === void 0 ? void 0 : _a.type) !== (template === null || template === void 0 ? void 0 : template.type) && (h("ion-item-divider", null, template.type)),
              (!this.contracts ||
                (this.contracts && (template === null || template === void 0 ? void 0 : template.type) === "contract")) && (h("ion-item", { href: this.contracts ? "#" : `/templates/${template === null || template === void 0 ? void 0 : template.id}`, detail: !this.contracts },
                h("ion-label", null,
                  h("h2", null, (template === null || template === void 0 ? void 0 : template.subject) || ""),
                  h("p", null, (template === null || template === void 0 ? void 0 : template.description) || "No Description on File..."),
                  h("div", null, ((template === null || template === void 0 ? void 0 : template.tags) || []).map((tag) => (h("ion-badge", { style: {
                      margin: "0 3px",
                    } }, tag))))),
                h("ion-buttons", { slot: "end" },
                  ((_b = state === null || state === void 0 ? void 0 : state.claims) === null || _b === void 0 ? void 0 : _b.admin) && (h("ion-button", { onClick: (event) => this.delete(event, template === null || template === void 0 ? void 0 : template.id) },
                    h("ion-icon", { name: "trash", slot: "icon-only" }))),
                  h("ion-button", { onClick: (event) => this.print(event, template === null || template === void 0 ? void 0 : template.id) },
                    h("ion-icon", { name: "print", slot: "icon-only" })),
                  ((_c = state === null || state === void 0 ? void 0 : state.claims) === null || _c === void 0 ? void 0 : _c.admin) && (h("ion-button", { onClick: (event) => this.duplicate(event, template) },
                    h("ion-icon", { name: "copy", slot: "icon-only" })))))),
            ];
          })))),
      h("floodteam-navigation-footer", null),
    ];
  }
  static get is() { return "app-template-list"; }
  static get originalStyleUrls() { return {
    "$": ["app-template-list.css"]
  }; }
  static get styleUrls() { return {
    "$": ["app-template-list.css"]
  }; }
  static get properties() { return {
    "contracts": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "contracts",
      "reflect": false
    }
  }; }
  static get states() { return {
    "searchResults": {},
    "paginationEl": {},
    "loading": {}
  }; }
  static get events() { return [{
      "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"
          }
        }
      }
    }]; }
  static get listeners() { return [{
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "fireenjinError",
      "method": "onError",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "ionChange",
      "method": "search",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "ionCancel",
      "method": "cancel",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=app-template-list.js.map