import { r as registerInstance, d as createEvent, B as Build, h, e as getElement } from './index-33e43734.js';
import { b as __decorate } from './tslib.es6-78d2ab52.js';
import { s as state } from './store-97abe726.js';
import { i as index_min } from './index.min-fdd763e8.js';
import './index-91eb4513.js';
import './getCache-f7c123d8.js';
import './_commonjsHelpers-4ed75ef8.js';
import './logic-88140d62.js';
import './index-964da72a.js';

const appTemplateCss = "app-template .tabs-bar{background-color:var(--ion-background-color, #fff) !important}app-template floodteam-floating-button ion-fab{margin-top:20px}app-template .ce-toolbar{background-color:var(--ion-background-color)}app-template .ce-toolbox{background:var(--ion-background-color) !important}app-template ion-item{--background:transparent}app-template .gjs-editor-cont{top:50px}app-template .sample-editor{display:block;height:300px;position:relative;--min-height:300px}app-template ion-item.sample-editor ion-label{position:absolute;top:0;left:0;color:var(--ion-color-medium-shade) !important;font-size:0.75em !important;font-weight:bold;text-transform:uppercase;font-family:arial;display:block;background:transparent;text-align:left;padding:0 0 8px 0;font-family:var(--ion-font-family)}app-template .preview-wrapper{margin:30px auto}app-template fireenjin-mx-editor fireenjin-render-template#preview{position:absolute;top:0;bottom:0 !important;left:0;right:0;height:100%}app-template fireenjin-editor{padding-left:0 !important;--editor-background:var(--ion-color-base);--editor-color:var(--ion-text-color);--editor-color-shadow:var(--ion-color-base-dark);--editor-color-punctuation:rgba(var(--ion-color-secondary-rgb), 0.6);--editor-color-property:var(--ion-color-primary);--ion-color-light-shade:var(--ion-color-base);--ion-color-light-tint:var(--ion-color-base-dark);--ion-background-color:transparent}app-template .pdf-wrapper{background-color:#fff;--ion-text-color:#000}app-template fireenjin-render-template{background-color:#fff;--ion-text-color:#000;color:var(--ion-text-color)}";

