// Выбор фона одного личного чата. Нажатие сразу меняет чат за шторкой: страница слушает DM_CHAT_PREFS_EVENT.
import { t } from '../i18n/index.js';
import { iconHtml } from './ui-icon.js';
import { openSheet } from './sheet.js';
import { getDmChatBackground, setDmChatBackground } from '../services/dm-chat-prefs.js';
import { findDmChatBackground, listDmChatBackgrounds, paintDmChatBackground } from '../services/dm-chat-backgrounds.js';
export function openDmBackgroundSheet(chatId) {
const hint = document.createElement('p');
hint.className = 'meta-muted';
hint.textContent = t('screens.dm_background.hint');
const grid = document.createElement('div');
grid.className = 'dm-bg-options';
grid.setAttribute('role', 'group');
grid.setAttribute('aria-label', t('screens.dm_background.title'));
const buttons = [{ id: '', label: t('screens.dm_background.none') }, ...listDmChatBackgrounds()].map(({ id, label }) => {
const button = document.createElement('button');
button.type = 'button';
button.className = 'ui-button dm-bg-option';
const preview = document.createElement('span');
preview.className = 'dm-bg-option__preview dm-bg-image';
preview.setAttribute('aria-hidden', 'true');
preview.innerHTML = `${iconHtml('check')}`;
paintDmChatBackground(preview, findDmChatBackground(id));
const name = document.createElement('span');
name.className = 'dm-bg-option__name';
name.textContent = label;
button.append(preview, name);
button.addEventListener('click', () => {
setDmChatBackground(chatId, id);
markSelected();
});
grid.append(button);
return { id, button };
});
const markSelected = () => {
const current = findDmChatBackground(getDmChatBackground(chatId))?.id || '';
buttons.forEach(({ id, button }) => button.setAttribute('aria-pressed', String(id === current)));
};
markSelected();
const content = document.createElement('div');
content.className = 'dm-bg-sheet__content';
content.append(hint, grid);
return openSheet({ title: t('screens.dm_background.title'), className: 'dm-bg-sheet', content });
}