'use strict';

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

const index = require('./index-8277cbb3.js');
const domContextHooks_module = require('./dom-context-hooks.module-e7b6727c.js');
const index_module = require('./index.module-f8c8495f.js');
const jsonpointer = require('./jsonpointer-11327262.js');
const cjs = require('./cjs-1066ec21.js');
const utils = require('./utils-6847bc06.js');
require('./JSS-8503a151.js');
require('./mixins-4761d472.js');
require('./sqm-text-span-view-b9e10f5d.js');
const sqmPortalResetPasswordView = require('./sqm-portal-reset-password-view-a767b044.js');

function usePortalResetPassword(props) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  const [reset, setReset] = domContextHooks_module.useState(false);
  const [error, setError] = domContextHooks_module.useState("");
  const [verifyPasswordResetCode, verifyPasswordResetCodeState] = index_module.Je();
  const [resetPassword, resetPasswordState] = index_module.Te();
  const urlParams = new URLSearchParams(index_module.jn.location.search);
  const oobCode = urlParams.get("oobCode");
  urlParams.delete("oobCode");
  const nextPageOverride = urlParams.get("nextPage");
  const verifyPasswordResetCodeAndHandleResponse = async () => {
    const result = await verifyPasswordResetCode({ oobCode });
    if (result instanceof Error) {
      setError(props.networkErrorMessage);
    }
  };
  domContextHooks_module.useEffect(() => {
    verifyPasswordResetCodeAndHandleResponse();
  }, []);
  const submit = async (event) => {
    setError("");
    let formData = event.detail.formData;
    formData === null || formData === void 0 ? void 0 : formData.forEach((value, key) => {
      jsonpointer.jsonpointer.set(formData, key, value);
    });
    const variables = { oobCode, password: formData.password };
    if (formData.password !== formData.confirmPassword) {
      setError(props.passwordMismatchText);
      return;
    }
    const result = await resetPassword(variables);
    if (result instanceof Error) {
      return setError(props.networkErrorMessage);
    }
    if (result.resetManagedIdentityPassword) {
      setReset(true);
      setTimeout(() => {
        gotoNextPage();
      }, 5000);
    }
  };
  const gotoNextPage = () => {
    urlParams.delete("nextPage");
    const url = utils.sanitizeUrlPath(nextPageOverride || props.nextPage);
    index_module.jn.push(url.href);
  };
  const failed = () => {
    index_module.jn.push({
      pathname: props.failedPage,
      search: urlParams.toString() && "?" + urlParams.toString(),
    });
  };
  return {
    states: {
      loading: resetPasswordState.loading,
      reset,
      confirmPassword: props.confirmPassword,
      error: ((_e = (_d = (_c = (_b = (_a = resetPasswordState.errors) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.errors) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.extensions) === null || _e === void 0 ? void 0 : _e.message) || ((_j = (_h = (_g = (_f = resetPasswordState.errors) === null || _f === void 0 ? void 0 : _f.response) === null || _g === void 0 ? void 0 : _g.errors) === null || _h === void 0 ? void 0 : _h[0]) === null || _j === void 0 ? void 0 : _j.message) ||
        error,
      oobCodeValidating: verifyPasswordResetCodeState.loading,
      oobCodeValid: (_k = verifyPasswordResetCodeState.data) === null || _k === void 0 ? void 0 : _k.verifyManagedIdentityPasswordResetCode.success,
      content: {
        passwordResetHeader: props.passwordResetHeader,
        resetPasswordHeader: props.resetPasswordHeader,
        continueButtonText: props.continueButtonText,
        resetPasswordButtonText: props.resetPasswordButtonText,
        confirmPasswordFieldLabel: props.confirmPasswordFieldLabel,
        passwordFieldLabel: props.passwordFieldLabel,
        meetsRequirementsText: props.meetsRequirementsText,
        doesNotMeetRequirementsText: props.doesNotMeetRequirementsText,
        minErrorText: props.minErrorText,
        uppercaseErrorText: props.uppercaseErrorText,
        lowercaseErrorText: props.lowercaseErrorText,
        hasErrorText: props.hasErrorText,
        passwordMismatchText: props.passwordMismatchText,
        codeInvalidText: props.codeInvalidText,
        resetSuccessText: props.resetSuccessText,
      },
    },
    callbacks: { submit, failed, gotoNextPage },
  };
}

const PortalResetPassword = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ignored = true;
    /**
     * Redirect participants to this page when they successfully reset their password.
     *
     * @uiName Successful reset redirect
     * @uiWidget pageSelect
     */
    this.nextPage = "/";
    /**
     * Redirect participants to this page if password reset fails due to an outdated reset attempt.
     *
     * @uiName Failed reset redirect
     * @uiWidget pageSelect
     */
    this.failedPage = "/";
    /**
     * @uiName Show confirm password field
     */
    this.confirmPassword = false;
    /**
     * @uiName Reset password header text
     */
    this.resetPasswordHeader = "Reset your password";
    /**
     * Displayed after a successful password reset.
     *
     * @uiName Successful password reset text
     * @uiWidget textArea
     */
    this.passwordResetHeader = "Password reset";
    /**
     * @uiName Password reset button text
     */
    this.resetPasswordButtonText = "Reset Password";
    /**
     * Button text displayed after a successful password reset.
     *
     * @uiName Continue button text
     */
    this.continueButtonText = "Continue";
    /**
     * @uiName Password confirmation field label
     */
    this.confirmPasswordFieldLabel = "Confirm Password";
    /**
     * @uiName Password field label
     */
    this.passwordFieldLabel = "New Password";
    /**
     * @uiName Password requirement met
     */
    this.meetsRequirementsText = "Password has met all requirements";
    /**
     * @uiName Password requirement failed
     */
    this.doesNotMeetRequirementsText = "Password must meet the following requirements:";
    /**
     * @uiName Minimum length text
     */
    this.minErrorText = "be a minimum of 8 characters";
    /**
     * @uiName Missing uppercase text
     */
    this.uppercaseErrorText = "contain at least 1 uppercase character";
    /**
     * @uiName Missing lowercase text
     */
    this.lowercaseErrorText = "contain at least 1 lowercase character";
    /**
     * @uiName Missing number or symbol text
     */
    this.hasErrorText = "contain at least 1 number or symbol";
    /**
     * @uiName Passwords don’t match error message
     * Please make sure your passwords match
     */
    this.passwordMismatchText = "Passwords do not match.";
    /**
     * @uiName Reset code invalid error message
     */
    this.codeInvalidText = "The password reset code is invalid or has expired, please try again.";
    /**
     * @uiName Reset success text
     */
    this.resetSuccessText = "Your password has been reset and you are being redirected. If you are not redirected, please click Continue.";
    /**
     * Displayed when the password reset fails due to a network error. The participant can try refreshing the page.
     * @uiName Network error message
     */
    this.networkErrorMessage = "An error occurred while resetting your password. Please refresh the page and try again.";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const { states, callbacks } = index_module.isDemo()
      ? usePortalResetPasswordDemo(this)
      : usePortalResetPassword(this);
    return index.h(sqmPortalResetPasswordView.PortalResetPasswordView, { states: states, callbacks: callbacks });
  }
};
function usePortalResetPasswordDemo(props) {
  return cjs.cjs({
    states: {
      error: "",
      loading: false,
      reset: false,
      confirmPassword: props.confirmPassword,
      oobCodeValidating: false,
      oobCodeValid: true,
      content: {
        passwordResetHeader: props.passwordResetHeader,
        resetPasswordHeader: props.resetPasswordHeader,
        continueButtonText: props.continueButtonText,
        resetPasswordButtonText: props.resetPasswordButtonText,
        confirmPasswordFieldLabel: props.confirmPasswordFieldLabel,
        passwordFieldLabel: props.passwordFieldLabel,
        meetsRequirementsText: props.meetsRequirementsText,
        doesNotMeetRequirementsText: props.doesNotMeetRequirementsText,
        minErrorText: props.minErrorText,
        uppercaseErrorText: props.uppercaseErrorText,
        lowercaseErrorText: props.lowercaseErrorText,
        hasErrorText: props.hasErrorText,
        passwordMismatchText: props.passwordMismatchText,
        codeInvalidText: props.codeInvalidText,
        resetSuccessText: props.resetSuccessText,
      },
    },
    callbacks: {
      submit: async (_event) => {
        console.log("submit");
      },
      failed: () => { },
      gotoNextPage: () => { },
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}

exports.sqm_portal_reset_password = PortalResetPassword;
