'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { getElevatedAccessToken } from '@dynamic-labs-sdk/client';
import { NoAccessError, DynamicError, ErrorCode, AccountExistsError, SandboxMaximumThresholdReachedError, EmailVerificationError, UserHasAccountWithEmailError } from '@dynamic-labs/utils';
import { TokenScope, UnprocessableEntityErrorCode } from '@dynamic-labs/sdk-api-core';
import { sdkApi } from '../api.js';
import { ELEVATED_ACCESS_TOKEN_HEADER } from '../constants.js';
import { logResponseError } from '../utils.js';

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

export { createEmailVerification, retryEmailVerification, signInWithEmailVerification, verifyEmail };
