/**
 * @file storage.js
 * @description Hantering av chrome.storage för StatusNotifier Extension
 * 
 * Author: Linus Olofsson - Iver 
 * Date: 2025-07-03 
 * Last modified: Linus Olofsson (2025-07-03)
 * 
 * @purpose Hantera all interaktion med chrome.storage
 * @invoked_by Alla andra moduler som behöver lagra/hämta data
 * @invokes Ingen
 */

// Funktion för att spara timer-inställningar
const saveTimerSettings = (settings) => {
    return new Promise((resolve, reject) => {
        chrome.storage.local.set({ 'timerSettings': settings }, () => {
            if (chrome.runtime.lastError) {
                console.error('Error saving timer settings:', chrome.runtime.lastError);
                reject(chrome.runtime.lastError);
            } else {
                console.log('Timer settings saved:', settings);
                resolve(settings);
            }
        });
    });
};

// Funktion för att hämta timer-inställningar
const getTimerSettings = () => {
    return new Promise((resolve, reject) => {
        chrome.storage.local.get('timerSettings', (result) => {
            if (chrome.runtime.lastError) {
                console.error('Error getting timer settings:', chrome.runtime.lastError);
                reject(chrome.runtime.lastError);
            } else {
                console.log('Timer settings retrieved:', result.timerSettings);
                resolve(result.timerSettings || { hours: 0, minutes: 30 });
            }
        });
    });
};

// Funktion för att spara listobjekt
const saveItems = (items) => {
    return new Promise((resolve, reject) => {
        chrome.storage.local.set({ 'scrapedItems': items }, () => {
            if (chrome.runtime.lastError) {
                console.error('Error saving items:', chrome.runtime.lastError);
                reject(chrome.runtime.lastError);
            } else {
                console.log('Items saved:', items);
                resolve(items);
            }
        });
    });
};

// Funktion för att hämta listobjekt
const getItems = () => {
    return new Promise((resolve, reject) => {
        chrome.storage.local.get('scrapedItems', (result) => {
            if (chrome.runtime.lastError) {
                console.error('Error getting items:', chrome.runtime.lastError);
                reject(chrome.runtime.lastError);
            } else {
                console.log('Items retrieved:', result.scrapedItems);
                resolve(result.scrapedItems || []);
            }
        });
    });
};

// Exportera funktioner
export {
    saveTimerSettings,
    getTimerSettings,
    saveItems,
    getItems
};
