'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var client = require('@dynamic-labs-sdk/client');
var utils$1 = require('@dynamic-labs/utils');
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
var api = require('../api.cjs');
var constants = require('../constants.cjs');
var utils = require('../utils.cjs');

const createEmailVerification = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ email, environmentId, captchaToken, }) {
    try {
        const res = yield api.sdkApi().createEmailVerification({
            emailVerificationCreateRequest: {
                captchaToken,
                email,
            },
            environmentId,
        });
        return res;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error creating email verification');
        throw new utils$1.EmailVerificationError(data.error, data.code);
    }
});
const signInWithEmailVerification = (_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ verificationToken, verificationUUID, environmentId, captchaToken, sessionPublicKey, }) {
    try {
        const res = yield api.sdkApi().signInWithEmailVerification({
            emailVerificationVerifyRequest: {
                captchaToken,
                sessionPublicKey,
                verificationToken,
                verificationUUID,
            },
            environmentId,
        });
        return res;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error email signin');
        if (data.error.code === 'missing_from_list') {
            throw new utils$1.NoAccessError({
                code: data.error.code,
                email: data.payload.email,
            });
        }
        if (data.code === 'credential_not_enabled_for_sign_in') {
            throw new utils$1.DynamicError(data.error, utils$1.ErrorCode.CREDENTIAL_NOT_ENABLED_FOR_SIGN_IN);
        }
        if (data.code === 'email_associated_with_different_provider') {
            throw new utils$1.AccountExistsError(data.error, data.payload, data.code);
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new utils$1.EmailVerificationError(data.error, data.code);
    }
});
const retryEmailVerification = (_c) => _tslib.__awaiter(void 0, [_c], void 0, function* ({ email, environmentId, verificationUUID, }) {
    try {
        const res = yield api.sdkApi().retryEmailVerification({
            emailVerificationRetryRequest: {
                email,
                verificationUUID,
            },
            environmentId,
        });
        return res;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error retrying email verification');
        throw new utils$1.EmailVerificationError(data.error, data.code);
    }
});
const verifyEmail = (_d) => _tslib.__awaiter(void 0, [_d], void 0, function* ({ verificationToken, verificationUUID, environmentId, }) {
    try {
        const elevatedToken = client.getElevatedAccessToken({
            scope: sdkApiCore.TokenScope.Credentiallink,
        });
        const res = yield api.sdkApi({
            customHeaders: elevatedToken
                ? { [constants.ELEVATED_ACCESS_TOKEN_HEADER]: elevatedToken }
                : undefined,
        }).verifyEmailVerification({
            emailVerificationVerifyRequest: {
                verificationToken,
                verificationUUID,
            },
            environmentId,
        });
        return res;
    }
    catch (e) {
        const data = yield utils.logResponseError(e, 'Error linking email');
        if (data.code === sdkApiCore.UnprocessableEntityErrorCode.UserHasAlreadyAccountWithEmail) {
            throw new utils$1.UserHasAccountWithEmailError(data.error, data.code);
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new utils$1.EmailVerificationError(data.error, data.code);
    }
});

exports.createEmailVerification = createEmailVerification;
exports.retryEmailVerification = retryEmailVerification;
exports.signInWithEmailVerification = signInWithEmailVerification;
exports.verifyEmail = verifyEmail;
