'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-9f0649b4.js');
const hardwareBackButton = require('./overlays-a2727821.js');
const store = require('./store-10cc4a17.js');
require('./ionic-global-838555f5.js');
require('./helpers-c2496722.js');
require('./index-bac05b73.js');

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

const PageLogin = class {
  constructor(hostRef) {
    index.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 hardwareBackButton.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: store.state.config.url,
              dynamicLinkDomain: store.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 [
      index.h("ion-content", { class: "ion-padding" }, index.h("img", { alt: "fireenjin.mx Icon", src: "./assets/icon/icon.png", height: "250", width: "250", style: { margin: "0 auto", display: "block" } }), this.error && (index.h("ion-label", { style: {
          display: "block",
          maxWidth: "500px",
          margin: "15px auto 0 auto",
        }, color: "danger" }, index.h("ion-icon", { name: "information-circle" }), " ", this.error)), this.isAwaitingEmail ? (index.h("ion-grid", { style: { maxWidth: "500px", margin: "0 auto" } }, index.h("ion-row", null, index.h("ion-col", { class: "ion-padding" }, index.h("ion-icon", { style: {
          display: "block",
          width: "100px",
          height: "100px",
          color: "var(--ion-color-medium-tint)",
          margin: "15px auto",
        }, name: "mail" }), index.h("ion-label", null, "We sent an email to ", this.email, " with instructions. Please check your inbox and spam folders."))), index.h("ion-row", null, index.h("ion-col", null, index.h("ion-button", { fill: "outline", expand: "block", onClick: () => (this.isAwaitingEmail = false) }, index.h("ion-icon", { slot: "start", name: "arrow-back" }), index.h("ion-label", null, "Go Back"))), index.h("ion-col", null)))) : (index.h("ion-grid", { style: { maxWidth: "500px", margin: "0 auto" } }, index.h("ion-row", null, index.h("ion-col", null, index.h("fireenjin-input", { name: "email", type: "email", label: "Email Address", labelPosition: "stacked", placeholder: "j.doe@madnesslabs.net", value: this.email, required: true }), !this.isUsingEmailLink && (index.h("fireenjin-input", { name: "password", type: "password", labelPosition: "stacked", label: "Password", required: true })))), index.h("ion-row", null, index.h("ion-col", null, index.h("ion-button", { fill: "outline", expand: "block", onClick: () => (this.isUsingEmailLink = !this.isUsingEmailLink) }, index.h("ion-icon", { slot: "start", name: !this.isUsingEmailLink ? "mail" : "lock-closed" }), index.h("ion-label", null, this.isUsingEmailLink
        ? "Sign-in with Password"
        : "Sign-in With Email Link"))), index.h("ion-col", null, index.h("ion-button", { color: "warning", expand: "block", onClick: () => this.login("email") }, index.h("ion-label", null, "Sign-in"), index.h("ion-icon", { slot: "end", name: "log-in" })))), !this.isUsingEmailLink && (index.h("ion-row", null, index.h("ion-col", null, index.h("ion-button", { fill: "clear", color: "medium", expand: "block", onClick: () => this.sendPasswordReset() }, "Forgot my Password"))))))),
      index.h("app-footer", null),
    ];
  }
};
PageLogin.style = pageLoginCss;

exports.fireenjin_mx_page_login = PageLogin;
