'use client'
import { dynamicEvents } from '../../../../events/dynamicEvents.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import '../../../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 '@dynamic-labs/utils';
import '../../../../utils/constants/colors.js';
import '../../../../utils/constants/values.js';
import '../../../../shared/consts/index.js';
import '../../../../../../_virtual/_tslib.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../../store/state/nonce/nonce.js';
import '@dynamic-labs/locale';
import '../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import { convertSdkUserToUserProfile } from '../convertSdkUserToUserProfile/convertSdkUserToUserProfile.js';
import { hasSocialAccountChanged } from '../hasSocialAccountChanged/hasSocialAccountChanged.js';
import { getProjectSettings } from '../../projectSettings/getProjectSettings/getProjectSettings.js';

const hasFieldChanged = (fields, record1, record2) => {
    if (!record1 || !record2)
        return false;
    for (const name of fields) {
        const oldValue = record1[name];
        const newValue = record2[name];
        if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
            return true;
        }
    }
    return false;
};
const raiseUserProfileEvent = ({ oldUser, newUser, }) => {
    var _a, _b;
    const settings = getProjectSettings();
    if (!settings)
        return;
    const hasKycChanged = hasFieldChanged(settings.kyc.map(({ name }) => name), oldUser, newUser);
    const hasCustomFieldsChanged = hasFieldChanged((_b = (_a = settings.customFields) === null || _a === void 0 ? void 0 : _a.map(({ name }) => name)) !== null && _b !== void 0 ? _b : [], oldUser === null || oldUser === void 0 ? void 0 : oldUser.metadata, newUser.metadata);
    // Only check for social account changes if oldUser exists (not initial sign-up)
    const socialAccountChanged = oldUser
        ? hasSocialAccountChanged(oldUser, newUser)
        : false;
    if (!hasKycChanged && !hasCustomFieldsChanged && !socialAccountChanged)
        return;
    dynamicEvents.emit('userProfileUpdated', convertSdkUserToUserProfile(newUser));
};

export { hasFieldChanged, raiseUserProfileEvent };
