import { r as registerInstance, h } from './index-000d2977.js';
import { F as Fuse } from './fuse.esm-80f40788.js';
import { s as state } from './store-14d7760f.js';

const pageHomeCss = "";

const PageHome = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  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 === null || state === void 0 ? void 0 : 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 === null || state === void 0 ? void 0 : state.templates) || [], {
      keys: ["subject", "description"],
      fieldNormWeight: 1,
      sortFn: (a, b) => {
        var _a, _b, _c, _d;
        return ((_a = a === null || a === void 0 ? void 0 : a.item) === null || _a === void 0 ? void 0 : _a.type) > ((_b = b === null || b === void 0 ? void 0 : b.item) === null || _b === void 0 ? void 0 : _b.type)
          ? 1
          : ((_c = b === null || b === void 0 ? void 0 : b.item) === null || _c === void 0 ? void 0 : _c.type) > ((_d = a === null || a === void 0 ? void 0 : a.item) === null || _d === void 0 ? void 0 : _d.type)
            ? -1
            : 0;
      },
    });
    this.searchResults = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  async cancel() {
    this.searchResults = null;
  }
  render() {
    const templates = this.searchResults || (state === null || state === void 0 ? void 0 : state.templates) || [];
    return [
      h("ion-content", { class: "ion-padding" }, h("ion-grid", null, h("ion-row", null, h("ion-col", { size: "8" }, h("ion-searchbar", null)), h("ion-col", { size: "4" }, h("ion-button", { expand: "block", class: "ion-float-right", href: "/templates/new" }, "Add Template")))), h("ion-card", null, h("ion-list", { class: "ion-no-padding" }, templates.map((template, index) => {
        var _a;
        return [
          ((_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", { color: "primary" }, template.type)),
          h("ion-item", { href: `/templates/${template === null || template === void 0 ? void 0 : template.id}`, detail: true }, 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("app-footer", null),
    ];
  }
};
PageHome.style = pageHomeCss;

export { PageHome as fireenjin_mx_page_home };
