import { Container as n } from "unstated";
import { Iam as r } from "@accelbyte/sdk-iam";
const a = {
  isRequestingVerificationCode: !1,
  isRequestVerificationCodeSuccess: null,
  requestVerificationCodeError: null,
  isVerifying: !1,
  verifyError: null,
  isFetchingPolicy: !1,
  legalPolicy: null
};
class f extends n {
  constructor(i) {
    super(), this.sdk = i, this.sendVerificationCode = async (e, t) => {
      let s = null;
      if (!this.state.isRequestingVerificationCode)
        try {
          this.setState({
            isRequestingVerificationCode: !0,
            isRequestVerificationCodeSuccess: null,
            requestVerificationCodeError: null
          }), await r.UsersApi(this.sdk).createUserMeCodeRequest({ context: t, emailAddress: e });
        } catch (o) {
          throw s = o, o;
        } finally {
          this.setState({
            isRequestingVerificationCode: !1,
            requestVerificationCodeError: s,
            isRequestVerificationCodeSuccess: s === null
          });
        }
    }, this.state = a;
  }
  async sendNewUserVerificationCode(i) {
    if (!this.state.isRequestingVerificationCode)
      try {
        this.setState({
          isRequestingVerificationCode: !0,
          isRequestVerificationCodeSuccess: null,
          requestVerificationCodeError: null
        }), await r.UsersApi(this.sdk).createUserCodeRequest({ emailAddress: i }), this.setState({ isRequestVerificationCodeSuccess: !0 });
      } catch (e) {
        throw this.setState({ requestVerificationCodeError: e, isRequestVerificationCodeSuccess: !1 }), e;
      } finally {
        this.setState({ isRequestingVerificationCode: !1 });
      }
  }
  async verify(i, e) {
    try {
      this.setState({ isVerifying: !0, verifyError: null }), await r.UsersApi(this.sdk).createUserMeCodeVerify({ code: i, contactType: "email", languageTag: e });
    } catch (t) {
      throw this.setState({ verifyError: t }), t;
    } finally {
      this.setState({ isVerifying: !1 });
    }
  }
  clear() {
    this.setState(a);
  }
}
export {
  f as UserVerificationManager
};
