import type { ApiNotifyException } from '../../api/types'; import type { ISettings, IThemeSettings, NotifyException, ThemeKey, } from '../../types'; import type { GlobalState } from '../types'; import { updateUserBlockedState } from './users'; export function replaceSettings(global: T, newSettings?: Partial): T { return { ...global, settings: { ...global.settings, byKey: { ...global.settings.byKey, ...newSettings, }, }, }; } export function replaceThemeSettings( global: T, theme: ThemeKey, newSettings?: Partial, ): T { return { ...global, settings: { ...global.settings, themes: { ...global.settings.themes, [theme]: { ...(global.settings.themes[theme] || {}), ...newSettings, }, }, }, }; } export function addNotifyExceptions( global: T, notifyExceptions: ApiNotifyException[], ): T { notifyExceptions.forEach((notifyException) => { const { chatId, ...exceptionData } = notifyException; global = addNotifyException(global, chatId, exceptionData); }); return global; } export function addNotifyException( global: T, id: string, notifyException: NotifyException, ): T { return { ...global, settings: { ...global.settings, notifyExceptions: { ...global.settings.notifyExceptions, [id]: notifyException, }, }, }; } // eslint-disable-next-line consistent-return export function updateNotifySettings( global: T, peerType: 'contact' | 'group' | 'broadcast', isSilent?: boolean, shouldShowPreviews?: boolean, ): T { switch (peerType) { case 'contact': return replaceSettings(global, { ...(typeof isSilent !== 'undefined' && { hasPrivateChatsNotifications: !isSilent }), ...(typeof shouldShowPreviews !== 'undefined' && { hasPrivateChatsMessagePreview: shouldShowPreviews }), }); case 'group': return replaceSettings(global, { ...(typeof isSilent !== 'undefined' && { hasGroupNotifications: !isSilent }), ...(typeof shouldShowPreviews !== 'undefined' && { hasGroupMessagePreview: shouldShowPreviews }), }); case 'broadcast': return replaceSettings(global, { ...(typeof isSilent !== 'undefined' && { hasBroadcastNotifications: !isSilent }), ...(typeof shouldShowPreviews !== 'undefined' && { hasBroadcastMessagePreview: shouldShowPreviews }), }); } } export function addBlockedUser(global: T, contactId: string): T { global = updateUserBlockedState(global, contactId, true); return { ...global, blocked: { ...global.blocked, ids: [contactId, ...global.blocked.ids], totalCount: global.blocked.totalCount + 1, }, }; } export function removeBlockedUser(global: T, contactId: string): T { global = updateUserBlockedState(global, contactId, false); return { ...global, blocked: { ...global.blocked, ids: global.blocked.ids.filter((id) => id !== contactId), totalCount: global.blocked.totalCount - 1, }, }; }