import {
addFreeformLine,
addFreeformList,
categoryLabel,
deleteList,
getActiveList,
getListSummaries,
KITCHEN_LIST_ID,
removeItemFromList,
setActiveListId,
toggleItemInList,
} from '../services/pantryShopping.js';
import { showAppToast } from '../ui/toast.js';
function escapeHtml(s) {
return String(s)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
export function getShoppingHTML() {
return `
`;
}
function syncListSelect() {
const sel = document.getElementById('shopping-list-select');
if (!sel) return;
const { lists, activeListId } = getListSummaries();
sel.innerHTML = lists.map((l) => {
const suffix = l.openCount ? ` (${l.openCount})` : '';
const label = `${l.name}${suffix}`;
return ``;
}).join('');
sel.value = activeListId;
}
function syncChromeForList() {
const list = getActiveList();
const isKitchen = list.type === 'kitchen';
const delBtn = document.getElementById('shopping-delete-list');
const ffAdd = document.getElementById('shopping-freeform-add');
if (ffAdd) ffAdd.classList.toggle('hidden', isKitchen);
if (delBtn) {
delBtn.classList.toggle('hidden', isKitchen);
}
}
function renderKitchenItems() {
const root = document.getElementById('shopping-list-root');
if (!root) return;
const list = getActiveList();
if (list.type !== 'kitchen') return;
const items = list.items;
if (items.length === 0) {
root.innerHTML = 'Brak pozycji.
';
return;
}
const groups = {};
for (const it of items) {
const c = it.category || 'inne';
if (!groups[c]) groups[c] = [];
groups[c].push(it);
}
root.innerHTML = Object.keys(groups)
.sort((a, b) => categoryLabel(a).localeCompare(categoryLabel(b)))
.map((cat) => `
${escapeHtml(categoryLabel(cat))}
${groups[cat].map((it) => `
-
${escapeHtml(it.name)}
${escapeHtml(String(it.amount))} ${escapeHtml(it.unit)}
${it.sourceNote ? `
${escapeHtml(it.sourceNote)}
` : ''}
`).join('')}
`)
.join('');
bindItemButtons(list.id);
}
function renderFreeformItems() {
const root = document.getElementById('shopping-list-root');
if (!root) return;
const list = getActiveList();
if (list.type !== 'freeform') return;
const items = list.items;
if (items.length === 0) {
root.innerHTML = 'Dodaj dowolny tekst powyżej — bez powiązania z katalogiem składników.
';
return;
}
root.innerHTML = `
${items.map((it) => `
-
${escapeHtml(it.text)}
${it.note ? `
${escapeHtml(it.note)}
` : ''}
`).join('')}
`;
bindItemButtons(list.id);
}
function bindItemButtons(listId) {
const root = document.getElementById('shopping-list-root');
if (!root) return;
root.querySelectorAll('[data-shop-toggle]').forEach((btn) => {
btn.addEventListener('click', () => {
const id = btn.getAttribute('data-shop-toggle');
if (id) toggleItemInList(listId, id);
refreshShopping();
});
});
root.querySelectorAll('[data-shop-remove]').forEach((btn) => {
btn.addEventListener('click', () => {
const id = btn.getAttribute('data-shop-remove');
if (id) removeItemFromList(listId, id);
refreshShopping();
});
});
}
export function refreshShopping() {
syncListSelect();
syncChromeForList();
const list = getActiveList();
if (list.type === 'kitchen') renderKitchenItems();
else renderFreeformItems();
}
export function setupShopping() {
const sel = document.getElementById('shopping-list-select');
sel?.addEventListener('change', () => {
const v = sel.value;
if (v) setActiveListId(v);
refreshShopping();
});
document.getElementById('shopping-new-list')?.addEventListener('click', () => {
const name = window.prompt('Nazwa nowej listy (dowolne zakupy):', 'Nowa lista');
if (name === null) return;
addFreeformList(name);
showAppToast('Utworzono listę.');
refreshShopping();
});
document.getElementById('shopping-delete-list')?.addEventListener('click', () => {
const list = getActiveList();
if (list.id === KITCHEN_LIST_ID) return;
if (!window.confirm(`Usunąć listę „${list.name}”?`)) return;
deleteList(list.id);
showAppToast('Lista usunięta.');
refreshShopping();
});
const submitFreeform = () => {
const list = getActiveList();
if (list.type !== 'freeform') return;
const input = document.getElementById('shopping-freeform-input');
const note = document.getElementById('shopping-freeform-note');
const text = input?.value || '';
addFreeformLine(list.id, text, note?.value || '');
if (input) input.value = '';
if (note) note.value = '';
refreshShopping();
};
document.getElementById('shopping-freeform-submit')?.addEventListener('click', submitFreeform);
document.getElementById('shopping-freeform-input')?.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
submitFreeform();
}
});
refreshShopping();
window.refreshShopping = refreshShopping;
}