import type { CollectionData, CollectionRule } from './types/api'; async function loadCollections(): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch('/api/collections', { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const data = await response.json(); renderCollections(data.collections || []); } } catch (error) { console.error('Failed to load collections:', error); } } function renderCollections(collections: CollectionData[]): void { const container = document.getElementById('collections-list'); if (!container) return; if (collections.length === 0) { container.innerHTML = '

No collections yet

'; return; } container.innerHTML = collections.map(collection => `
${collection.icon || '📁'}

${collection.name}

${collection.description ? `

${collection.description}

` : ''}
`).join(''); } async function loadCollectionRules(collectionId: string): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch(`/api/collections/${collectionId}/rules`, { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const rules: CollectionRule[] = await response.json(); renderRules(rules); } } catch (error) { console.error('Failed to load rules:', error); } } function renderRules(rules: CollectionRule[]): void { const container = document.getElementById('rules-list'); if (!container) return; if (rules.length === 0) { container.innerHTML = '

No rules defined

'; return; } container.innerHTML = rules.map(rule => `

${rule.field} ${rule.operator} "${rule.value}"

Priority: ${rule.priority} | ${rule.enabled ? 'Enabled' : 'Disabled'}

`).join(''); } async function createRule(collectionId: string, rule: Partial): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch(`/api/collections/${collectionId}/rules`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(rule) }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Rule created'); } loadCollectionRules(collectionId); } else { const error = await response.json(); if ((window as any).showToast?.error) { (window as any).showToast.error(error.error || 'Failed to create rule'); } } } catch (error) { console.error('Failed to create rule:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to create rule'); } } } async function deleteRule(collectionId: string, ruleId: string): Promise { const token = localStorage.getItem('token'); if (!token) return; if (!confirm('Are you sure you want to delete this rule?')) return; try { const response = await fetch(`/api/collections/${collectionId}/rules/${ruleId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Rule deleted'); } loadCollectionRules(collectionId); } } catch (error) { console.error('Failed to delete rule:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to delete rule'); } } } async function testRule(collectionId: string, rule: Partial): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch(`/api/collections/${collectionId}/rules/test`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(rule) }); if (response.ok) { const results = await response.json(); renderTestResults(results); } } catch (error) { console.error('Failed to test rule:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to test rule'); } } } function renderTestResults(results: unknown[]): void { const container = document.getElementById('test-results'); if (!container) return; if (!results || (Array.isArray(results) && results.length === 0)) { container.innerHTML = '

No matching books found

'; return; } container.innerHTML = `

${Array.isArray(results) ? results.length : 0} matching books

`; } (window as any).loadCollections = loadCollections; (window as any).loadCollectionRules = loadCollectionRules; (window as any).createRule = createRule; (window as any).deleteRule = deleteRule; (window as any).testRule = testRule;