let AppTemplate = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 7);
    this.isLoading = false;
    this.viewCode = false;
  }
  onTrigger(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) === "linkTemplate") {
      window.open(`${((_b = window.location) === null || _b === void 0 ? void 0 : _b.origin) || "."}/templates/${(_e = (_d = (_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.payload) === null || _d === void 0 ? void 0 : _d.template) === null || _e === void 0 ? void 0 : _e.id}`, "_blank");
    }
  }
  onTemplateChange() {
    setTimeout(async () => {
      var _a;
      console.log("template change");
      this.previewHtml = await this.editorEl.generatePreview();
      this.template = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.template;
    }, 500);
  }
  onSuccess(event) {
    var _a, _b, _c, _d;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "addTemplate" &&
      this.templateId === "new") {
      const routerEl = document.querySelector("ion-router");
      this.templateId = ((_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.addTemplate) === null || _d === void 0 ? void 0 : _d.id) || "new";
      if (routerEl)
        routerEl.push(`/templates/${this.templateId}`);
    }
  }
  async sync(template) {
    if (!(template === null || template === void 0 ? void 0 : template.id))
      return;
    let response;
    try {
      response = await fetch(`https://us-central1-${state.config.env === "live" ? "the-flood-team" : "ftms-ca85b"}.cloudfunctions.net/api/template/${template.id}`, {
        method: "POST",
        body: JSON.stringify(template),
      });
    }
    catch (e) {
      console.log("Error syncing template to live site!");
    }
    return (response === null || response === void 0 ? void 0 : response.json) ? response.json() : null;
  }
  async openPdfPreview() {
    var _a;
    if (!((_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.save))
      return;
    await this.editorEl.save();
    await this.editorEl.generatePdf();
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.fireenjinTrigger.emit({
      name: "subscribe",
      payload: {
        collection: "templates",
        id: this.templateId,
        callback: () => {
          this.editorEl.fetchData();
        },
      },
    });
  }
  disconnectedCallback() {
    this.fireenjinTrigger.emit({
      name: "unsubscribe",
      payload: {
        collection: "templates",
        id: this.templateId,
      },
    });
  }
  render() {
    var _a, _b, _c, _d, _e;
    const template = ((state === null || state === void 0 ? void 0 : state.templates) || []).find((template) => (template === null || template === void 0 ? void 0 : template.id) === this.templateId);
    const pin = {
      href: `/templates/${this.templateId}`,
      name: (template === null || template === void 0 ? void 0 : template.subject) || "Template",
      icon: "document",
    };
    return [
      h("ion-content", null, h("fireenjin-mx-editor", { class: {
          "is-on-canvas": ["contract", "pdf", "email"].includes(template === null || template === void 0 ? void 0 : template.type),
        }, colors: state.colors, ref: (el) => (this.editorEl = el), templates: state === null || state === void 0 ? void 0 : state.templates, templateId: this.templateId === "new" ? null : this.templateId, helpers: state === null || state === void 0 ? void 0 : state.helpers, fetch: true })),
      h("fireenjin-render-template", { style: {
          display: ((_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.isPreviewing) ? "none" : "block",
          position: "absolute",
          minHeight: "216px",
          minWidth: "384px",
          maxHeight: "216px",
          maxWidth: "384px",
          bottom: "50px",
          right: "18px",
        }, zoom: "0.2", partials: ((state === null || state === void 0 ? void 0 : state.templates) || []), template: this.template, helpers: state === null || state === void 0 ? void 0 : state.helpers, allowFullscreen: true, enableClicks: true }),
      h("floodteam-navigation-footer", null, h("fireenjin-chip-bar", null, h("ion-router-link", { href: "/templates" }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "arrow-back-circle" }), h("ion-label", null, "Back"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.save) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "save" }), h("ion-label", null, "Save"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.togglePreview) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: !!((_b = this.editorEl) === null || _b === void 0 ? void 0 : _b.isPreviewing) ? "eye-off" : "eye" }), h("ion-label", null, "Preview"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openTemplateInfoModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "information-circle" }), h("ion-label", null, ((_c = this.template) === null || _c === void 0 ? void 0 : _c.name) || ((_d = this.template) === null || _d === void 0 ? void 0 : _d.subject) || "Info"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openLayoutModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "scan" }), h("ion-label", null, "Layout"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openSampleDataModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "document-text" }), h("ion-label", null, "Sample Data"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openHeadModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "search-circle" }), h("ion-label", null, "Edit Head"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openRoutesModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "globe" }), h("ion-label", null, "Edit Routes"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openCodeModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "code-slash" }), h("ion-label", null, "Code"))), h("ion-router-link", { onClick: () => { var _a, _b; return (_b = (_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.openSendEmailModal) === null || _b === void 0 ? void 0 : _b.call(_a); } }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "mail" }), h("ion-label", null, "Email"))), h("ion-router-link", { onClick: () => this.openPdfPreview() }, h("ion-chip", { color: "light" }, h("ion-icon", { class: "ft-icon-style", name: "document" }), h("ion-label", null, "PDF"))), h("ion-router-link", { onClick: () => this.fireenjinTrigger.emit({
          name: "pin",
          payload: pin,
        }) }, h("ion-chip", null, h("ion-icon", { name: "pin", class: "ft-icon-style" }), h("ion-label", null, ((_e = ((state === null || state === void 0 ? void 0 : state.pins) || []).map((p) => p === null || p === void 0 ? void 0 : p.href)) === null || _e === void 0 ? void 0 : _e.includes(pin === null || pin === void 0 ? void 0 : pin.href))
        ? "Unpin"
        : "Pin"))))),
    ];
  }
  get appTemplateEl() { return getElement(this); }
};
__decorate([
  index_min.debounce(2000)
], AppTemplate.prototype, "onTemplateChange", null);
AppTemplate.style = appTemplateCss;

export { AppTemplate as app_template };

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