import jsonpointer from "jsonpointer";
import { useEffect, useState } from "@saasquatch/universal-hooks";
import { navigation, useVerifyPasswordResetCodeMutation, useResetPasswordMutation, } from "@saasquatch/component-boilerplate";
import { sanitizeUrlPath } from "../../utils/utils";
export 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] = useVerifyPasswordResetCodeMutation();
  const [resetPassword, resetPasswordState] = useResetPasswordMutation();
  const urlParams = new URLSearchParams(navigation.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);
    navigation.push(url.href);
  };
  const failed = () => {
    navigation.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 },
  };
}
