// ── Shared app state ──
import { esc } from './util.js';
export const state = {
cards: [],
buckets: [],
activeBucket: 'all',
activeTags: [],
hideTermine: localStorage.getItem('hyprcrd-hide-termine') === '1',
superView: localStorage.getItem('hyprcrd-superview') === '1',
activeGroup: null,
sortMode: localStorage.getItem('hyprcrd-sort') || 'manual',
editingId: null,
viewingCard: null,
openedFromSearch: false,
openedFromGallery: false,
galleryOrigin: null,
returnToGalleryAfterEdit: false,
previewVisible: false,
};
export const SORT_MODES = ['manual', 'newest', 'oldest', 'alpha', 'tag'];
export const SORT_ICONS = { manual: 'ph-dots-six', newest: 'ph-sort-descending', oldest: 'ph-sort-ascending', alpha: 'ph-text-aa', tag: 'ph-hash' };
export const CARD_COLORS = [
{ name: 'sky', hex: '#38bdf8' }, { name: 'violet', hex: '#8b5cf6' },
{ name: 'rose', hex: '#fb7185' }, { name: 'amber', hex: '#fbbf24' },
{ name: 'green', hex: '#4ade80' }, { name: 'orange', hex: '#fb923c' },
{ name: 'cyan', hex: '#22d3ee' }, { name: 'pink', hex: '#f472b6' },
{ name: 'lime', hex: '#a3e635' }, { name: 'slate', hex: '#94a3b8' },
];
export function parseTags(card) {
return (card.tags || '').split(',').filter(Boolean);
}
export function tagChipHtml(tag) {
const parts = tag.split('/');
const segs = parts.map((p, i) => {
const path = parts.slice(0, i + 1).join('/');
return `${i === 0 ? '#' : ''}${esc(p)}`;
});
return `${segs.join('/')}`;
}
function effectiveDate(c) {
const m = (c.title || '').match(/^(\d{4}-\d{2}-\d{2})/);
if (m) return m[1];
return (c.created_at || '').substring(0, 10);
}
export function sortedCards(cards) {
const arr = [...cards];
switch (state.sortMode) {
case 'newest': return arr.sort((a, b) => effectiveDate(b).localeCompare(effectiveDate(a)));
case 'oldest': return arr.sort((a, b) => effectiveDate(a).localeCompare(effectiveDate(b)));
case 'alpha': return arr.sort((a, b) => (a.title || '').localeCompare(b.title || ''));
case 'tag': return arr.sort((a, b) => {
const at = (a.tags || '').split(',')[0] || '\uffff';
const bt = (b.tags || '').split(',')[0] || '\uffff';
return at.localeCompare(bt);
});
default: return arr.sort((a, b) => (a.position || 0) - (b.position || 0));
}
}
export function getChildren(parentId) {
return state.cards.filter(c => c.parent_id == parentId);
}
export function getGroupCards(card) {
if (card.parent_id) {
const parent = state.cards.find(c => c.id == card.parent_id);
if (parent) return [parent, ...getChildren(parent.id)];
}
const children = getChildren(card.id);
if (children.length) return [card, ...children];
return null;
}
// Recently opened
export function getRecent() {
try { return JSON.parse(localStorage.getItem('hyprcrd-recent') || '[]'); } catch { return []; }
}
export function markRecent(id) {
const r = getRecent().filter(i => i != id);
r.unshift(String(id));
localStorage.setItem('hyprcrd-recent', JSON.stringify(r.slice(0, 10)));
}
// Favorites
export function getFavorites() {
try { return JSON.parse(localStorage.getItem('hyprcrd-favorites') || '[]'); } catch { return []; }
}
export function toggleFavorite(id) {
const favs = getFavorites();
const sid = String(id);
const idx = favs.indexOf(sid);
if (idx !== -1) favs.splice(idx, 1);
else favs.push(sid);
localStorage.setItem('hyprcrd-favorites', JSON.stringify(favs));
return favs.includes(sid);
}
export function isFavorite(id) { return getFavorites().includes(String(id)); }