import { r as registerInstance, e as createEvent, B as Build, h } from './index-e0adbd54.js';
import { t as toastController } from './overlays-026eb2e6.js';

const modalSendEmailCss = ".fireenjin-mx-modal-send-email-wrapper{--min-width:300px}fireenjin-mx-modal-send-email ion-item-divider{--padding-start:0px}fireenjin-mx-modal-send-email fireenjin-avatar{margin-left:16px}fireenjin-mx-modal-send-email h5,fireenjin-mx-modal-send-email p{display:block;--ion-color-step-600:#666666;font-size:0.9em}fireenjin-mx-modal-send-email ion-button{display:table;margin-left:0px;margin:0 auto}fireenjin-mx-modal-send-email fireenjin-render-template#email-preview{color:#000000;display:block;margin:auto;border:1px solid var(--ion-color-light-shade);background-color:#ffffff;zoom:0.75;overflow:auto;width:100%;min-height:300px;max-height:300px}";

const ModalSendEmail = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = createEvent(this, "fireenjinTrigger", 7);
    this.to = [];
    this.text = undefined;
    this.html = undefined;
    this.template = undefined;
    this.subject = undefined;
    this.replyTo = undefined;
    this.from = undefined;
    this.attachments = [];
    this.templateId = undefined;
    this.partials = undefined;
    this.frames = undefined;
    this.templates = undefined;
    this.users = [];
    this.templateFilter = undefined;
    this.hideTemplateSelect = false;
    this.filters = undefined;
    this.helpers = undefined;
    this.payload = undefined;
    this.zoom = 0.45;
  }
  onChange(event) {
    var _a;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "templateId") {
      this.templateId = event.target.value;
    }
  }
  async onSuccess(event) {
    var _a;
    if (((_a = event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "sendTemplateEmail") {
      const sendEmailModalEl = document.querySelector("ion-modal.modal-send-email-wrapper");
      if (sendEmailModalEl === null || sendEmailModalEl === void 0 ? void 0 : sendEmailModalEl.dismiss) {
        await sendEmailModalEl.dismiss();
        this.presentToast();
      }
    }
  }
  async presentToast() {
    const toast = await toastController.create({
      message: "Your emails are on their way.",
      duration: 2000,
    });
    toast.present();
  }
  onBeforeSubmit(formData) {
    return {
      templateId: (formData === null || formData === void 0 ? void 0 : formData.templateId)
        ? formData.templateId
        : this.templateId
          ? this.templateId
          : null,
      text: this.text,
      html: this.html,
      subject: this.subject,
      attachments: this.attachments,
      from: (formData === null || formData === void 0 ? void 0 : formData.from) || this.from,
      emails: (formData === null || formData === void 0 ? void 0 : formData.to) ? formData.to : this.to ? this.to : [],
      filters: this.filters ? this.filters : null,
    };
  }
  componentWillLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
  }
  render() {
    return [
      h("ion-header", null, h("ion-toolbar", null, h("ion-title", null, "Send Email"), h("ion-buttons", { slot: "end" }, h("ion-button", { onClick: () => this.formEl.submit() }, h("ion-icon", { slot: "icon-only", name: "send" })), h("ion-button", { onClick: (event) => this.fireenjinTrigger.emit({
          event,
          name: "closeModal",
        }) }, h("ion-icon", { slot: "icon-only", name: "close-circle-outline" }))))),
      h("ion-content", { style: {
          "--background": "var(--ion-color-base)",
        } }, h("fireenjin-form", { ref: (el) => (this.formEl = el), endpoint: "sendTemplateEmail", disableEnterButton: true, hideControls: true, beforeSubmit: this.onBeforeSubmit.bind(this) }, h("ion-grid", null, h("ion-row", null, h("ion-col", { size: "12", sizeMd: "7" }, !this.hideTemplateSelect && (h("fireenjin-select", { ref: (el) => (this.templateSelectEl = el), name: "templateId", label: "Template", labelPosition: "stacked", placeholder: "Select Message Template", value: this.templateId, options: (this.templates || [])
          .filter((template) => !(template === null || template === void 0 ? void 0 : template.partial) &&
          (template === null || template === void 0 ? void 0 : template.type) === "email" &&
          (!this.templateFilter ||
            this.templateFilter(template)))
          .map((template) => ({
          label: template === null || template === void 0 ? void 0 : template.subject,
          value: template === null || template === void 0 ? void 0 : template.id,
        })), interface: "action-sheet" })), h("fireenjin-input", { name: "subject", label: "Subject", labelPosition: "stacked", placeholder: "Email Subject", value: this.subject }), h("fireenjin-select-tags", { name: "to", label: "Send To", labelPosition: "stacked", placeholder: "Add Emails", allowAdding: true, value: this.to, options: (this.users || []).map((user) => ({
          label: (user === null || user === void 0 ? void 0 : user.email) ||
            `${user === null || user === void 0 ? void 0 : user.firstName} ${user === null || user === void 0 ? void 0 : user.lastName} has no Email on File.`,
          value: user === null || user === void 0 ? void 0 : user.email,
          disabled: !(user === null || user === void 0 ? void 0 : user.email),
        })) }), h("fireenjin-chip-bar", null, (this.attachments || []).map((url) => {
        var _a, _b, _c, _d, _e, _f;
        return (h("ion-chip", null, h("ion-icon", { name: "document", class: "ft-icon-style" }), h("ion-label", null, decodeURI(((_f = (_e = (_d = (_c = (_b = (_a = url === null || url === void 0 ? void 0 : url.split) === null || _a === void 0 ? void 0 : _a.call(url, "/")) === null || _b === void 0 ? void 0 : _b.pop) === null || _c === void 0 ? void 0 : _c.call(_b)) === null || _d === void 0 ? void 0 : _d.split) === null || _e === void 0 ? void 0 : _e.call(_d, "?")) === null || _f === void 0 ? void 0 : _f[0]) ||
            "No File Name"))));
      }))), h("ion-col", { size: "12", sizeMd: "5", class: "ion-no-padding" }, this.template && (h("fireenjin-render-template", { id: "email-preview", template: this.template, data: this.payload || {}, helpers: this.helpers || {}, partials: this.partials || [], zoom: this.zoom || 0.45 }))))), h("ion-accordion-group", null, h("ion-accordion", null, h("ion-item", { slot: "header", style: { "--background": "transparent" } }, h("ion-label", null, "Advanced")), h("ion-grid", { slot: "content" }, h("ion-row", null, h("ion-col", null, h("fireenjin-input", { name: "from", label: "Email From", labelPosition: "stacked", placeholder: "Email Address to Send From", value: this.from })), h("ion-col", null, h("fireenjin-input", { name: "replyTo", label: "Reply To", labelPosition: "stacked", placeholder: "Email Address to Reply", value: this.replyTo })))))))),
    ];
  }
};
ModalSendEmail.style = modalSendEmailCss;

export { ModalSendEmail as fireenjin_mx_modal_send_email };
