'use strict';

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

const index = require('./index-fd2bbf0b.js');
const fuse_esm = require('./fuse.esm-847a739f.js');
const store = require('./store-03756ad8.js');
require('./index-81aa1bcb.js');
require('./getCache-c039a6ae.js');
require('./_commonjsHelpers-a5111d61.js');
require('./logic-d8c34975.js');
require('./index-6307644b.js');

const getTemplateTagOptions = [
  {
    label: "Imported",
    value: "imported",
  },
  {
    label: "Job",
    value: "job",
  },
  {
    label: "Location",
    value: "location",
  },
  {
    label: "Required",
    value: "required",
  },
  {
    label: "User",
    value: "user",
  },
  {
    label: "Deleted",
    value: "deleted",
  },
];

const getTemplateTypeOptions = [
  {
    label: "Component",
    value: "component",
  },
  {
    label: "Email",
    value: "email",
  },
  {
    label: "Contract",
    value: "contract",
  },
  {
    label: "Frame",
    value: "frame",
  },
  {
    label: "PDF",
    value: "pdf",
  },
  {
    label: "Feed Card",
    value: "feed",
  },
  {
    label: "Message",
    value: "message",
  },
  {
    label: "Web Page",
    value: "page",
  },
];

const appTemplateListCss = "";

let AppTemplateList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinSubmit = index.createEvent(this, "fireenjinSubmit", 7);
    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 = store.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_esm.Fuse(store.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 = ((store.state === null || store.state === void 0 ? void 0 : store.state.locations) || []).find((location) => { var _a, _b; return location.id === ((_b = (_a = store.state === null || store.state === void 0 ? void 0 : store.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 = store.state === null || store.state === void 0 ? void 0 : store.state.filters) === null || _a === void 0 ? void 0 : _a[location === null || location === void 0 ? void 0 : location.pathname];
    const templates = (this.searchResults || store.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 [
      index.h("ion-content", { class: "ion-padding" }, index.h("floodteam-title-bar", null, index.h("ion-router-link", { href: "/templates" }, this.contracts ? "Contracts" : "Templates"), !this.contracts && (index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { fill: "solid", color: "primary", href: "/templates/new" }, "Add")))), index.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,
          },
        ] }), index.h("ion-card", { class: {
          "is-loading": this.loading,
        } }, index.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$1) => {
        var _a, _b, _c;
        return [
          !this.contracts &&
            ((_a = templates === null || templates === void 0 ? void 0 : templates[index$1 - 1]) === null || _a === void 0 ? void 0 : _a.type) !== (template === null || template === void 0 ? void 0 : template.type) && (index.h("ion-item-divider", null, template.type)),
          (!this.contracts ||
            (this.contracts && (template === null || template === void 0 ? void 0 : template.type) === "contract")) && (index.h("ion-item", { href: this.contracts ? "#" : `/templates/${template === null || template === void 0 ? void 0 : template.id}`, detail: !this.contracts }, index.h("ion-label", null, index.h("h2", null, (template === null || template === void 0 ? void 0 : template.subject) || ""), index.h("p", null, (template === null || template === void 0 ? void 0 : template.description) || "No Description on File..."), index.h("div", null, ((template === null || template === void 0 ? void 0 : template.tags) || []).map((tag) => (index.h("ion-badge", { style: {
              margin: "0 3px",
            } }, tag))))), index.h("ion-buttons", { slot: "end" }, ((_b = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _b === void 0 ? void 0 : _b.admin) && (index.h("ion-button", { onClick: (event) => this.delete(event, template === null || template === void 0 ? void 0 : template.id) }, index.h("ion-icon", { name: "trash", slot: "icon-only" }))), index.h("ion-button", { onClick: (event) => this.print(event, template === null || template === void 0 ? void 0 : template.id) }, index.h("ion-icon", { name: "print", slot: "icon-only" })), ((_c = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _c === void 0 ? void 0 : _c.admin) && (index.h("ion-button", { onClick: (event) => this.duplicate(event, template) }, index.h("ion-icon", { name: "copy", slot: "icon-only" })))))),
        ];
      })))),
      index.h("floodteam-navigation-footer", null),
    ];
  }
};
AppTemplateList.style = appTemplateListCss;

exports.app_template_list = AppTemplateList;

//# sourceMappingURL=app-template-list.cjs.entry.js.map