'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');
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 updateUserProfileFields = (environmentId, fields) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const response = yield api.sdkApi().updateSelf({
            environmentId,
            userFields: fields,
        });
        const userProfile = storeTokenAndUser.storeTokenAndUser(response);
        return Object.assign(Object.assign({}, response), { userProfile });
    }
    catch (e) {
        const error = yield utils.logResponseError(e, 'Error updateUserProfileFields');
        if (e instanceof Response) {
            if (error.code === 'email_already_exists') {
                throw new utils$1.EmailAlreadyExistsError();
            }
            else if (error.code === 'custom_field_data_not_unique') {
                throw new utils$1.CustomFieldNotUniqueError();
            }
            else if (error.code === 'username_already_exists') {
                throw new utils$1.UsernameAlreadyExistsError();
            }
            else if (error.code === 'too_many_verification_attempts') {
                throw new utils$1.TooManyEmailVerificationsError();
            }
            else if (error.code === 'invalid_phone' ||
                error.code === 'invalid_sms_verification') {
                throw new utils$1.InvalidPhoneNumberError(error.error);
            }
        }
        throw e;
    }
});
const mergeUserAccounts = (environmentId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const response = yield api.sdkApi().mergeUsers({
            environmentId,
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        yield utils.logResponseError(e, 'Error mergeUserAccounts');
        throw e;
    }
});
const verifyMergeUsers = (environmentId, mergeConflictResolutions) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const response = yield api.sdkApi().verifyMergeUsers({
            environmentId,
            mergeUserConflictResolutions: {
                mergeUserResolutions: mergeConflictResolutions,
            },
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        yield utils.logResponseError(e, 'Error verifyMergeUsers');
        throw e;
    }
});
const hardDeleteUser = (environmentId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    try {
        yield api.sdkApi().hardDeleteUser({ environmentId });
    }
    catch (e) {
        yield utils.logResponseError(e, 'Error hardDeleteUser');
        throw e;
    }
});
const refreshUserJwt = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ environmentId, }) {
    try {
        const response = yield api.sdkApi().refreshAuth({
            environmentId,
        });
        const user = storeTokenAndUser.storeTokenAndUser(response);
        return user;
    }
    catch (e) {
        yield utils.logResponseError(e, 'Error refreshUserJwt');
        return undefined;
    }
});
/** Hook implementation of [getUserFieldsCheck] */
const getUserFieldsCheck = (_c) => _tslib.__awaiter(void 0, [_c], void 0, function* ({ environmentId, filter, }) {
    try {
        return yield api.sdkApi().getUserFieldsCheck({
            environmentId,
            filter,
        });
    }
    catch (e) {
        yield utils.logResponseError(e, 'Error checking user field availability');
        return undefined;
    }
});

exports.getUserFieldsCheck = getUserFieldsCheck;
exports.hardDeleteUser = hardDeleteUser;
exports.mergeUserAccounts = mergeUserAccounts;
exports.refreshUserJwt = refreshUserJwt;
exports.updateUserProfileFields = updateUserProfileFields;
exports.verifyMergeUsers = verifyMergeUsers;
