'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 locale = require('@dynamic-labs/locale');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../utils/constants/colors.cjs');
require('../../../utils/constants/values.cjs');
require('../../../shared/consts/index.cjs');
var api = require('../api.cjs');
var constants = require('../constants.cjs');
var utils = require('../utils.cjs');

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

exports.createSmsVerification = createSmsVerification;
exports.retrySmsVerification = retrySmsVerification;
exports.signInWithSmsVerification = signInWithSmsVerification;
exports.verifySms = verifySms;
