import { useState } from "@saasquatch/universal-hooks";
import jsonpointer from "jsonpointer";
import { navigation, useAuthenticateWithGoogle, useAuthenticateWithEmailAndPasswordMutation, } from "@saasquatch/component-boilerplate";
import { sanitizeUrlPath } from "../../utils/utils";
export function usePortalLogin(props) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
  const [request, { loading, errors, data }] = useAuthenticateWithEmailAndPasswordMutation();
  const [loginWithGoogle, { loading: gLoading, errors: gErrors }] = useAuthenticateWithGoogle();
  const [error, setError] = useState("");
  const urlParams = new URLSearchParams(navigation.location.search);
  const nextPageOverride = urlParams.get("nextPage");
  const googleSubmit = async (e) => {
    var _a, _b, _c, _d;
    if (!((_a = e.detail) === null || _a === void 0 ? void 0 : _a.credential))
      return;
    const result = await loginWithGoogle({ idToken: e.detail.credential });
    if (result instanceof Error) {
      if ((result === null || result === void 0 ? void 0 : result.message) || ((_b = result === null || result === void 0 ? void 0 : result["response"]) === null || _b === void 0 ? void 0 : _b["error"]))
        if ((_c = result === null || result === void 0 ? void 0 : result["response"]) === null || _c === void 0 ? void 0 : _c["errors"].some((error) => { var _a; return ((_a = error === null || error === void 0 ? void 0 : error["extensions"]) === null || _a === void 0 ? void 0 : _a["message"]) === "Invalid Credentials."; })) {
          setError(props.googleUserNotRegisteredError);
        }
        else {
          setError(props.networkErrorMessage);
        }
      return;
    }
    if ((_d = result.authenticateManagedIdentityWithGoogle) === null || _d === void 0 ? void 0 : _d.token) {
      urlParams.delete("nextPage");
      const url = sanitizeUrlPath(nextPageOverride || props.nextPage);
      navigation.push(url.href);
    }
  };
  const submit = async (event) => {
    var _a, _b;
    setError("");
    let formData = event.detail.formData;
    formData === null || formData === void 0 ? void 0 : formData.forEach((value, key) => {
      jsonpointer.set(formData, key, value);
    });
    const variables = { email: formData.email, password: formData.password };
    const result = await request(variables);
    if (result instanceof Error) {
      if ((result === null || result === void 0 ? void 0 : result.message) || ((_a = result === null || result === void 0 ? void 0 : result["response"]) === null || _a === void 0 ? void 0 : _a["error"]))
        setError(props.networkErrorMessage);
      return;
    }
    if ((_b = result.authenticateManagedIdentityWithEmailAndPassword) === null || _b === void 0 ? void 0 : _b.token) {
      urlParams.delete("nextPage");
      const url = sanitizeUrlPath(nextPageOverride || props.nextPage);
      navigation.push(url.href);
    }
  };
  const errorMessage = ((_d = (_c = (_b = (_a = errors === null || errors === void 0 ? void 0 : errors.response) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.extensions) === null || _d === void 0 ? void 0 : _d.message) || ((_g = (_f = (_e = errors === null || errors === void 0 ? void 0 : errors.response) === null || _e === void 0 ? void 0 : _e.errors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.message) ||
    error;
  const googleErrorMessage = ((_l = (_k = (_j = (_h = gErrors === null || gErrors === void 0 ? void 0 : gErrors.response) === null || _h === void 0 ? void 0 : _h.errors) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.extensions) === null || _l === void 0 ? void 0 : _l.message) || ((_p = (_o = (_m = gErrors === null || gErrors === void 0 ? void 0 : gErrors.response) === null || _m === void 0 ? void 0 : _m.errors) === null || _o === void 0 ? void 0 : _o[0]) === null || _p === void 0 ? void 0 : _p.message) ||
    error;
  return {
    states: {
      loading: loading || gLoading,
      error: errorMessage || googleErrorMessage,
      registerPath: props.registerPath,
      forgotPasswordPath: props.forgotPasswordPath,
    },
    callbacks: {
      googleSubmit,
      submit,
    },
  };
}
