'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { getElevatedAccessToken } from '@dynamic-labs-sdk/client';
import { DynamicError, ErrorCode, NoAccessError, SmsVerificationError, SandboxMaximumThresholdReachedError } from '@dynamic-labs/utils';
import { TokenScope } from '@dynamic-labs/sdk-api-core';
import { Locale } from '@dynamic-labs/locale';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import { logger } from '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../../utils/constants/colors.js';
import '../../../utils/constants/values.js';
import '../../../shared/consts/index.js';
import { sdkApi } from '../api.js';
import { ELEVATED_ACCESS_TOKEN_HEADER } from '../constants.js';
import { logResponseError } from '../utils.js';

const createSmsVerification = (_a) => __awaiter(void 0, [_a], void 0, function* ({ isoCountryCode, phoneNumber, phoneCountryCode, environmentId, captchaToken, }) {
    try {
        return yield sdkApi().createSmsVerification({
            environmentId,
            smsVerificationCreateRequest: {
                captchaToken,
                isoCountryCode,
                phoneCountryCode,
                phoneNumber,
            },
        });
    }
    catch (e) {
        const data = yield logResponseError(e, 'Error creating phone number verification');
        throw new SmsVerificationError(data.error, data.code);
    }
});
const signInWithSmsVerification = (_b) => __awaiter(void 0, [_b], void 0, function* ({ verificationToken, verificationUUID, environmentId, captchaToken, sessionPublicKey, }) {
    var _c;
    try {
        return yield sdkApi().signInWithSmsVerification({
            environmentId,
            smsVerificationVerifyRequest: {
                captchaToken,
                sessionPublicKey,
                verificationToken,
                verificationUUID,
            },
        });
    }
    catch (e) {
        const data = yield logResponseError(e, 'Error signin phone number');
        if (data.code === 'credential_not_enabled_for_sign_in') {
            throw new DynamicError(data.error, ErrorCode.CREDENTIAL_NOT_ENABLED_FOR_SIGN_IN);
        }
        if (((_c = data.error) === null || _c === void 0 ? void 0 : _c.code) === 'missing_from_list') {
            throw new NoAccessError({
                code: data.error.code,
                phoneNumber: data.payload.phoneNumber,
            });
        }
        if (data.code === 'wrong_sms_verification_token') {
            throw new SmsVerificationError(Locale.getInstance().t('dyn_login.helper.phone_number_form.invalid_sms_verification'), data.code);
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new SmsVerificationError(data.error, data.code);
    }
});
const retrySmsVerification = (_d) => __awaiter(void 0, [_d], void 0, function* ({ isoCountryCode, phoneCountryCode, phoneNumber, environmentId, verificationUUID, }) {
    try {
        return yield sdkApi().retrySmsVerification({
            environmentId,
            smsVerificationRetryRequest: {
                isoCountryCode,
                phoneCountryCode,
                phoneNumber,
                verificationUUID,
            },
        });
    }
    catch (e) {
        const data = yield logResponseError(e, 'Error retrying phone number verification');
        throw new SmsVerificationError(data.error, data.code);
    }
});
const verifySms = (_e) => __awaiter(void 0, [_e], void 0, function* ({ verificationToken, verificationUUID, environmentId, }) {
    try {
        const elevatedToken = getElevatedAccessToken({
            scope: TokenScope.Credentiallink,
        });
        return yield sdkApi({
            customHeaders: elevatedToken
                ? { [ELEVATED_ACCESS_TOKEN_HEADER]: elevatedToken }
                : undefined,
        }).verifySmsVerification({
            environmentId,
            smsVerificationVerifyRequest: {
                verificationToken,
                verificationUUID,
            },
        });
    }
    catch (e) {
        const data = yield logResponseError(e, 'Error verifying phone number');
        logger.error(data.error);
        if (data.code === 'credential_not_enabled_for_sign_in') {
            throw new DynamicError(data.error, ErrorCode.CREDENTIAL_NOT_ENABLED_FOR_SIGN_IN);
        }
        if (data.error.code === 'sandbox_maximum_threshold_reached') {
            throw new SandboxMaximumThresholdReachedError(data.error.message);
        }
        throw new SmsVerificationError(data.error, data.code);
    }
});

export { createSmsVerification, retrySmsVerification, signInWithSmsVerification, verifySms };
