import{k as i,f as r}from"./p-a7fc1e02.js";import{d as e,J as o,X as n,$ as s}from"./p-d0993bb9.js";const t=e.gql`
  query userLookup {
    viewer {
      ... on User {
        id
        accountId
        email
        impactConnection {
          user {
            id
            email
          }
        }
      }
    }
  }
`,a=e.gql`
  mutation requestUserEmailVerification($user: UserIdInput!) {
    requestUserEmailVerification(user: $user) {
      success
    }
  }
`,l=e.gql`
  mutation requestImpactPublisherEmail2FA($user: UserIdInput!) {
    requestImpactPublisherEmail2FA(user: $user) {
      success
    }
  }
`,u=e.gql`
  mutation verifyUserEmail($user: UserIdInput!, $code: String!) {
    verifyUserEmail(user: $user, code: $code) {
      id
      accountId
      email
      emailVerified
    }
  }
`,c=e.gql`
  mutation submitImpactPublisherEmail2FACode(
    $user: UserIdInput!
    $code: String!
  ) {
    submitImpactPublisherEmail2FACode(user: $user, code: $code) {
      verifiedEmail
      accessKey
    }
  }
`;function d(){const e=o(),[d,v]=i(null),[m,f]=i({participant:!1,impact:!1}),[p]=n(t),[E,{loading:I,errors:y}]=s(a),[$,{loading:h,errors:w}]=s(l),U=I||h,b=y||w,[g,{loading:C,errors:q}]=s(u),[A,{loading:F,errors:P}]=s(c),V=C||F,j=q||P,[k,K]=i(!1);return r((()=>{k||(async()=>{var i,r,e,o,n,s,t,a;try{const l=await p({});if(!l||l instanceof Error)throw new Error;f({participant:!!(null===(i=null==l?void 0:l.viewer)||void 0===i?void 0:i.email),impact:!!(null===(o=null===(e=null===(r=null==l?void 0:l.viewer)||void 0===r?void 0:r.impactConnection)||void 0===e?void 0:e.user)||void 0===o?void 0:o.email)}),v((null===(t=null===(s=null===(n=null==l?void 0:l.viewer)||void 0===n?void 0:n.impactConnection)||void 0===s?void 0:s.user)||void 0===t?void 0:t.email)||(null===(a=null==l?void 0:l.viewer)||void 0===a?void 0:a.email)||null),K(!0)}catch(i){console.error("Could not initialise verification",i)}})()}),[]),{initialized:k,verificationEmail:d,send:[async()=>{let i=null;if(!k)return i;if(!m.participant&&!m.impact)return i;try{const r=m.impact?$:E,o=await r({user:{id:e.id,accountId:e.accountId}});if(o instanceof Error||!o)throw new Error;i=m.impact?o.requestImpactPublisherEmail2FA:o.requestUserEmailVerification}catch(i){console.error("Could not send verification email",i)}finally{return i}},{loading:U,errors:b}],verify:[async i=>{var r;let o=null;if(!k)return o;if(!m.participant&&!m.impact)return o;try{const n=m.impact?A:g,s=await n({user:{id:e.id,accountId:e.accountId},code:i});if(s instanceof Error||!s)throw new Error;o={success:!0,accessKey:m.impact?null===(r=s.submitImpactPublisherEmail2FACode)||void 0===r?void 0:r.accessKey:void 0}}catch(i){console.error("Could not verify email",i)}finally{return o}},{loading:V,errors:j}]}}export{d as u}