// src/economic/extraction_script.js
const events = [];
let currentDate = '';

const rows = document.querySelectorAll('#TeletraderForm table tbody tr');

for (let i = 0; i < rows.length; i++) {
    const row = rows[i];
    const cells = row.querySelectorAll('td');

    if (cells.length === 1 && cells[0].colSpan === 9) {
        const dateText = cells[0].textContent.trim();
        const monthMap = {
            'Januar': '01', 'Februar': '02', 'März': '03', 'April': '04',
            'Mai': '05', 'Juni': '06', 'Juli': '07', 'August': '08',
            'September': '09', 'Oktober': '10', 'November': '11', 'Dezember': '12'
        };
        const match = dateText.match(/(\d{1,2})\.\s+([a-zA-ZäöüßÄÖÜ]+)\s+(\d{4})/);
        if (match) {
            const day = match[1].padStart(2, '0');
            const month = monthMap[match[2]] || '01';
            const year = match[3];
            currentDate = `${year}-${month}-${day}`;
        } else {
            currentDate = '';
        }
        continue;
    }

    if (cells.length >= 8) {
        const time = cells[0]?.textContent?.trim() || '';
        const country = cells[2]?.textContent?.trim() || '';
        const eventName = cells[4]?.textContent?.trim() || '';
        if (!time || !country || !eventName) continue;

        const yellowStars = cells[3]?.querySelectorAll('.icon--star.font-color-yellow').length || 0;
        if (yellowStars !== 3) continue;

        let description = '';
        if (i + 1 < rows.length) {
            const next = rows[i + 1];
            const descP = next.querySelector('p');
            if (descP) description = descP.textContent?.trim() || '';
        }

        events.push({
            country,
            date: currentDate,
            time,
            event: eventName,
            actual: cells[7]?.textContent?.trim() || '',
            forecast: cells[6]?.textContent?.trim() || '',
            previous: cells[5]?.textContent?.trim() || '',
            importance: 'High',
            description
        });
    }
}

return events;