import { r as registerInstance, h } from './index-33e43734.js';
import { l as loadingController } from './overlays-97615cf7.js';
import { s as state } from './store-880ba14d.js';
import './ionic-global-bd76bde2.js';
import './helpers-a9270f9e.js';
import './index-91eb4513.js';

const pageLoginCss = "fireenjin-mx-page-login app-header ion-button.menu-button{display:none !important}";

let PageLogin = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.isAwaitingEmail = false;
    this.isUsingEmailLink = false;
  }
  onChange(event) {
    var _a, _b, _c, _d;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "email" && ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value)) {
      this.email = event.target.value;
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.name) === "password" && ((_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.value)) {
      this.password = event.target.value;
    }
  }
  async presentLoading() {
    const loading = await loadingController.create({
      cssClass: "my-custom-class",
      message: "Please wait...",
      duration: 2000,
    });
    await loading.present();
    const { role, data } = await loading.onDidDismiss();
    console.log("Loading dismissed!", role, data);
  }
  async login(type) {
    var _a, _b;
    this.error = null;
    this.presentLoading();
    let res;
    try {
      res =
        type === "email"
          ? this.isUsingEmailLink
            ? await this.auth.withEmailLink(this.email, {
              url: state.config.url,
              dynamicLinkDomain: state.config.dynamicLinkDomain,
              iOS: {
                bundleId: "net.madnessenjin.mx",
              },
              android: {
                packageName: "net.madnessenjin.mx",
                installApp: false,
                minimumVersion: "12",
              },
              handleCodeInApp: true,
            })
            : await this.auth.withEmail(this.email, this.password)
          : await this.auth.withSocial(type);
      if (((_a = res === null || res === void 0 ? void 0 : res.user) === null || _a === void 0 ? void 0 : _a.uid) || ((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.user)) {
        const routerEl = document.querySelector("ion-router");
        routerEl.push("dashboard");
      }
      if (type === "email" && this.isUsingEmailLink) {
        this.isAwaitingEmail = true;
      }
    }
    catch (error) {
      this.error = error.message;
    }
  }
  async sendPasswordReset() {
    var _a;
    if (!((_a = this.email) === null || _a === void 0 ? void 0 : _a.length)) {
      alert("Please enter a valid email address.");
      return;
    }
    try {
      this.presentLoading();
      await this.auth.sendPasswordReset(this.email);
      this.isAwaitingEmail = true;
    }
    catch (error) {
      this.error = error.message;
    }
  }
  render() {
    return [
      h("ion-content", { class: "ion-padding" }, h("img", { alt: "fireenjin.mx Icon", src: "./assets/icon/icon.png", height: "250", width: "250", style: { margin: "0 auto", display: "block" } }), this.error && (h("ion-label", { style: {
          display: "block",
          maxWidth: "500px",
          margin: "15px auto 0 auto",
        }, color: "danger" }, h("ion-icon", { name: "information-circle" }), " ", this.error)), this.isAwaitingEmail ? (h("ion-grid", { style: { maxWidth: "500px", margin: "0 auto" } }, h("ion-row", null, h("ion-col", { class: "ion-padding" }, h("ion-icon", { style: {
          display: "block",
          width: "100px",
          height: "100px",
          color: "var(--ion-color-medium-tint)",
          margin: "15px auto",
        }, name: "mail" }), h("ion-label", null, "We sent an email to ", this.email, " with instructions. Please check your inbox and spam folders."))), h("ion-row", null, h("ion-col", null, h("ion-button", { fill: "outline", expand: "block", onClick: () => (this.isAwaitingEmail = false) }, h("ion-icon", { slot: "start", name: "arrow-back" }), h("ion-label", null, "Go Back"))), h("ion-col", null)))) : (h("ion-grid", { style: { maxWidth: "500px", margin: "0 auto" } }, h("ion-row", null, h("ion-col", null, h("fireenjin-input", { name: "email", type: "email", label: "Email Address", labelPosition: "stacked", placeholder: "j.doe@madnesslabs.net", value: this.email, required: true }), !this.isUsingEmailLink && (h("fireenjin-input", { name: "password", type: "password", labelPosition: "stacked", label: "Password", required: true })))), h("ion-row", null, h("ion-col", null, h("ion-button", { fill: "outline", expand: "block", onClick: () => (this.isUsingEmailLink = !this.isUsingEmailLink) }, h("ion-icon", { slot: "start", name: !this.isUsingEmailLink ? "mail" : "lock-closed" }), h("ion-label", null, this.isUsingEmailLink
        ? "Sign-in with Password"
        : "Sign-in With Email Link"))), h("ion-col", null, h("ion-button", { color: "warning", expand: "block", onClick: () => this.login("email") }, h("ion-label", null, "Sign-in"), h("ion-icon", { slot: "end", name: "log-in" })))), !this.isUsingEmailLink && (h("ion-row", null, h("ion-col", null, h("ion-button", { fill: "clear", color: "medium", expand: "block", onClick: () => this.sendPasswordReset() }, "Forgot my Password"))))))),
      h("app-footer", null),
    ];
  }
};
PageLogin.style = pageLoginCss;

export { PageLogin as fireenjin_mx_page_login };

//# sourceMappingURL=fireenjin-mx-page-login.entry.js.map