'use strict';

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

const index = require('./index-22fd9091.js');
const core = require('./core-787915c4.js');
require('./_commonjsHelpers-5cfcba41.js');
require('./index-a20d8492.js');
const runStore = require('./runStore-bfd81991.js');
const index$1 = require('./index-66ca60fa.js');
const Mutations = require('./Mutations-7f7eb38b.js');

const Reset = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.resetEmailFormId = "resetEmail";
    this.resetPasswordFormId = "resetPassword";
    this.resetRequested = false;
    this.resetWithToken = false;
    this.resetCompleted = false;
    this.token = null;
  }
  async formEventHandler({ detail }) {
    if (detail.formId == this.resetEmailFormId)
      return this.requestReset(detail);
    if (detail.formId == this.resetPasswordFormId)
      return this.resetPassword(detail);
  }
  async componentDidRender() {
    const reset = new URLSearchParams(location.search).get("reset");
    if (reset) {
      this.token = reset;
      this.resetWithToken = true;
    }
  }
  async requestReset(detail) {
    const userInput = {};
    Object.keys(detail).map((value) => {
      if (detail[value].key) {
        userInput[detail[value].key] = detail[value].value;
      }
    });
    const request = await core.coreState.client.mutate({
      mutation: index$1.src(Mutations.requestPasswordResetGQL),
      variables: userInput,
    });
    if (request.data.userRequestPasswordReset) {
      this.resetRequested = true;
    }
  }
  async resetPassword(detail) {
    const userInput = {};
    Object.keys(detail).map((value) => {
      if (detail[value].key) {
        userInput[detail[value].key] = detail[value].value;
      }
    });
    const request = await core.coreState.client.mutate({
      mutation: index$1.src(Mutations.passwordResetGQL),
      variables: {
        token: this.token,
        resetInput: userInput,
      },
    });
    if (request.data.userResetPassword) {
      this.resetCompleted = true;
      runStore.runState.router.push("/login");
    }
  }
  render() {
    return (index.h(index.Host, null, index.h("corejam-box", { p: 8, max: "md", mx: "auto", px: 4, lgPx: 0, flex: true, justify: "between", mb: 24 }, index.h("corejam-box", { w: "5", mx: "auto" }, this.resetWithToken == false ? (index.h(index.Fragment, null, this.resetRequested == false ? (index.h(index.Fragment, null, index.h("corejam-box", { bWidthBottom: 1, bColor: "gray-400", mb: 8, mt: 12 }, index.h("corejam-type", { as: "h3", size: "xl" }, "Reset Password")), index.h("corejam-form-container", { name: this.resetEmailFormId }, index.h("corejam-form-input", { name: "email", type: "text", formId: this.resetEmailFormId, label: "Your Email", placeholder: "Your email" }), index.h("corejam-base-link", { href: "#" }, index.h("corejam-form-submit", { formId: this.resetEmailFormId }, index.h("corejam-type", { size: "sm", weight: "bold", transform: "uppercase", decoration: "underline" }, "Reset")))))) : (index.h("corejam-modal", { message: "Please check your mail", type: "success" })))) : (index.h(index.Fragment, null, this.resetCompleted == false ? (index.h(index.Fragment, null, index.h("corejam-box", { bWidthBottom: 1, bColor: "gray-400", mb: 8, mt: 12 }, index.h("corejam-type", { as: "h3", size: "xl" }, "Update Password")), index.h("corejam-form-container", { name: this.resetPasswordFormId }, index.h("corejam-box", { flex: true, mb: 8 }, index.h("corejam-form-input", { name: "password", type: "password", formId: this.resetPasswordFormId, label: "Password", placeholder: "Password" })), index.h("corejam-box", { flex: true, mb: 8 }, index.h("corejam-form-input", { name: "passwordConfirm", type: "password", formId: this.resetPasswordFormId, label: "Confirm Password", placeholder: "Confirm Password" })), index.h("corejam-base-link", { href: "#" }, index.h("corejam-form-submit", { formId: this.resetPasswordFormId }, index.h("corejam-type", { size: "sm", weight: "bold", transform: "uppercase", decoration: "underline" }, "Reset")))))) : (index.h("corejam-type", null, "Please check your mail"))))))));
  }
};

exports.auth_reset = Reset;
