'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var utils$1 = require('@dynamic-labs/utils');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('../../../client/client.cjs');
require('react');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
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');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
var api = require('../api.cjs');
var utils = require('../utils.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../events/dynamicEvents.cjs');
var storeTokenAndUser = require('../../../client/extension/storeTokenAndUser/storeTokenAndUser.cjs');

const initAuth = (environmentId, providerType, oauthInitAuthRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        yield api.sdkApi().initAuth({
            environmentId,
            oauthInitAuthRequest,
            providerType,
        });
    }
    catch (err) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let data;
        try {
            data = yield err.json();
            logger.logger.error(data);
        }
        catch (_a) {
            logger.logger.error(err);
            throw err;
        }
        throw new Error(data.error);
    }
});
const verifyOAuth = (environmentId_1, providerType_1, _b) => _tslib.__awaiter(void 0, [environmentId_1, providerType_1, _b], void 0, function* (environmentId, providerType, { code, codeVerifier, state }) {
    const oauthRequest = {
        code,
        codeVerifier,
        state,
    };
    try {
        const response = yield api.sdkApi().oauthVerify({
            environmentId,
            oauthRequest,
            providerType,
        });
        return storeTokenAndUser.storeTokenAndUser(response);
    }
    catch (e) {
        return handleVerifyError(e);
    }
});
const unlinkOAuth = (_c) => _tslib.__awaiter(void 0, [_c], void 0, function* ({ environmentId, primaryWalletId, verifiedCredentialId, }) {
    // 7702 wallets have a -zerodev suffix because it's a credential created on the fly
    const normalizedPrimaryWalletId = primaryWalletId === null || primaryWalletId === void 0 ? void 0 : primaryWalletId.replace('-zerodev', '');
    try {
        const response = yield api.sdkApi().verifyUnlink({
            environmentId,
            verifyUnlinkRequest: {
                primaryWalletId: normalizedPrimaryWalletId,
                verifiedCredentialId,
            },
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return undefined;
    }
});
const signInOAuth = (environmentId_2, providerType_2, _d) => _tslib.__awaiter(void 0, [environmentId_2, providerType_2, _d], void 0, function* (environmentId, providerType, { code, codeVerifier, state, captchaToken, sessionPublicKey, ssoProviderId, }) {
    const oauthRequest = {
        captchaToken,
        code,
        codeVerifier,
        sessionPublicKey,
        ssoProviderId,
        state,
    };
    try {
        const response = yield api.sdkApi().oauthSignIn({
            environmentId,
            oauthRequest,
            providerType,
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return handleSignInError(e);
    }
});
const getOAuthResult = (environmentId_3, providerType_3, _e) => _tslib.__awaiter(void 0, [environmentId_3, providerType_3, _e], void 0, function* (environmentId, providerType, { state }) {
    const oauthResultRequest = {
        state,
    };
    try {
        const result = yield api.sdkApi().oauthResult({
            environmentId,
            oauthResultRequest,
            providerType,
        });
        return result;
    }
    catch (e) {
        yield utils.logResponseError(e, 'error getting oauth result');
        return undefined;
    }
});
const farcasterSignIn = (environmentId, farcasterSignInRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const response = yield api.sdkApi().farcasterSignIn({
            environmentId,
            farcasterSignInRequest,
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return handleSignInError(e);
    }
});
const farcasterVerify = (farcasterVerifyRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const response = yield api.sdkApi().farcasterVerify(farcasterVerifyRequest);
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return handleVerifyError(e);
    }
});
const telegramSignIn = (environmentId, oauthResultRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const telegramSignInRequest = {
        environmentId,
        oauthResultRequest,
    };
    try {
        const response = yield api.sdkApi().telegramSignIn(telegramSignInRequest);
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return handleSignInError(e);
    }
});
const isUserLinkedWithTelegram = (environmentId, oauthResultRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const telegramCheckAuthRequest = {
        environmentId,
        oauthResultRequest,
    };
    try {
        yield api.sdkApi().telegramCheckAuth(telegramCheckAuthRequest);
        return true;
    }
    catch (e) {
        return false;
    }
});
const telegramVerify = (environmentId, oauthResultRequest) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const telegramVerifyRequest = {
        environmentId,
        oauthResultRequest,
    };
    try {
        const response = yield api.sdkApi().telegramVerify(telegramVerifyRequest);
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        return handleVerifyError(e);
    }
});
const handleVerifyError = (e) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const data = yield utils.logResponseError(e, 'error verifying social');
    if (data.code === 'social_account_already_exists') {
        throw new utils$1.SocialAccountAlreadyExistsError();
    }
    if (data.error.code === 'sandbox_maximum_threshold_reached') {
        throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
    }
    return undefined;
});
const handleSignInError = (e) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    var _f, _g, _h;
    const data = yield utils.logResponseError(e, 'error signin social');
    if (data.error.code === 'gate_blocked') {
        throw new utils$1.GateBlockedError(data.payload.walletPublicKey);
    }
    if (data.error.code === 'missing_from_list') {
        throw new utils$1.NoAccessError({
            socialProvider: (_f = data === null || data === void 0 ? void 0 : data.payload) === null || _f === void 0 ? void 0 : _f.socialProvider,
            socialUsername: (_g = data === null || data === void 0 ? void 0 : data.payload) === null || _g === void 0 ? void 0 : _g.socialUsername,
            walletPublicKey: (_h = data === null || data === void 0 ? void 0 : data.payload) === null || _h === void 0 ? void 0 : _h.walletPublicKey,
        });
    }
    if (data.code === 'email_associated_with_different_provider') {
        throw new utils$1.AccountExistsError(data.error, data.payload);
    }
    if (data.error.code === 'sandbox_maximum_threshold_reached') {
        throw new utils$1.SandboxMaximumThresholdReachedError(data.error.message);
    }
    return undefined;
});

exports.farcasterSignIn = farcasterSignIn;
exports.farcasterVerify = farcasterVerify;
exports.getOAuthResult = getOAuthResult;
exports.initAuth = initAuth;
exports.isUserLinkedWithTelegram = isUserLinkedWithTelegram;
exports.signInOAuth = signInOAuth;
exports.telegramSignIn = telegramSignIn;
exports.telegramVerify = telegramVerify;
exports.unlinkOAuth = unlinkOAuth;
exports.verifyOAuth = verifyOAuth;
