import { r as registerInstance, d as createEvent, B as Build, h, H as Host, e as getElement } from './index-33e43734.js';
import { b as __decorate } from './tslib.es6-78d2ab52.js';
import { r as ref, u as uploadBytes, a as getDownloadURL, g as getStorage } from './index.esm2017-f161ed27.js';
import { m as modalController, f as actionSheetController, t as toastController } from './overlays-97615cf7.js';
import { l as lib } from './index-15dbcbaf.js';
import { D as Debounce } from './index-afc43872.js';
import './index.esm2017-8e70fa12.js';
import './global-e1c7e609.js';
import './ionic-global-bd76bde2.js';
import './helpers-a9270f9e.js';
import './_commonjsHelpers-cca61ea8.js';

/**
 * Cleans all JavaScript referneces and prototypes from Object
 * so it can be stored in Firestore.
 * @param input The JS Object to clean
 * @param keepDocumentReferenceId Should we keep reference ids for relationships
 */
async function cleanObjectOfReferences(input, keepDocumentReferenceId = false) {
  var _a, _b, _c, _d, _e;
  const data = input;
  for (const key of Object.keys(input)) {
    const value = input[key];
    if (!value)
      continue;
    try {
      if (((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === "Object") {
        data[key] = await cleanObjectOfReferences(value);
      }
      else if (((_b = value === null || value === void 0 ? void 0 : value.constructor) === null || _b === void 0 ? void 0 : _b.name) === "DocumentReference") {
        keepDocumentReferenceId
          ? (data[key] = { id: value.id })
          : delete data[key];
      }
      else if (((_c = value === null || value === void 0 ? void 0 : value.constructor) === null || _c === void 0 ? void 0 : _c.name) === "Timestamp") {
        data[key] = value.toDate();
      }
      else if (((_d = value === null || value === void 0 ? void 0 : value.constructor) === null || _d === void 0 ? void 0 : _d.name) === "Array") {
        const cleanArray = [];
        for (const item of data[key]) {
          cleanArray.push(await cleanObjectOfReferences(item));
        }
        data[key] = cleanArray;
      }
      else if (typeof value === "object" &&
        ((_e = value === null || value === void 0 ? void 0 : value.constructor) === null || _e === void 0 ? void 0 : _e.name) !== "Date") {
        data[key] = await cleanObjectOfReferences(JSON.parse(JSON.stringify(value)));
      }
    }
    catch (err) {
      delete data[key];
    }
  }
  return JSON.parse(JSON.stringify(data));
}

const editorCss = "fireenjin-mx-editor,fireenjin-mx-editor fireenjin-editor{display:block;opacity:1;transition:all ease 0.4s;position:relative;width:100%;height:100%}fireenjin-mx-editor.is-previewing fireenjin-editor{opacity:0;pointer-events:none}fireenjin-mx-editor #preview{transition:all ease 0.4;opacity:0;position:absolute;top:0;left:0;right:0;display:block;width:100%;min-height:100%;pointer-events:none}fireenjin-mx-editor.is-previewing #preview{opacity:1;pointer-events:all}";

let AppTemplateForm = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 7);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.fireenjinSubmit = createEvent(this, "fireenjinSubmit", 7);
    this.name = "editor";
    this.toastPosition = "bottom";
    this.isPreviewing = false;
    this.partials = [];
    this.sizes = [50, 50];
    this.disableFetch = false;
    this.controls = [];
    this.templateData = {};
    this.tags = [];
    this.helpers = {
      formatUSD: (amount) => {
        const formatter = new Intl.NumberFormat("en-US", {
          style: "currency",
          currency: "USD",
          minimumFractionDigits: 2,
        });
        return formatter.format(amount ? amount : 0);
      },
    };
    this.currentPartials = [];
    this.currentHelpers = [];
  }
  onKeydown(event) {
    let charCode = String.fromCharCode(event.which).toLowerCase();
    if ((event.ctrlKey || event.metaKey) && charCode === "s") {
      this.save();
    }
  }
  onCodeChange(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "header") {
      this.template.header = ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value) || "";
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.name) === "footer") {
      this.template.footer = ((_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.value) || "";
    }
    else if (((_e = event === null || event === void 0 ? void 0 : event.target) === null || _e === void 0 ? void 0 : _e.name) === "head") {
      this.template.header = ((_f = event === null || event === void 0 ? void 0 : event.target) === null || _f === void 0 ? void 0 : _f.value) || "";
    }
    else if (((_g = event === null || event === void 0 ? void 0 : event.target) === null || _g === void 0 ? void 0 : _g.name) === "sampleData") {
      this.template.sampleData =
        (typeof ((_h = event === null || event === void 0 ? void 0 : event.target) === null || _h === void 0 ? void 0 : _h.value) === "string" &&
          JSON.stringify((_j = event === null || event === void 0 ? void 0 : event.target) === null || _j === void 0 ? void 0 : _j.value)) ||
          ((_k = event === null || event === void 0 ? void 0 : event.target) === null || _k === void 0 ? void 0 : _k.value) ||
          {};
    }
    else if (((_l = event === null || event === void 0 ? void 0 : event.target) === null || _l === void 0 ? void 0 : _l.name) === "routes") {
      this.template.routes =
        (typeof ((_m = event === null || event === void 0 ? void 0 : event.target) === null || _m === void 0 ? void 0 : _m.value) === "string" &&
          JSON.stringify((_o = event === null || event === void 0 ? void 0 : event.target) === null || _o === void 0 ? void 0 : _o.value)) ||
          ((_p = event === null || event === void 0 ? void 0 : event.target) === null || _p === void 0 ? void 0 : _p.value) ||
          {};
    }
  }
  async onEditorChange() {
    var _a;
    if (!((_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.exportHTML))
      return;
    this.generatePreview();
  }
  async onChange(event) {
    const listEl = event.target.closest("ion-card ion-list");
    if (listEl) {
      this.setProgress(listEl);
    }
  }
  onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findTemplate") {
      this.template = ((_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.template) || {};
      this.templateId = ((_d = this.template) === null || _d === void 0 ? void 0 : _d.id) || this.templateId;
      if (!((_e = this.template) === null || _e === void 0 ? void 0 : _e.editor))
        return;
      try {
        this.editorJs.blocks.render(((_f = this.template) === null || _f === void 0 ? void 0 : _f.editor) || {});
        this.generatePreview();
      }
      catch (error) {
        setTimeout(async () => {
          var _a;
          this.editorJs.blocks.render(((_a = this.template) === null || _a === void 0 ? void 0 : _a.editor) || {});
          this.generatePreview();
        }, 1000);
      }
    }
    else if (["addTemplate", "editTemplate"].includes((_g = event === null || event === void 0 ? void 0 : event.detail) === null || _g === void 0 ? void 0 : _g.endpoint)) {
      this.fetchData();
      if ((_h = this.modalEl) === null || _h === void 0 ? void 0 : _h.dismiss)
        this.modalEl.dismiss();
    }
    else if (((_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.endpoint) === "generatePdf") {
      if ((window === null || window === void 0 ? void 0 : window.open) &&
        ((_m = (_l = (_k = event.detail) === null || _k === void 0 ? void 0 : _k.data) === null || _l === void 0 ? void 0 : _l.generatePdf) === null || _m === void 0 ? void 0 : _m.url)) {
        window.open((_q = (_p = (_o = event.detail) === null || _o === void 0 ? void 0 : _o.data) === null || _p === void 0 ? void 0 : _p.generatePdf) === null || _q === void 0 ? void 0 : _q.url, "_blank").focus();
      }
    }
  }
  async onTrigger(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) === "save") {
      this.save();
    }
    else if (((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.name) === "export") {
      this.exportHTML();
      this.generatePdf();
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.name) === "send") {
      this.openSendEmailModal((_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.payload);
    }
    else if (((_e = event === null || event === void 0 ? void 0 : event.detail) === null || _e === void 0 ? void 0 : _e.name) === "preview") {
      this.isPreviewing = !this.isPreviewing;
      this.togglePreview(!!this.isPreviewing);
    }
    else if (((_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.name) === "more") {
      this.openTemplateInfoModal();
    }
    else if (((_g = event === null || event === void 0 ? void 0 : event.detail) === null || _g === void 0 ? void 0 : _g.name) === "closeModal" && ((_h = this.modalEl) === null || _h === void 0 ? void 0 : _h.dismiss)) {
      this.modalEl.dismiss();
      this.modalEl.remove();
      this.modalEl = null;
    }
    else if (((_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.name) === "sampleData") {
      this.openSampleDataModal();
    }
    else if (((_k = event === null || event === void 0 ? void 0 : event.detail) === null || _k === void 0 ? void 0 : _k.name) === "header") {
      this.openHeaderModal();
    }
    else if (((_l = event === null || event === void 0 ? void 0 : event.detail) === null || _l === void 0 ? void 0 : _l.name) === "footer") {
      this.openFooterModal();
    }
    else if (((_m = event === null || event === void 0 ? void 0 : event.detail) === null || _m === void 0 ? void 0 : _m.name) === "routes") {
      this.openRoutesModal();
    }
  }
  async togglePreview(show) {
    this.isPreviewing = show !== null && show !== void 0 ? show : !this.isPreviewing;
  }
  async save(customInput) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
    const input = { ...customInput };
    if ((_a = this.editorJs) === null || _a === void 0 ? void 0 : _a.save)
      input.editor = await this.editorJs.save();
    if ((_b = this.editorEl) === null || _b === void 0 ? void 0 : _b.exportHTML) {
      const html = await this.editorEl.exportHTML();
      if ((html === null || html === void 0 ? void 0 : html.length) > 0)
        input.html = html;
    }
    if ((_c = this.template) === null || _c === void 0 ? void 0 : _c.head)
      input.head = this.template.head;
    if ((_d = this.template) === null || _d === void 0 ? void 0 : _d.header)
      input.header = this.template.header;
    if ((_e = this.template) === null || _e === void 0 ? void 0 : _e.footer)
      input.footer = this.template.footer;
    if ((_f = this.template) === null || _f === void 0 ? void 0 : _f.routes)
      input.routes = this.template.routes;
    if ((_g = this.template) === null || _g === void 0 ? void 0 : _g.description)
      input.description = this.template.description;
    if ((_h = this.template) === null || _h === void 0 ? void 0 : _h.name)
      input.name = this.template.name;
    if ((_j = this.template) === null || _j === void 0 ? void 0 : _j.subject)
      input.subject = this.template.subject;
    if ((_k = this.template) === null || _k === void 0 ? void 0 : _k.sampleData)
      input.sampleData =
        typeof ((_l = this.template) === null || _l === void 0 ? void 0 : _l.sampleData) === "string"
          ? JSON.parse(this.template.sampleData)
          : this.template.sampleData;
    if ((_m = this.template) === null || _m === void 0 ? void 0 : _m.type)
      input.type = this.template.type;
    if ((_o = this.template) === null || _o === void 0 ? void 0 : _o.text)
      input.text = this.template.text;
    if ((_p = this.template) === null || _p === void 0 ? void 0 : _p.amp)
      input.amp = this.template.amp;
    if ((_q = this.template) === null || _q === void 0 ? void 0 : _q.tags)
      input.tags = this.template.tags;
    if ((_r = this.template) === null || _r === void 0 ? void 0 : _r.attachments)
      input.attachments = this.template.attachments;
    if ((_s = this.template) === null || _s === void 0 ? void 0 : _s.partial)
      input.partial =
        typeof ((_t = this.template) === null || _t === void 0 ? void 0 : _t.partial) === "boolean"
          ? this.template.partial
          : ((_u = this.template) === null || _u === void 0 ? void 0 : _u.partial) === "on";
    if ((_v = this.template) === null || _v === void 0 ? void 0 : _v.prerender)
      input.prerender =
        typeof ((_w = this.template) === null || _w === void 0 ? void 0 : _w.prerender) === "boolean"
          ? this.template.prerender
          : ((_x = this.template) === null || _x === void 0 ? void 0 : _x.prerender) === "on";
    const data = await cleanObjectOfReferences(input);
    if (this.currentInput &&
      data &&
      Object.entries(data).toString() ===
        Object.entries(this.currentInput).toString())
      return;
    this.currentInput = data;
    if (this.templateId === "new") {
      this.fireenjinSubmit.emit({
        endpoint: "addTemplate",
        name: this.name,
        params: {
          data,
        },
      });
    }
    else {
      this.fireenjinSubmit.emit({
        endpoint: "editTemplate",
        name: this.name,
        id: this.templateId || ((_y = this.template) === null || _y === void 0 ? void 0 : _y.id),
        data,
      });
    }
    await this.presentToast();
  }
  async openTemplateInfoModal() {
    var _a, _b;
    if (!this.modalEl)
      this.modalEl = await modalController.create({
        component: "fireenjin-mx-modal-template-settings",
        cssClass: "fireenjin-mx-modal-template-settings-wrapper",
        componentProps: {
          tags: this.tags || [],
          types: this.types,
          template: this.template,
          templateId: this.templateId || ((_a = this.template) === null || _a === void 0 ? void 0 : _a.id),
          html: this.renderedTemplateHtml || ((_b = this.template) === null || _b === void 0 ? void 0 : _b.html) || null,
        },
      });
    await this.modalEl.present();
    return this.modalEl;
  }
  async openSendEmailModal(input) {
    var _a, _b, _c;
    if (!this.modalEl)
      this.modalEl = await modalController.create({
        component: "fireenjin-mx-modal-send-email",
        cssClass: "fireenjin-mx-modal-send-email-wrapper",
        componentProps: {
          to: (input === null || input === void 0 ? void 0 : input.to) || [],
          templateId: (input === null || input === void 0 ? void 0 : input.templateId) || this.templateId || ((_a = this.template) === null || _a === void 0 ? void 0 : _a.id),
          hideTemplateSelect: (_b = input === null || input === void 0 ? void 0 : input.hideTemplateSelect) !== null && _b !== void 0 ? _b : true,
          subject: (input === null || input === void 0 ? void 0 : input.subject) || ((_c = this.template) === null || _c === void 0 ? void 0 : _c.subject) || null,
          from: (input === null || input === void 0 ? void 0 : input.from) || this.mailFrom || null,
          replyTo: (input === null || input === void 0 ? void 0 : input.replyTo) || this.mailReplyTo || null,
        },
      });
    await this.modalEl.present();
    return this.modalEl;
  }
  async openRoutesModal() {
    var _a;
    this.modalEl = await modalController.create({
      component: "fireenjin-mx-modal-code-editor",
      cssClass: "fireenjin-mx-modal-code-editor-wrapper",
      componentProps: {
        pageTitle: "Routes",
        name: "routes",
        language: "json",
        value: JSON.stringify(((_a = this.template) === null || _a === void 0 ? void 0 : _a.routes) || {}),
        endpoint: this.templateId ? "editTemplate" : "addTemplate",
        documentId: this.templateId,
      },
    });
    await this.modalEl.present();
    return this.modalEl;
  }
  async openFooterModal() {
    var _a;
    this.modalEl = await modalController.create({
      component: "fireenjin-mx-modal-code-editor",
      cssClass: "fireenjin-mx-modal-code-editor-wrapper",
      componentProps: {
        pageTitle: "Footer Template",
        name: "footer",
        language: "html",
        value: (_a = this.template) === null || _a === void 0 ? void 0 : _a.footer,
        endpoint: this.templateId ? "editTemplate" : "addTemplate",
        documentId: this.templateId,
      },
    });
    await this.modalEl.present();
    return this.modalEl;
  }
  async openHeaderModal() {
    var _a;
    this.modalEl = await modalController.create({
      component: "fireenjin-mx-modal-code-editor",
      cssClass: "fireenjin-mx-modal-code-editor-wrapper",
      componentProps: {
        pageTitle: "Header Template",
        name: "header",
        language: "html",
        value: (_a = this.template) === null || _a === void 0 ? void 0 : _a.header,
        endpoint: this.templateId ? "editTemplate" : "addTemplate",
        documentId: this.templateId,
      },
    });
    await this.modalEl.present();
    return this.modalEl;
  }
  async openSampleDataModal() {
    var _a;
    this.modalEl = await modalController.create({
      component: "fireenjin-mx-modal-code-editor",
      cssClass: "fireenjin-mx-modal-code-editor-wrapper",
      componentProps: {
        name: "sampleData",
        language: "json",
        value: JSON.stringify((_a = this.template) === null || _a === void 0 ? void 0 : _a.sampleData, null, 4),
        endpoint: this.templateId ? "editTemplate" : "addTemplate",
        documentId: this.templateId,
      },
    });
    await this.modalEl.present();
    return this.modalEl;
  }
  async generatePdf() {
    var _a;
    this.fireenjinSubmit.emit({
      endpoint: "generatePdf",
      params: {
        data: {
          templateId: this.templateId,
          payload: this.templateData || ((_a = this.template) === null || _a === void 0 ? void 0 : _a.sampleData) || {},
        },
      },
    });
  }
  async openControls() {
    this.actionEl = await actionSheetController.create({
      header: this.controlsHeader,
      buttons: [
        {
          text: "Toggle Preview",
          icon: this.isPreviewing ? "eye-off" : "eye",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "preview",
              payload: {
                isPreviewing: !this.isPreviewing,
              },
            });
          },
        },
        {
          text: "Save",
          icon: "save",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "save",
            });
          },
        },
        {
          text: "Edit Info",
          icon: "create",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "more",
            });
          },
        },
        {
          text: "Export",
          icon: "exit",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "export",
            });
          },
        },
        {
          text: "Send as Email",
          icon: "mail",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "send",
            });
          },
        },
        {
          text: "Edit Sample Data",
          icon: "code-slash",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "sampleData",
            });
          },
        },
        {
          text: "Edit Routes",
          icon: "code-slash",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "routes",
            });
          },
        },
        {
          text: "Edit Header",
          icon: "code-slash",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "header",
            });
          },
        },
        {
          text: "Edit Footer",
          icon: "code-slash",
          handler: () => {
            this.fireenjinTrigger.emit({
              name: "footer",
            });
          },
        },
        ...this.controls,
      ],
    });
    await this.actionEl.present();
    return this.actionEl;
  }
  async fetchData(input) {
    this.getTemplate((input === null || input === void 0 ? void 0 : input.templateId) || this.templateId);
  }
  async unsetPartials() {
    for (const partialName of this.currentPartials) {
      lib.unregisterPartial(partialName);
    }
    this.currentPartials = [];
    this.partials = [];
  }
  async setPartials(partials) {
    try {
      lib.registerPartial(null, "");
    }
    catch (_a) {
      // Make sure handlebars doesn't error when null as partial name
    }
    try {
      if ((partials === null || partials === void 0 ? void 0 : partials.length) && this.partials !== partials) {
        this.partials = partials;
      }
      for (const partial of this.partials) {
        try {
          if (this.currentPartials.includes(partial === null || partial === void 0 ? void 0 : partial.id))
            lib.unregisterPartial(partial === null || partial === void 0 ? void 0 : partial.id);
          lib.registerPartial(partial === null || partial === void 0 ? void 0 : partial.id, (partial === null || partial === void 0 ? void 0 : partial.html) || "");
          if (!this.currentPartials.includes(partial === null || partial === void 0 ? void 0 : partial.id))
            this.currentPartials = [...this.currentPartials, partial === null || partial === void 0 ? void 0 : partial.id];
        }
        catch (_b) {
          console.log(`Error setting partial ${partial === null || partial === void 0 ? void 0 : partial.id}.`);
        }
      }
    }
    catch (_c) {
      console.log("Error setting partials.");
    }
  }
  async setHelpers(helpers) {
    try {
      if ((helpers === null || helpers === void 0 ? void 0 : helpers.length) && this.helpers !== helpers) {
        this.helpers = helpers;
      }
      for (const [helperName, helperFn] of Object.entries(this.helpers)) {
        try {
          if (this.currentHelpers.includes(helperName))
            lib.unregisterHelper(helperName);
          lib.registerHelper(helperName, helperFn);
          if (!this.currentHelpers.includes(helperName))
            this.currentHelpers = [...this.currentHelpers, helperName];
        }
        catch (_a) {
          console.log(`Error setting helper ${helperName}.`);
        }
      }
    }
    catch (_b) {
      console.log("Error setting helpers.");
    }
  }
  async exportHTML() {
    var _a, _b, _c;
    const html = [
      ((_a = this.template) === null || _a === void 0 ? void 0 : _a.header) || "",
      await this.editorEl.exportHTML(),
      ((_b = this.template) === null || _b === void 0 ? void 0 : _b.footer) || "",
    ].join("");
    const template = lib.compile(html);
    return template(this.templateData || ((_c = this.template) === null || _c === void 0 ? void 0 : _c.sampleData) || {});
  }
  async generatePreview(templateData) {
    var _a, _b, _c;
    if (!((_a = this.editorEl) === null || _a === void 0 ? void 0 : _a.exportHTML))
      return;
    console.log(this.template);
    this.renderedTemplateHtml = await this.renderTemplateBody([
      ((_b = this.template) === null || _b === void 0 ? void 0 : _b.header) || "",
      await this.editorEl.exportHTML(),
      ((_c = this.template) === null || _c === void 0 ? void 0 : _c.footer) || "",
    ].join(""), templateData);
    return this.renderedTemplateHtml;
  }
  async renderTemplateBody(body, templateData) {
    var _a;
    const template = lib.compile(body);
    const data = templateData || ((_a = this.template) === null || _a === void 0 ? void 0 : _a.sampleData) || {};
    data.app = {
      url: (Build === null || Build === void 0 ? void 0 : Build.isBrowser) ? window.location.origin : ".",
    };
    return template(data);
  }
  setProgress(listEl) {
    var _a;
    let completed = 0;
    const items = Array.from(listEl.querySelectorAll("ion-item"));
    const progressBarEl = listEl.querySelector("ion-progress-bar");
    for (const item of items) {
      completed =
        completed + (((_a = item === null || item === void 0 ? void 0 : item.querySelector("ion-checkbox")) === null || _a === void 0 ? void 0 : _a.checked) ? 1 : 0);
    }
    const progressMath = completed / items.length;
    const progress = isFinite(progressMath) && progressMath > 0 ? progressMath : 0;
    progressBarEl.value = progress;
  }
  async presentToast() {
    const toast = await toastController.create({
      message: "The template has been saved successfully!",
      duration: 2000,
      position: this.toastPosition || "bottom",
    });
    toast.present();
  }
  getTemplate(id) {
    this.fireenjinFetch.emit({
      endpoint: "findTemplate",
      name: this.name,
      params: {
        id,
      },
    });
  }
  getExtension(path) {
    var basename = path.split(/[\\/]/).pop(), // extract file name from full path ...
    // (supports `\\` and `/` separators)
    pos = basename.lastIndexOf("."); // get last position of `.`
    if (basename === "" || pos < 1)
      // if file name is empty or ...
      return ""; //  `.` not found (-1) or comes first (0)
    return basename.slice(pos + 1); // extract extension ignoring `.`
  }
  async onPhotoUpload(event) {
    var _a;
    const storageRef = ref(this.storage, `${new Date().toISOString()}.${this.getExtension((_a = event === null || event === void 0 ? void 0 : event.file) === null || _a === void 0 ? void 0 : _a.name)}`);
    const upload = await uploadBytes(storageRef, event.file);
    const url = await getDownloadURL(upload.ref);
    return {
      success: true,
      file: {
        url,
      },
    };
  }
  async componentDidLoad() {
    var _a, _b, _c;
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.storage = getStorage();
    this.fetchData();
    if (this.helpers && ((_a = Object.keys(this.helpers)) === null || _a === void 0 ? void 0 : _a.length))
      this.setHelpers();
    if ((_b = this.partials) === null || _b === void 0 ? void 0 : _b.length)
      this.setPartials();
    if ((_c = this.editorEl) === null || _c === void 0 ? void 0 : _c.getInstance) {
      this.editorJs = await this.editorEl.getInstance();
    }
    else {
      setTimeout(async () => {
        this.editorJs = await this.editorEl.getInstance();
      }, 2000);
    }
  }
  render() {
    return (h(Host, { class: { "is-previewing": !!this.isPreviewing } }, (Build === null || Build === void 0 ? void 0 : Build.isBrowser) && (h("fireenjin-editor", { partials: this.partials || [], ref: (el) => (this.editorEl = el), uploadCallback: (event) => this.onPhotoUpload(event) })), h("div", { ref: (el) => (this.previewEl = el), id: "preview", innerHTML: this.renderedTemplateHtml })));
  }
  get appTemplateEl() { return getElement(this); }
  static get watchers() { return {
    "partials": ["setPartials"],
    "helpers": ["setHelpers"]
  }; }
};
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "onKeydown", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "onCodeChange", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "onEditorChange", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "onChange", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "save", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "setPartials", null);
__decorate([
  Debounce(1000)
], AppTemplateForm.prototype, "setHelpers", null);
AppTemplateForm.style = editorCss;

export { AppTemplateForm as fireenjin_mx_editor };

//# sourceMappingURL=fireenjin-mx-editor.entry.js.map