'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { EmailAlreadyExistsError, CustomFieldNotUniqueError, UsernameAlreadyExistsError, TooManyEmailVerificationsError, InvalidPhoneNumberError } from '@dynamic-labs/utils';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import '../../../client/client.js';
import 'react';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../../utils/constants/colors.js';
import '../../../utils/constants/values.js';
import '../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import { sdkApi } from '../api.js';
import { logResponseError } from '../utils.js';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../events/dynamicEvents.js';
import { storeTokenAndUser } from '../../../client/extension/storeTokenAndUser/storeTokenAndUser.js';

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

export { getUserFieldsCheck, hardDeleteUser, mergeUserAccounts, refreshUserJwt, updateUserProfileFields, verifyMergeUsers };
