import { useLazyQuery, useParentState, useSetParent, useUserIdentity, } from "@saasquatch/component-boilerplate";
import { useState } from "@saasquatch/stencil-hooks";
import { useEffect } from "@saasquatch/universal-hooks";
import { gql } from "graphql-request";
import { SHOW_CODE_NAMESPACE, VERIFICATION_EMAIL_NAMESPACE, VERIFICATION_PARENT_NAMESPACE, } from "./keys";
const USER_LOOKUP = gql `
  query checkUserVerification {
    viewer {
      ... on User {
        id
        accountId
        email
        emailVerified
        managedIdentity {
          email
          emailVerified
        }
      }
    }
  }
`;
export function useWidgetVerification() {
  const userIdentity = useUserIdentity();
  const [showCode, setShowCode] = useParentState({
    namespace: SHOW_CODE_NAMESPACE,
    initialValue: false,
  });
  const [email, setEmail] = useParentState({
    namespace: VERIFICATION_EMAIL_NAMESPACE,
    initialValue: userIdentity === null || userIdentity === void 0 ? void 0 : userIdentity.email,
  });
  const setContext = useSetParent(VERIFICATION_PARENT_NAMESPACE);
  const [loading, setLoading] = useState(true);
  const [fetch] = useLazyQuery(USER_LOOKUP);
  useEffect(() => {
    const checkUser = async () => {
      var _a, _b, _c;
      try {
        const res = await fetch({});
        if (!res || res instanceof Error)
          throw new Error();
        if ((_a = res === null || res === void 0 ? void 0 : res.viewer) === null || _a === void 0 ? void 0 : _a.emailVerified)
          setContext(true);
        else if ((_c = (_b = res === null || res === void 0 ? void 0 : res.viewer) === null || _b === void 0 ? void 0 : _b.managedIdentity) === null || _c === void 0 ? void 0 : _c.emailVerified)
          setContext(true);
      }
      catch (e) {
        console.error("Could not fetch user information:", e);
      }
      finally {
        setLoading(false);
      }
    };
    checkUser();
  }, []);
  const onVerification = () => {
    setContext(true);
  };
  return { showCode, onVerification, loading };
}
