import { r as registerInstance, h as h$1 } from './index-38ad4957.js';
import { k as useState, f as useEffect, n as h } from './dom-context-hooks.module-50442785.js';
import { f as Je, T as Te, j as jn, i as isDemo } from './index.module-b23fdad8.js';
import { j as jsonpointer } from './jsonpointer-388a7082.js';
import { c as cjs } from './cjs-bdfb4486.js';
import { s as sanitizeUrlPath } from './utils-334c1e34.js';
import './JSS-67b5cff8.js';
import './mixins-f750863a.js';
import './sqm-text-span-view-72f43dff.js';
import { P as PortalResetPasswordView } from './sqm-portal-reset-password-view-f84c04cf.js';

function usePortalResetPassword(props) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  const [reset, setReset] = useState(false);
  const [error, setError] = useState("");
  const [verifyPasswordResetCode, verifyPasswordResetCodeState] = Je();
  const [resetPassword, resetPasswordState] = Te();
  const urlParams = new URLSearchParams(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);
    }
  };
  useEffect(() => {
    verifyPasswordResetCodeAndHandleResponse();
  }, []);
  const submit = async (event) => {
    setError("");
    let formData = event.detail.formData;
    formData === null || formData === void 0 ? void 0 : formData.forEach((value, key) => {
      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 = sanitizeUrlPath(nextPageOverride || props.nextPage);
    jn.push(url.href);
  };
  const failed = () => {
    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) {
    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.";
    h(this);
  }
  disconnectedCallback() { }
  render() {
    const { states, callbacks } = isDemo()
      ? usePortalResetPasswordDemo(this)
      : usePortalResetPassword(this);
    return h$1(PortalResetPasswordView, { states: states, callbacks: callbacks });
  }
};
function usePortalResetPasswordDemo(props) {
  return 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 });
}

export { PortalResetPassword as sqm_portal_reset_password };
