import type { QueueItemResponse } from './types/api'; async function refreshQueue(): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch('/api/queue/all', { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const data = await response.json(); renderQueueItems(data.items || []); } } catch (error) { console.error('Failed to refresh queue:', error); } } async function processPendingItems(): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch('/api/queue/process', { method: 'POST', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Processing queue items'); } refreshQueue(); } } catch (error) { console.error('Failed to process queue:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to process queue'); } } } async function clearFailedItems(): Promise { const token = localStorage.getItem('token'); if (!token) return; if (!confirm('Are you sure you want to clear all failed items?')) return; try { const response = await fetch('/api/queue/failed', { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Failed items cleared'); } refreshQueue(); } } catch (error) { console.error('Failed to clear failed items:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to clear items'); } } } async function clearAllItems(): Promise { const token = localStorage.getItem('token'); if (!token) return; if (!confirm('Are you sure you want to clear all queue items?')) return; try { const response = await fetch('/api/queue/all', { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Queue cleared'); } refreshQueue(); } } catch (error) { console.error('Failed to clear queue:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to clear queue'); } } } async function retryQueueItem(itemId: string): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch(`/api/queue/items/${itemId}/retry`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Item queued for retry'); } refreshQueue(); } } catch (error) { console.error('Failed to retry item:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to retry item'); } } } async function deleteQueueItem(itemId: string): Promise { const token = localStorage.getItem('token'); if (!token) return; try { const response = await fetch(`/api/queue/items/${itemId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { if ((window as any).showToast?.success) { (window as any).showToast.success('Item deleted'); } refreshQueue(); } } catch (error) { console.error('Failed to delete item:', error); if ((window as any).showToast?.error) { (window as any).showToast.error('Failed to delete item'); } } } function renderQueueItems(items: QueueItemResponse[]): void { const container = document.getElementById('queue-items'); if (!container) return; if (items.length === 0) { container.innerHTML = '

Queue is empty

'; return; } container.innerHTML = items.map(item => `

${item.media_title || 'Unknown'}

${item.status} - ${item.sync_type}

Attempts: ${item.attempts}/${item.max_attempts}

${item.status === 'failed' ? `` : ''}
${item.error_message ? `

${item.error_message}

` : ''}
`).join(''); } (window as any).refreshQueue = refreshQueue; (window as any).processPendingItems = processPendingItems; (window as any).clearFailedItems = clearFailedItems; (window as any).clearAllItems = clearAllItems; (window as any).retryQueueItem = retryQueueItem; (window as any).deleteQueueItem = deleteQueueItem;